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.
My solution based on @zh2196 's approach.It passes all the test cases.
//the numbers can be very large,so we take longstaticArrayList<Long>li;staticArrayList<Long>lu;staticlongfairCut(intk1,ArrayList<Long>arr){li=newArrayList<Long>();lu=newArrayList<Long>();Collections.sort(arr);System.out.println("sorted list is:"+arr);intn=arr.size();intk=Math.min(k1,n-k1);//li will always get the element at position at n/2// (doesnt matter whether n is even or odd)//if k is 1 then give li n/2th element and find absolute differenceif(k==1){li.add(arr.get(n/2));arr.remove(n/2);returncalcResult(li,arr);}//if k is greater then 1,then we must make the 01010101 //combination//assume that li denotes 0 and lu denotes 1//now to ensure the combination,li must skip one element and take //the immediately following element li.add(arr.get(n/2));//loc1 is a leftPointer and loc2 is rightPointerintloc=n/2,loc1=n/2,loc2=n/2;arr.set(n/2,(long)0);//change the position at n/2 to 0//this means that the element at this position has been given to li//then in the calcResult function,continue when this 0 is //encountered--k;while(k>1){//skipping elements is possible in both directions,left and right//loc1 will take every "other" element in the left direction//(this means that it will take element after skipping one //element) similarly loc2 will take every other element in right directionloc1=loc1-2;loc2=loc2+2;//adding 2 elements,one from both directions into li listli.add(arr.get(loc1));li.add(arr.get(loc2));arr.set(loc1,(long)0);arr.set(loc2,(long)0);k-=2;//two more elements were addded to li's list so decrement k count}if(k==0)//all the requisite elements have been obtainedreturncalcResult(li,arr);elseif(k==1){//now one element more is to be added //we will have to choose an element that gives minimum resultlongx1=loc1-2;//x1 is index position of the element after skipping one //element from the current position in the left direction.longx2=loc2+2;longvalueReplaced=-1;longres1=Long.MAX_VALUE,res2=Long.MAX_VALUE;if(x1>=0)//the position should ,ofcourse be >=0 to find it in the given list{//now we are calculating result using x1li.add(arr.get((int)x1));valueReplaced=arr.get((int)x1);//this variable will be used later//it stores the value that will be replaced in the original array listarr.set((int)x1,(long)0);System.out.println("li arr is:"+li+"and arr is:"+arr);res1=calcResult(li,arr);//res1 has the result when element at x1 position //is chosen}if(x2<=arr.size()-1)//x2 should evidently be <=0 to find it//in the given arrayList{System.out.println("li array is:"+li+"and original array is:"+arr);if(valueReplaced==-1){//x1 is less than 0,no changes took place//so we continue normally li.add(arr.get((int)x2));arr.set((int)x2,(long)0);res2=calcResult(li,arr);//res2 is the result obtained when we choose element at x2 in the li list}else{//element at position x1 was used to find the result//so we must restore the list to its previous stateli.remove(li.size()-1);arr.set((int)x1,valueReplaced);li.add(arr.get((int)x2));arr.set((int)x2,(long)0);res2=calcResult(li,arr);}}//end of x2 ifSystem.out.println("li list is:"+li);System.out.println("arr list is:"+arr);returnMath.min(res1,res2);//return the minimum value obtained by adding elements at position //x1 or x2 to the li list}//end of else ifreturn0;}//end of funcpublicstaticlongcalcResult(ArrayList<Long>liArr,ArrayList<Long>luArr){longsum=0;for(inti=0;i<liArr.size();i++){for(intj=0;j<luArr.size();j++){if(luArr.get(j)==0)//do not calculate for this as it has been added to //li listcontinue;elsesum+=Math.abs(liArr.get(i)-luArr.get(j));}}returnsum;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Fair Cut
You are viewing a single comment's thread. Return to all comments →
My solution based on @zh2196 's approach.It passes all the test cases.