Ice Cream Parlor – HackerRank Solution

In this post, we will solve Ice Cream Parlor HackerRank Solution. This problem (Ice Cream Parlor) is a part of HackerRank Problem Solving series.

Solution – Ice Cream Parlor – HackerRank Solution

C++

#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;
}

Python

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)

Java

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; }
                     }
                     }
                 }           
            
            }        
    }
}

Note: This problem (Ice Cream Parlor) is generated by HackerRank but the solution is provided by CodingBroz. This tutorial is only for Educational and Learning purpose.

Leave a Comment

Your email address will not be published. Required fields are marked *