In: Computer Science
Given:
S = [ 2, 1, 3, 4, 7, 5 ]
Z = 8
Create a function that will return true if numbers X and Y exist within S such that X + Y = Z, return false otherwise.
Code in Java 8.
Source Code:
import java.util.Scanner;
class ex
{
static boolean isIn(int arr[],int X)
{
int len=arr.length;
boolean isFound=false;
for(int i=0;i
if(arr[i]==X)
{
isFound=true;
break;
}
}
return isFound;
}
static boolean isThere(int arr[],int X,int Y)
{
boolean isExist=false;
if(isIn(arr,X) &&
isIn(arr,Y))
{
if((X+Y)==8)
isExist=true;
}
return isExist;
}
public static void main(String[] args)
{
Scanner input=new
Scanner(System.in);
System.out.print("Enter the value
for X:");
int X=input.nextInt();
System.out.print("Enter the value
for Y:");
int Y=input.nextInt();
int arr[]=new
int[]{2,1,3,4,7,5};
boolean res=isThere(arr,X,Y);
if(res==true)
System.out.println("Yes there Exist.");
else
System.out.println("No there does not Exist.");
}
}
sample input and output: