Shop In Candy Store Geeks For Geeks
Problem Link: https://practice.geeksforgeeks.org/problems/shop-in-candy-store1145/1# (It is also the problem of the day for 28-04-2022) Solution: Using Arrays.sort and Without additional arraylist: class Solution{ static ArrayList<Integer> candyStore(int arr[],int n,int k){ ArrayList<Integer>ab=new ArrayList<Integer>(); Arrays.sort(arr); /*for(int i=0;i<arr.length;i++){ System.out.print(arr[i]+" "); } */ int start=0; int end=arr.length-1; int cost=0; while(start<=end){ cost=cost+arr[start]; start++; end=end-k; } //System.out.println(cost); start=arr.length-1; end=0; int cost2=0; while(end<=start){ cost2=cost2+arr[start]; //System.out.println(cost2); end+=k; start--; } // System.out.println(cos