In this HackerRank interview Hash Tables: Ice Cream Parlor kit problem You need to Complete the function whatFlavors in the editor.


HackerRank Hash Tables: Ice Cream Parlor solution


Problem solution in Python programming.

#!/bin/python3

import math
import os
import random
import re
import sys
from collections import Counter

# Complete the whatFlavors function below.
def whatFlavors(cost, money):
   
    d=Counter(cost)
       
    for sunny in cost:
        johnny=money-sunny
        if sunny!=johnny:
            if d[johnny]>0:
                j=cost.index(johnny)
                print(cost.index(sunny)+1,j+1)
                return
        else:
            if d[johnny]>1:
                j=cost[cost.index(johnny)+1:].index(johnny)
                print(cost.index(sunny)+1,j+2+cost.index(sunny))
                return

if __name__ == '__main__':
    t = int(input())

    for t_itr in range(t):
        money = int(input())

        n = int(input())

        cost = list(map(int, input().rstrip().split()))

        whatFlavors(cost, money)



Problem solution in Java Programming.

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

public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        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 C++ programming.

#include <stdio.h>
#include <vector>
#include <algorithm>
#define NOT_FOUND -1
using namespace std;

struct ice_cream {
    int cost;
    int idx;

    ice_cream() {}
    ice_cream(int cost, int idx):cost(cost), idx(idx) {}
};

bool operator <(const ice_cream &a, const ice_cream &b) {
    return a.cost < b.cost || (a.cost == b.cost && a.idx < b.idx);
}

int search(int l, int r, int need, vector <ice_cream> &v) {
    while(l <= r) {
        int mid = (l + r) / 2;
        if (v[mid].cost == need) {
            return v[mid].idx;
        } else if (v[mid].cost < need) {
            l = mid + 1;
        } else {
            r = mid - 1;
        }
    }
    return NOT_FOUND;
}

int main(void) {
    int t;
    int m, n;
    int cost;
    vector <ice_cream> v;

    scanf(" %d", &t);
    for (int test = 0; test < t; test++) {
        scanf(" %d", &m);
        scanf(" %d", &n);
        v.clear();
        for (int i = 0; i < n; i++) {
            scanf(" %d", &cost);
            v.push_back(ice_cream(cost, i + 1));
        }
        sort(v.begin(), v.end());
        for (int i = 0; i < (int)v.size(); i++) {
            int cost = v[i].cost;
            int need = m - cost;
            int idx = search(i + 1, (int)v.size() - 1, need, v);
            if (idx != NOT_FOUND) {
                printf("%d %d\n", min(v[i].idx, idx), max(v[i].idx, idx));
                break;
            }
        }
    }
    return 0;
}


Problem solution in C programming.

#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 JavaScript programming.

process.stdin.resume();
process.stdin.setEncoding('ascii');

var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;

process.stdin.on('data', function (data) {
    input_stdin += data;
});

process.stdin.on('end', function () {
    input_stdin_array = input_stdin.split("\n");
    main();    
});

function readLine() {
    return input_stdin_array[input_currentline++];
}

/////////////// ignore above this line ////////////////////

function main() {
    var t = parseInt(readLine());
    for(var a0 = 0; a0 < t; a0++){
        var m = parseInt(readLine());
        var n = parseInt(readLine());
        a = readLine().split(' ');
        a = a.map(Number);
        
        var hash = {};
        var indecies;
        for (var i = 0; i < a.length; i++){
            if (typeof hash[a[i]] === 'undefined') {
                hash[a[i]] = [];
            }
            hash[a[i]].push(i+1);
        }
        
        for (var j = 0; j < a.length; j++){
            var complement = m - a[j];
            if (typeof hash[complement] !== 'undefined'){
                var cI = -1;
                for (var h = 0; h < hash[complement].length; h++){
                    if (hash[complement][h] !== (j+1)) {
                        cI = hash[complement][h];
                        break;
                    }
                }
                if (cI > -1) {
                    indecies = [Math.min(cI, j+1), Math.max(cI, j+1)];
                    break;
                }
            }
        }
        console.log(indecies[0], indecies[1]);
    }

}