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.
functionicecreamParlor(m,arr){// Write your code hereletansArr=[];// Initialize an empty array to store the resultfor(leti=0;i<arr.length;i++){// Loop through each element in the arrayletcanBreak=false;// Initialize a flag to indicate if a pair is found for the current elementletflavorA=arr[i];// Get the current element as flavorAfor(letj=i+1;j<arr.length;j++){// Iterate through the remaining elements in the arrayletflavorB=arr[j];// Get the next element as flavorBif(flavorA+flavorB===m){// Check if the sum of flavorA and flavorB equals mcanBreak=true;// Set the flag to true as a pair is foundansArr.push(i+1,j+1);// Add the indices (1-based) of the pair to the ansArrbreak;// Exit the inner loop since a pair is found}}if(canBreak){// Check if a pair is foundbreak;// Exit the outer loop since a pair is found}}returnansArr;// Return the ansArr with the indices of the pair}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Ice Cream Parlor
You are viewing a single comment's thread. Return to all comments →
JavaScript solution with comments: