Header Ad

HackerRank Ice Cream Parlor problem solution

In this HackerRank Ice Cream Parlor problem solution, we have given a list of prices for the flavors of ice cream, select the two that will cost all of the money they have.

HackerRank Ice Cream Parlor problem solution


Problem solution in Python.

for i in range(int(input())):
    target = int(input())
    loop = int(input())
    costs = input().split(" ")
    cur = 0
    done = False
    for j in range(loop - 1):
        for k in range(j+1, loop):
            if(int(costs[j]) + int(costs[k]) == target):
                print(str(j+1) + " " + str(k+1))
                done = True
                break;
        if(done): break

{"mode":"full","isActive":false}


Problem solution in Java.

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

public class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while(t-- != 0){
            int m = sc.nextInt();
            int n = sc.nextInt();
            int[] np = new int[n];
            Map<Integer, Integer> map = new HashMap<>();
            for(int i = 0; i < n ; i++){
                np[i] = sc.nextInt();
                if(map.containsKey(np[i]) == false)
                    map.put(np[i], i+1);
            }
            
            Arrays.sort(np);
            
            int s = 0;
            int e = n - 1;
            while(s < e){
                if(np[s] + np[e] > m){
                    e--;
                } else if (np[s] + np[e] < m){
                    s++;
                } else {
                    int i1 = map.get(np[s]);
                    int i2 = map.get(np[e]);
                    if(np[s] == np[e])
                        i2++;
                    
                    System.out.printf("%d %d%n", Math.min(i1, i2), Math.max(i1, i2));
                    e--;
                    s++;
                }
            }
        }
    }
}

{"mode":"full","isActive":false}


Problem solution in C++.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    
    int tc, i, k, flag, x, y, sum, C, L, Li[10009];
    scanf("%d", &tc);
    while( tc-- )
    {
        flag=0;
        scanf("%d %d %d", &C, &L, &Li[0]);
        for(i=1; i<L; i++)
        {
            scanf("%d", &Li[i]);
            sum=Li[0]+Li[i];
            if(sum==C)
            {
                flag=1;
                y=i+1;
            }
        }
        if(flag) printf("1 %d\n", y);
        else
        {
            for(i=1; i<L; i++)
            {
                for(k=i+1; k<L; k++)
                {
                    sum=Li[i]+Li[k];
                    if(sum==C)
                    {
                        flag=1;
                        x=i+1;
                        y=k+1;
                        break;
                    }
                }
                if(flag) break;
            }
            printf("%d %d\n", x, y);
        }

    }
    return 0;
}

{"mode":"full","isActive":false}


Problem solution in C.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */  
    int n,m;
    scanf("%d",&n);
    int i=0;
    for(;i<n;i++)
    {
        int c ;
        scanf("%d",&c);
        scanf("%d",&m);
        int data[m];
        int j=0;
        for(;j<m;j++)
        {
            scanf("%d",&data[j]);
            //printf("%d ",data[j]);
        }
        int k=0;
        for(j=0;j<m;j++)
        {
            for(k=j+1;k<m;k++)
            {
                if(data[j]+data[k]==c)
                {
                    printf("%d %d\n",j+1,k+1);
                    goto flag;
                }
            }
        }
       flag:;
    }
    return 0;
}

{"mode":"full","isActive":false}


Post a Comment

0 Comments