Ice Cream Parlor

  • + 5 comments

    Here is a Java solutions that runs in O(n)

    public static void main(String[] args) {
    
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
    
        for(int i = 0; i < n; i++){
    
            int cost = scan.nextInt();
            int size = scan.nextInt();
            int prices[] =  new int[size];
    
            for(int j = 0; j < size; j++){
                prices[j] = scan.nextInt();
            }
    
            determineFlavors(prices,cost);
    
        }
    
    }
    
    public static void determineFlavors(int prices[], int maxCost){
    
        HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
    
        map.put(prices[0],1);
    
        for(int i = 1; i < prices.length; i++){
    
            Integer k = map.get(maxCost - prices[i]);
    
            if(k == null){
                map.put(prices[i], i+1);
            }else{
                System.out.println(k + " " + (i+1));
                break;    
            }
    
        }
    
    }