You are viewing a single comment's thread. Return to all comments →
Have you solved it with O(N)?
I thought my code below was O(N) but apparently not because it fails 4 test cases :
static int pairs(int k, int[] arr) { // Complete this function Dictionary dict = new Dictionary(); int count =0; int k1; for(int i=0; i k) { k1 = arr[i]-k; //{(5,0),} } else { k1 = arr[i]; //3, 1 }
if(dict.ContainsKey(k1) || dict.ContainsKey(arr[i])) { if(dict.ContainsKey(k1)) { dict[k1]++; count++; } else { dict.Add(k1,0); //{(1,0),(1,1),(4,0)}, {(1,1),(3,1),(2,1)} } if(dict.ContainsKey(arr[i])&&arr[i] != k1) { dict[arr[i]]++; count++; } else if(arr[i] != k) { dict.Add(arr[i],0); } } else { dict.Add(arr[i],0); if(arr[i] != k1) { dict.Add(k1,0); } } } return(count); }
Pairs
You are viewing a single comment's thread. Return to all comments →
Have you solved it with O(N)?
I thought my code below was O(N) but apparently not because it fails 4 test cases :
static int pairs(int k, int[] arr) { // Complete this function Dictionary dict = new Dictionary(); int count =0; int k1; for(int i=0; i k) { k1 = arr[i]-k; //{(5,0),} } else { k1 = arr[i]; //3, 1 }