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.
my O(N) approach using maps in JAVA8
ps:this is my first discussion :)
code for same is-
import java.util.*;
class Solution
{
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
int test =scan.nextInt();
for(int l=0;l
HashMap<Integer,List<Integer>> hm=new HashMap<Integer,List<Integer>>();
for(int i=1;i<=n;i++)
{
int b=scan.nextInt();
hm.putIfAbsent(b,new ArrayList<>());
hm.get(b).add(i);
}
for(Integer key:hm.keySet())
{
int search=money-key;
if(search==key)
{
int min=Math.min(hm.get(key).get(0),hm.get(key).get(1));
int max=Math.max(hm.get(key).get(0),hm.get(key).get(1));
System.out.println(min+" "+max);
break;
}
else
{ if(hm.containsKey(search))
{
int min=Math.min(hm.get(key).get(0),hm.get(search).get(0));
int max=Math.max(hm.get(key).get(0),hm.get(search).get(0));
System.out.println(min+" "+max);
break;
}
}
}
}
}
}
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 →
my O(N) approach using maps in JAVA8 ps:this is my first discussion :)
code for same is-
import java.util.*; class Solution { public static void main(String args[]) { Scanner scan = new Scanner(System.in); int test =scan.nextInt(); for(int l=0;l
}