Pair Sum Existence- Revisited | Practice | GeeksforGeeks

You are given an array A of size N, and you are also given a sum. You need to find if two numbers in A exists that have sum equal to the given sum.
Input Format:
The first line of input contains T denoting the number of testcases. T testcases follow. Each testcase contains three lines of input. The first line contains N denoting the size of the array A. The second line contains N elements of the array. The third line contains element sum.
Output Format:
For each testcase, in a new line, print the required output.
Your Task:
Since this is a function problem, you don't need to take any input. Just complete the provided function sumExists().
Constraints:
1 <= T <= 100
1 <= N <= 1000
1 <= Ai <= 106
Examples:
Input:
1
10
1 2 3 4 5 6 7 8 9 10
14
Output:
1
+2
yogeshrajendra10106 months ago
// Easy JAVA Solution Using HashSet.
TC:- O(n) , SC:-O(n)
public static int sumExists(int arr[], int n, int sum)   {       // your code here       HashSet<Integer>hs=new HashSet<>();       for(int i=0;i<arr.length;i++){           if(hs.contains(sum-arr[i])){               return 1;           }           else{               hs.add(arr[i]);           }       }       return 0;   }
0
Jayesh Badgujar8 months ago
Jayesh Badgujar
https://uploads.disquscdn.c...
0
Amir Ansari1 year ago
Amir Ansari
Correct AnswerExecution Time:0.75
public static int sumExists(int arr[], int n, int sum)    {          Map<integer, integer="">map = new HashMap<integer, integer="">();
     for(int i = 0; i < n;i++) {     if(map.get(sum-arr[i]) != null) {     return 1;     }     map.put(arr[i], i);     }      return 0;    }
0
Sumit Pardhiya1 year ago
Sumit Pardhiya
Arrays.sort(arr);        int l=0;        int r=n-1;        int c=0;        while(l<r) {="" if(arr[l]+arr[r]="=sum)" {="" c++;="" l++;="" r--;="" }="" else="" if(arr[l]+arr[r]<sum)="" {="" l++;="" }="" else="" {="" r--;="" }="" }="" if(c="=0)" return="" 0;="" return="" 1;="">
0
Sanjit Paul1 year ago
Sanjit Paul
Easy Java solution in O(n) timehttps://ide.geeksforgeeks.o...
0
pranav kul1 year ago
pranav kul
public static boolean isDoublyFrequent(int[] arr, int num){        int count = 0;        for(int x : arr){            if(x==num){                count++;            }        }        return count==2;    }    // function to check if any pair having specified sum    // and return 1 if so, else 0    public static int sumExists(int arr[], int n, int sum)    {        // your code here        HashMap<integer, integer=""> diff = new HashMap<>();        for(Integer x: arr){            diff.putIfAbsent(x, sum-x);        }
        for(Integer y: arr){            if( y == (sum/2) ){                if( isDoublyFrequent(arr,y) ){                    return 1;                }            }            else if( diff.containsValue(y) ){                return  1;            }        }
        return 0;    }
0
Isha Gupta2 years ago
Isha Gupta
// SIMPLE CLEAN SOLUTION//class Geeks{    // function to check if any pair having specified sum    // and return 1 if so, else 0    public static int sumExists(int arr[], int n, int sum)    {        for(int i=0;i<n-1;i++){ for(int="" j="i+1;j&lt;n;j++)" if(arr[i]+arr[j]="=" sum)="" return="" 1;="" }="" return="" 0;="" }="" }="">
0
Ansh Gupta2 years ago
Ansh Gupta
JAVA SOLUTION:
public static int sumExists(int arr[], int n, int sum)    {        HashSet<integer> set=new HashSet<>();        set.add(arr[0]);        for(int i=1;i<n;i++){ if(set.contains(sum-arr[i]))="" return="" 1;="" else="" set.add(arr[i]);="" }="" return="" 0;="" }="">
0
Toshik Langade2 years ago
Toshik Langade
First line should contain the number of test cases but in some test cases the line is missing.How to solve this problem?
0
Sneha Bandi3 years ago
Sneha Bandi
https://uploads.disquscdn.c...
This code can be referred
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.