Wednesday, 7 January 2015

Given a large array of unsigned ints, quickly find two who's sum is 10

import java.util.*; public class AdditionDemo { public static void main(String[] args) { HashMap<Integer,Integer> hm=new HashMap<Integer,Integer>(); int arr[]=new int[9]; for(int i=0;i<9;i++) arr[i]=i+1; for (int i:arr) { if(hm.get(i)==null) { if(hm.get(10-i)!=null) hm.put((10-i),i); else hm.put(i,0); } } System.out.println(hm); } }

No comments:

Post a Comment