MCPcopy Create free account
hub / github.com/Tiwarishashwat/InterviewCodes / getPairsCount

Method getPairsCount

CountPairsWithGivenum.java:3–24  ·  view source on GitHub ↗
(int[] arr, int n, int k)

Source from the content-addressed store, hash-verified

1class Solution
2{
3 int getPairsCount(int[] arr, int n, int k)
4 {
5 int counter=0;
6 HashMap<Integer,Integer> map = new HashMap<>();
7 for(int i=0;i<n;i++)
8 {
9 int x=k-arr[i];
10 if(map.containsKey(x))
11 {
12 counter=counter+map.get(x);
13 }
14 if(arr[i]<k)
15 {
16 if(map.get(arr[i])==null)
17 {
18 map.put(arr[i],0);
19 }
20 map.put(arr[i],map.get(arr[i])+1);
21 }
22 }
23 return counter;
24 }
25}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected