We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Search
  4. Ice Cream Parlor
  5. Discussions

Ice Cream Parlor

Problem
Submissions
Leaderboard
Discussions
Editorial
Topics

Sort 877 Discussions, By:

recency

Please Login in order to post a comment

  • karthikeya_vasu1
    4 days ago+ 0 comments

    def icecreamParlor(m, arr): for i in range(len(arr)): for j in range(i+1,len(arr)): if arr[i]+arr[j] == m: return [i+1,j+1]

    0|
    Permalink
  • pavanmarisetty1
    4 days ago+ 2 comments

    Two friends like to pool their money and go to the ice cream parlor. They always choose two distinct flavors and they spend all of their money.

    Given a list of prices for the flavors of ice cream, select the two that will cost all of the money they have.

    0|
    Permalink
  • andgonzalez75
    6 days ago+ 0 comments

    JavaScript O(n) Solution

    function icecreamParlor(m, arr) {
        // Write your code here
        let results = {};
        
        for(let i = 0; i <= arr.length; i++){
            if(results.hasOwnProperty(arr[i])) {
                return [results[arr[i]] + 1, i + 1]
            }
    
            results[m - arr[i]] = i;
        }
    }
    
    0|
    Permalink
  • Zamoh
    1 week ago+ 0 comments

    rust solution

    use std::collections::HashMap;
    fn icecreamParlor(m: i32, arr: &[i32]) -> Vec<i32> {
        let mut map = HashMap::new();
        arr.iter().enumerate().fold((0, vec![]), |(i, res), (j, &x)| {
            if res.is_empty() && map.contains_key(&(m - x)) {
                (i + 1, vec![map[&(m - x)], j as i32 + 1])
            } else {
                map.entry(x).or_insert(j as i32 + 1);
                (i + 1, res)
            }
        }).1
    }
    
    0|
    Permalink
  • dcd26
    4 weeks ago+ 0 comments

    Binary search

    vector<int> icecreamParlor(int m, vector<int> arr) {
        vector<int> res;
        vector<ii> v;
        for (int i = 0; i < arr.size(); i++) v.push_back(ii(arr[i], i + 1));
        sort(v.begin(), v.end());
        int rem, ind;
        for (int i = 0; i < v.size(); i++){
            rem = m - v[i].first;
            ind = lower_bound(v.begin() + i + 1, v.end(), ii(rem, 0)) - v.begin();
            if (v[ind].first == rem){
                res.push_back(v[i].second);
                res.push_back(v[ind].second);
                break;
            }
        }
        sort(res.begin(), res.end());
        return res;
    }
    
    0|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy