Find the sum of pairs from array in java
Enter the array size as input, array elements as separated by space as input. Then the third input as integer which sum up of array elements.
Input:
7
33 12 -76 11 9 7 6
20
Output:
true
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class HackerRank { | |
public static void main(String[] args) { | |
Scanner in = new Scanner(System.in); | |
int arraySize=0; | |
arraySize=Integer.parseInt(in.nextLine().trim()); | |
String [] arr=new String[arraySize]; | |
String elements=in.nextLine(); | |
arr=elements.split(" "); //splitting spaces and saving it into arrays. | |
int find; | |
find=Integer.parseInt(in.nextLine().trim()); //number to be found in the array | |
boolean isExist=false; | |
for(int a=0;a<arraySize-1;a++){ | |
int x=Integer.parseInt(arr[a]); //converting string to integer | |
int y=Integer.parseInt(arr[a+1]); | |
if((x+y)==find){ | |
isExist=true; | |
break; //if condition matches break the loop | |
} | |
} | |
System.out.println(isExist?"true":"false"); | |
in.close(); | |
} | |
} |
Input:
7
33 12 -76 11 9 7 6
20
Output:
true
Comments
Post a Comment