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.
i have a O(n) approach in java using maps.
PS: this is my first discussion :)
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;
}
}
}
}
}
Ice Cream Parlor
You are viewing a single comment's thread. Return to all comments →
i have a O(n) approach in java using maps. PS: this is my first discussion :)
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
}