Hash Tables: Ice Cream Parlor

  • + 0 comments

    JAVA Solution:

        int[] arr=cost.stream().mapToInt(Integer::intValue).toArray();
    Arrays.sort(arr);
    int start=0;
    int end=arr.length-1;
    while(start<end){
        if(arr[start]+arr[end]>money){
            end--;
        }
        else if(arr[start]+arr[end]<money){
            start++;
        }
        else{
            TreeSet<Integer> res=new TreeSet<>();
            res.add(cost.indexOf(arr[start])+1);
            int pos2=cost.indexOf(arr[end])+1;;
            if(arr[start]==arr[end]){
                cost.remove(cost.indexOf(arr[start]));
            pos2=cost.indexOf(arr[end])+2;
            }
            res.add(pos2);
    
    
            System.out.print(res.first()+" "+res.last());
            System.out.println();
            break;
        }
    }