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.
- Prepare
- Algorithms
- Search
- Ice Cream Parlor
- Discussions
Ice Cream Parlor
Ice Cream Parlor
+ 0 comments Here is my c++ solution, you can watch the explanation here : https://youtu.be/WvLuA-LCsb0
vector<int> icecreamParlor(int m, vector<int> arr) { map<int, int> mp; for (int i = 0; i < arr.size(); i++) { int complement = m - arr[i]; if (mp.count(complement)) { return {mp[complement] + 1, i + 1}; } mp[arr[i]] = i; } return {}; }
+ 0 comments vector<int> icecreamParlor(int m, vector<int> arr) { int n=arr.size(); vector<int> index; for(int i=0; i<n-1; i++){ for(int j=i+1; j<n; j++){ if(arr[i]+arr[j]==m){ index.push_back(i+1); index.push_back(j+1); return index; } } } return index; }
+ 0 comments # ThinhNguyen97 """ I Miss Those Moments I Spent With You Kitchen Table Mirror Sofa """ # Python is scripting language use interpreter # import numpy as np # import matplotlib.pyplot as plt # import pandas as pd from math import * from builtins import staticmethod from collections import Counter from collections import defaultdict from collections import namedtuple from collections import deque from queue import LifoQueue import heapq import functools import hashlib from datetime import datetime, timedelta import json import re from itertools import * import queue from bisect import bisect_left def solve(m, cost): # Create a dictionary to store the indices of flavors based on their prices flavor_indices = {} # Iterate through the list of costs for i in range(len(cost)): # Calculate the remaining amount of money after purchasing the current flavor remaining_money = m - cost[i] # Check if the remaining money corresponds to the price of any previously visited flavor if remaining_money in flavor_indices: # If found, return the indices of the two flavors (1-based indexing) return [flavor_indices[remaining_money] + 1, i + 1] else: # If not found, store the current flavor's index in the dictionary flavor_indices[cost[i]] = i def main(): t = int(input().strip()) for _ in range(t): m = int(input().strip()) n = int(input().strip()) arr = list(map(int, input().rstrip().split())) result = solve(m, arr) print(result[0], result[1]) if __name__ == '__main__': main()
+ 0 comments JavaScript:
function icecreamParlor(m, arr) { for (let i = 0; i < arr.length; i++) { let balance = m - arr[i]; let find = arr.findIndex((num, index) => { return i != index && num === balance }) if(find != -1) { return [i + 1, find + 1] } } }
+ 0 comments Java solution
public static List<Integer> icecreamParlor(int m, List<Integer> arr) { List<Integer> result = new ArrayList<>(); for (int i = 0; i < arr.size()-1; i++) { if(arr.get(i) >= m) continue; for (int j = i+1; j < arr.size(); j++) { int sum = arr.get(i) + arr.get(j); if(sum == m){ result.add(i+1); result.add(j+1); break; } } } return result; }
Load more conversations
Sort 914 Discussions, By:
Please Login in order to post a comment