Tuesday, June 6, 2017

K diff pairs in an Array

Problem Statement

Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array. Here a k-diff pair is defined as an integer pair (i, j), where i and j are both numbers in the array and their absolute difference is k.

Link to GitHubCode



Example 1:
Input: [3, 1, 4, 1, 5], k = 2
Output: 2
Explanation: There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of 
unique pairs.

Example 2:
Input:[1, 2, 3, 4, 5], k = 1
Output: 4
Explanation: There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) 
and (4, 5).

Example 3:
Input: [1, 3, 1, 5, 4], k = 0
Output: 1
Explanation: There is one 0-diff pair in the array, (1, 1).

Example 2:
Input:[1, 2, 3, 4, 5], k = 1
Output: 4
Explanation: There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) 
and (4, 5).

Example 3:
Input: [1, 3, 1, 5, 4], k = 0
Output: 1
Explanation: There is one 0-diff pair in the array, (1, 1).


Solution


1. We need to insert all the elements of the array to HashTable. While inserting we need to make sure that you check for duplication
2. We need to check the no of pairs in hash table by adding k value to the Hash table entries
3 . This condition map.put(i, map.getOrDefault(i,0)+1) would make sure that if there is a duplicate then 
      it would replace however it would increment the coutn
      [3, 1, 4, 1, 5]   .  Here it would (1,2) 
3. If K=0 then we need to increment the counter if the value pair is greater than 2

Example for checking if the K pair exists in the hash-map
 when K=2 
 keyValue + K 

1+2 =3 . It exists
package Algorithms;
import java.util.Map;
import java.util.HashMap;
public class KdiffPairsInArray {

 public static int get(int [] nums,int k)
 {
  if(nums== null || nums.length==0 || k<0 ) return 0;
  
  //An object that maps keys to values. A map cannot contain duplicate keys; 
  //each key can map to at most one value.
   Map<Integer, Integer> map = new HashMap<>();
  
   //Insert all the contents of the array to hash table.
   for(int i:nums)
   {
    //getOrDefault used to return a default value whenever the value was not found 
    //using the key specified in the hashkey. If there is a duplicate it would replace 
    //it with new value
//    map.put(i, map.getOrDefault(i,0)+1);
   }
   int count=0;
   //Count the no of pairs in mapping table
   for(Map.Entry<Integer, Integer> entry :map.entrySet())
   {
    //If K=0
    /*
     * Input: [1, 3, 1, 5, 4], k = 0
    Output: 1
    Explanation: There is one 0-diff pair in the array, (1, 1)
     * 
     */
    if(k==0){
    if(entry.getValue() >=2)
    {
     count++;
    }
     
    }
    else
    {
     //Check if the K pair exists in the hash-map
     /*
      * ex" when K=2 
      * keyValue + K 
      * 1+2 =3 . It exists
      */
     if(map.containsKey(entry.getKey()+k))
      count++;
  
    }
   }
   return count;
  
 }
}
   

No comments:

Post a Comment