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)); } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter