Skip to main content
added 10 characters in body
Source Link
p_flame
  • 112
  • 6

You can use the HashMap<K,V> where K: a[i] and V: k-a[i] This may result in an incorrect answer if there are duplicates in an array.

Say for instances:

int a[] = {4, 4, 4, 4, 4, 4, 4, 4, 4} 

where k = 8 or:

int a[] = {1, 3, 3, 3, 3, 1, 2, 1, 2} 

where k = 4.

So in order to avoid that, we can have a List<List<Integer>> , which can check each pair and see if it is already in the list.

static int numberOfPairs(int[] a, int k) { List<List<Integer>> res = new ArrayList<>(); Map<Integer, Integer> map = new HashMap<>(); for(int element:a) { List<Integer> list = new ArrayList<>(); if(map.containsKey(element)) { list.add(element); list.add(map.get(element)); if(!res.contains(list)) res.add(list); } else map.put(k - element, element);  } return res.size(); } 

You can use the HashMap<K,V> where K: a[i] and V: k-a[i] This may result in an incorrect answer if there are duplicates in an array.

Say for instances:

int a[] = {4, 4, 4, 4, 4, 4, 4, 4, 4} 

where k = 8 or:

int a[] = {1, 3, 3, 3, 3, 1, 2, 1, 2} 

where k = 4.

So in order to avoid that, we can have a List<List<Integer>> , which can check each pair and see if it is already in the list.

static int numberOfPairs(int[] a, int k) { List<List<Integer>> res = new ArrayList<>(); Map<Integer, Integer> map = new HashMap<>(); for(int element:a) { List<Integer> list = new ArrayList<>(); if(map.containsKey(element)) { list.add(element); list.add(map.get(element)); if(!res.contains(list)) res.add(list); } else map.put(k - element, element); } return res.size(); } 

You can use the HashMap<K,V> where K: a[i] and V: k-a[i] This may result in an incorrect answer if there are duplicates in an array.

Say for instances:

int a[] = {4, 4, 4, 4, 4, 4, 4, 4, 4} 

where k = 8 or:

int a[] = {1, 3, 3, 3, 3, 1, 2, 1, 2} 

where k = 4.

So in order to avoid that, we can have a List<List<Integer>> , which can check each pair and see if it is already in the list.

static int numberOfPairs(int[] a, int k) { List<List<Integer>> res = new ArrayList<>(); Map<Integer, Integer> map = new HashMap<>(); for(int element:a) { List<Integer> list = new ArrayList<>(); if(map.containsKey(element)) { list.add(element); list.add(map.get(element)); if(!res.contains(list)) res.add(list); } else map.put(k - element, element);  } return res.size(); } 

You can use the HashMap<K,V>HashMap<K,V> where K: a[i] and V: k-a[i] This may result in an incorrect answer if there are duplicates in an array.

say for eg: int a[ ] = {4, 4, 4, 4, 4, 4, 4, 4, 4} , where k = 8 or int a[ ] = {1, 3, 3, 3, 3, 1, 2, 1, 2}, where k= 4 Say for instances:

int a[] = {4, 4, 4, 4, 4, 4, 4, 4, 4} 

where k = 8 or:

int a[] = {1, 3, 3, 3, 3, 1, 2, 1, 2} 

where k = 4.

So in order to avoid that, we can have a List < List < Integer>>List<List<Integer>> , which can check each pair and see if it is already in the list.

static int numberOfPairs(int[] a, int k)  {  List<List<Integer>> res = new ArrayList<>();   Map<Integer, Integer> map = new HashMap<>(); int pairsCount = 0;  for(int i = 0; i < element:a.length; i++) { ArrayList<Integer>List<Integer> list = new ArrayList<>(); if(map.containsKey(a[i]element)) { list.add(a[i]element); list.add(map.get(a[i]element)); if(!res.contains(list)) res.add(list); } else map.put(k - a[i]element, a[i]element);     } //System.out.println(res); return res.size(); } 

You can use the HashMap<K,V> where K: a[i] and V: k-a[i] This may result in an incorrect answer if there are duplicates in an array

say for eg: int a[ ] = {4, 4, 4, 4, 4, 4, 4, 4, 4} , where k = 8 or int a[ ] = {1, 3, 3, 3, 3, 1, 2, 1, 2}, where k= 4

So in order to avoid that, we can have a List < List < Integer>> , which can check each pair and see if it is already in the list.

static int numberOfPairs(int[] a, int k)  {  List<List<Integer>> res = new ArrayList<>();   Map<Integer, Integer> map = new HashMap<>(); int pairsCount = 0;  for(int i = 0; i < a.length; i++) { ArrayList<Integer> list = new ArrayList<>(); if(map.containsKey(a[i])) { list.add(a[i]); list.add(map.get(a[i])); if(!res.contains(list)) res.add(list); } else map.put(k - a[i], a[i]);     } //System.out.println(res); return res.size(); } 

You can use the HashMap<K,V> where K: a[i] and V: k-a[i] This may result in an incorrect answer if there are duplicates in an array.

Say for instances:

int a[] = {4, 4, 4, 4, 4, 4, 4, 4, 4} 

where k = 8 or:

int a[] = {1, 3, 3, 3, 3, 1, 2, 1, 2} 

where k = 4.

So in order to avoid that, we can have a List<List<Integer>> , which can check each pair and see if it is already in the list.

static int numberOfPairs(int[] a, int k) { List<List<Integer>> res = new ArrayList<>(); Map<Integer, Integer> map = new HashMap<>(); for(int element:a) { List<Integer> list = new ArrayList<>(); if(map.containsKey(element)) { list.add(element); list.add(map.get(element)); if(!res.contains(list)) res.add(list); } else map.put(k - element, element); } return res.size(); } 
Source Link
p_flame
  • 112
  • 6

You can use the HashMap<K,V> where K: a[i] and V: k-a[i] This may result in an incorrect answer if there are duplicates in an array

say for eg: int a[ ] = {4, 4, 4, 4, 4, 4, 4, 4, 4} , where k = 8 or int a[ ] = {1, 3, 3, 3, 3, 1, 2, 1, 2}, where k= 4

So in order to avoid that, we can have a List < List < Integer>> , which can check each pair and see if it is already in the list.

static int numberOfPairs(int[] a, int k) { List<List<Integer>> res = new ArrayList<>(); Map<Integer, Integer> map = new HashMap<>(); int pairsCount = 0; for(int i = 0; i < a.length; i++) { ArrayList<Integer> list = new ArrayList<>(); if(map.containsKey(a[i])) { list.add(a[i]); list.add(map.get(a[i])); if(!res.contains(list)) res.add(list); } else map.put(k - a[i], a[i]); } //System.out.println(res); return res.size(); }