This question is from geeksforgeeks :
Here’s the link: https://practice.geeksforgeeks.org/problems/find-all-pairs-whose-sum-is-x5808/1#
Q) Given two unsorted arrays A of size N and B of size M of distinct elements, the task is to find all pairs from both arrays whose sum is equal to X.
import java.util.*; import java.lang.*; import java.io.*; class pair { long first, second; public pair(long first, long second) { this.first = first; this.second = second; } } class GFG { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine().trim()); while(t-->0) { StringTokenizer stt = new StringTokenizer(br.readLine()); long N = Long.parseLong(stt.nextToken()); long M = Long.parseLong(stt.nextToken()); long X = Long.parseLong(stt.nextToken()); long A[] = new long[(int)(N)]; long B[] = new long[(int)(M)]; String inputLine[] = br.readLine().trim().split(" "); for (int i = 0; i < N; i++) { A[i] = Long.parseLong(inputLine[i]); } String inputLine1[] = br.readLine().trim().split(" "); for (int i = 0; i < M; i++) { B[i] = Long.parseLong(inputLine1[i]); } Solution obj = new Solution(); pair [] answer = obj.allPairs(A, B, N, M, X); int sz = answer.length; if(sz==0) System.out.println(-1); else{ StringBuilder output = new StringBuilder(); for(int i=0;i<sz;i++){ if(i<sz-1) output.append(answer[i].first +" "+ answer[i].second + ", "); else output.append(answer[i].first +" "+ answer[i].second); } System.out.println(output); } } } } // } Driver Code Ends //User function Template for Java /* class pair { long first, second; public pair(long first, long second) { this.first = first; this.second = second; } } */ class Solution { public pair[] allPairs( long A[], long B[], long N, long M, long X) { //MY CODE STARTS FROM HERE ArrayList<pair> list = new ArrayList<>(); HashSet<Long> set = new HashSet<>(); for(long i : A){ set.add(i); } for(int i=0;i<M;i++){ if(set.contains(X-B[i])){ list.add(new pair(X-B[i], B[i])); } } pair arr[] = new pair[list.size()]; arr = list.toArray(arr); for(int j=0;j<(arr.length-1);j++){ if(arr[j]>arr[j+1]){ pair temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; j -= 1; } } return arr; } }
I have to sort the result in ascending order but the datatype for array here is user-defined (i.e. pair). So I am getting this error :
prog.java:100: error: bad operand types for binary operator ‘>’ if(arr[j]>arr[j+1]){ ^ first type: pair second type: pair 1 error
Please help me sort the array. Note: Time complexity should be O(nlogn)
Advertisement
Answer
Why use the custom sort when you can use built-in sort in Java list.
Just use list.sort(Comparator.comparing(p -> ((pair) p).first).thenComparing(p -> ((pair) p).second));
to sort the pair list.
public pair[] allPairs( long A[], long B[], long N, long M, long X) { // Your code goes here //MY CODE STARTS FROM HERE ArrayList<pair> list = new ArrayList<>(); HashSet<Long> set = new HashSet<>(); for(long i : A){ set.add(i); } for(int i=0;i<M;i++){ if(set.contains(X-B[i])){ list.add(new pair(X-B[i], B[i])); } } list.sort( Comparator .comparing(p -> ((pair) p).first) .thenComparing(p -> ((pair) p).second) ); return list.toArray(pair[]::new); }