Q-34PepCoding | Pairs With Equal Sum

PHOTO EMBED

Thu Jan 26 2023 21:32:40 GMT+0000 (Coordinated Universal Time)

Saved by @Ayush_dabas07

import java.util.*;

public class Main {

    public static boolean solution(int[] arr) {
// traverse loop in O(n^2) and store all sum in hashset , if ever any sum repeats return true else return false

        HashSet<Integer>set = new HashSet<>();
        
        for(int i =0 ;i < arr.length-1 ;i++){
            for(int j = i+1 ; j < arr.length;j++ ){
                int sum = arr[i]+arr[j];
                
                if(set.contains(sum))
                return true;
                
                set.add(sum);
            }
        }
        return false;
    }
    
    public static void main(String[] args) {
        Scanner scn = new Scanner(System.in);
        int n = scn.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = scn.nextInt();
        }
        System.out.println(solution(arr));
    }

}
content_copyCOPY

https://www.pepcoding.com/resources/data-structures-and-algorithms-in-java-levelup/hashmap-and-heaps/pairs-with-equal-sum-official/ojquestion