Function Description

Complete the function whatFlavors in the editor below. It must determine the two flavors they will purchase and print them as two space-separated integers on a line.

whatFlavors has the following parameter(s):

  • cost: an array of integers representing price for a flavor
  • money: an integer representing the amount of money they have to spend

Input Format

The first line contains an integer, , the number of trips to the ice cream parlor.

Each of the next  sets of  lines is as follows:

  • The first line contains .
  • The second line contains an integer, , the size of the array .
  • The third line contains  space-separated integers denoting the .

Constraints

Output Format

Print two space-separated integers denoting the respective indices for the two distinct flavors they choose to purchase in ascending order. Recall that each ice cream flavor has a unique ID number in the inclusive range from  to .



hackerrank has tables ice cream parlor solution


Problem solution in C++ programming language.

#include <bits/stdc++.h>
using namespace std;

class IceCream {
    
    public: 
        int flavor; 
        int index;

        IceCream(int _flavor, int _index) {
            flavor = _flavor;
            index = _index;
        }
    
};
    
int binarySearch(int first, int last, vector<IceCream> arr, int search) {
    while(first <= last ){
        int mid = ( first + last )>>1;
        if( arr[mid].flavor == search ) return arr[mid].index;
        if( arr[mid].flavor < search ){
            first = mid + 1;
        }else
            last = mid - 1;
    }
    return -1;
}

int compare(IceCream &a, IceCream &b){
    return (a.flavor < b.flavor) || (a.flavor == b.flavor && a.index < b.index);
}

int main() {
    int t;
    int n, m;
    cin >> t;
    for(int test = 0; test < t; test++) {       
        cin >> m >> n;
        vector<IceCream> arr;
        arr.reserve(n); 

        for (int i = 0; i < n; i++) {
            int cost;
            cin >> cost;
            arr.push_back(IceCream(cost, i + 1));
        }

        sort(arr.begin(), arr.end(), compare);
        int firstIndex = 100000, secondIndex = 100000;
        for(int i = 0; i < n - 1 ; i++) {
            int search = m - arr[i].flavor;
            //cout<<arr[i].flavor<<" -- "<<search<<endl;
            if(search >= arr[i].flavor) {
                int index = binarySearch( i + 1, n - 1, arr, search);
                if( index != -1 ) {
                    cout << min(arr[i].index, index) << " " << max(arr[i].index, index) << endl;
                    break;

                }
            }
        }

    }

}

Problem solution in C programming language.

#include <stdio.h>
#include <stdint.h>
#include <string.h>

#define RUN_TEST_CASES(VAR) int VAR##_total; scanf("%d", & VAR##_total); \
	for(int VAR=1; VAR<=VAR##_total; ++VAR)

int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }

int main()
{
#ifdef _DEBUG
	char FNAME[250];
	strcpy(FNAME, __FILE__);
	strcpy(strchr(FNAME, '.'), ".txt");
	freopen(FNAME, "rt", stdin);
#endif

	RUN_TEST_CASES(test_case)
	{
		int m, n;
		scanf("%d %d", &m, &n);

		int lookup[10001];
		memset(lookup, 0, sizeof lookup);

		for (int j = 1; j <= n; ++j) {
			int x;
			scanf("%d", &x);

			if (m > x)
			{
				const int other_pos = lookup[m - x];
				if (other_pos > 0)
					printf("%d %d\n", min(j, other_pos), max(j, other_pos));
			}

			lookup[x] = j;
		}
	}
}

Problem solution in Java programming language.

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        
        Scanner in=new Scanner(System.in);
        int T=in.nextInt();
        int M,N,x,y,c;
        
        
        for(int i=0;i<T;i++)
            {c=0;
            M=in.nextInt();
             N=in.nextInt();
             int ar[]=new int[N];
             for(int j=0;j<N;j++)
                 {ar[j]=in.nextInt();}
             for(int a=0;a<N;a++)
                 {if(c==1)
                     {break;}
                   for(int b=0;b<N;b++)
                     {
                      if(b!=a)
                     {
                          if((ar[a]+ar[b])==M)
                     {x=a+1;y=b+1;
                     if(x>y)
                {System.out.println(y+" "+x);}
             else {System.out.println(x+" "+y);}
                    c=1; }
                     }
                     }
                 
                 
                 }
            
            
            
            
            }
            
    }
}

Problem solution in Python programming language.

def flavors(m,a):
    prices = {}
    for idx, p in enumerate(a):
        if m-p in prices:
            return prices[m-p], idx
        prices[p] = idx
    return None

t = int(input().strip())
for a0 in range(t):
    m = int(input().strip())
    n = int(input().strip())
    a = list(map(int, input().strip().split(' ')))
    f1, f2 = flavors(m,a)
    print(f1+1, f2+1)