We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Here is a simple approaching in Java:
1. sort both array1 and array2, one in ascending and one in descending order
2. add biggest to smallest, 2nd biggest to 2nd smallest and so on... If any of these pairs's sum are smaller than k, return false.
3. After checking both arrays, if no pair's sum is smaller than k, return true
importjava.io.*;importjava.util.*;publicclassSolution{publicstaticvoidmain(String[]args){Scanners=newScanner(System.in);intt=s.nextInt();for(inti_t=0;i_t<t;i_t++){intn=s.nextInt();Integerk=s.nextInt();Integer[]arr1=newInteger[n];Integer[]arr2=newInteger[n];for(inti=0;i<n;i++){arr1[i]=s.nextInt();}for(inti=0;i<n;i++){arr2[i]=s.nextInt();}if(isPermutable(arr1,arr2,k)){System.out.println("YES");}else{System.out.println("NO");}}}staticbooleanisPermutable(Integer[]arr1,Integer[]arr2,Integerk){//sort in ascending orderArrays.sort(arr1);//sort in descending orderArrays.sort(arr2,Collections.reverseOrder());//matching largest with smaller, 2nd largest with 2nd smallest, and so onfor(inti=0;i<arr1.length;i++){//if any of those pairs is less than k, return falseif(arr1[i]+arr2[i]<k){returnfalse;}}//after traversing the whole length, return truereturntrue;}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Permuting Two Arrays
You are viewing a single comment's thread. Return to all comments →
Here is a simple approaching in Java: 1. sort both array1 and array2, one in ascending and one in descending order 2. add biggest to smallest, 2nd biggest to 2nd smallest and so on... If any of these pairs's sum are smaller than k, return false. 3. After checking both arrays, if no pair's sum is smaller than k, return true