Find pair of elements in array that sum is given number k

Simple C program to find pair in array. Simple method is run two for loop and find pair.
For example if array is arr[] = {2,6,1,8,3,6} and number is 14 then using two for loop check each element with other elements.

for element arr[0] => 2+6 = 8; 2+1 = 3; 2+8 = 10; 2+3 = 5; 2+6 = 8; Not found.
for element arr[1] => 6+1 = 7; 6+8 = 14 (found).; 6+3 = 9; 6+6 = 12;
for element arr[2] => 1+8 = 9; 1+3 = 4; 1+6 = 7; Not found.
and check for all elements.

When when pair found the print it.

C Program:

#include<stdio.h>
 
void FindPair(int arr[], int size, int num)
{
 int i,j;
 /* If pair is not in arrray then flag = 0 */
 /* Otherwise update it to 1 */
 
 int flag=0;
 
 for(i = 0;i<size-1;i++)
 
 {
  for(j=i+1;j<size;j++)
  {
   if(arr[i]+arr[j]==num)
   {
    printf("Pair is ( %d , %d) \n",arr[i],arr[j]);
  /* Update flag to 1 if pair is in array */  
    flag=1;
   }
  }
 }
 /* In case pair is not in array then print this statement */
 if(!flag)
 {
  printf("Pair is not in array \n");
 }
}
 
int main()
{
 int arr[]={30,8,19,9,12,32,17,20};
 
 int size=sizeof(arr)/sizeof(arr[0]);
 
 int num = 62;
 
 FindPair(arr,size,num);
 
 return 0;
}

Time complexity of this program O(n^2) because of two for loop.

Another method using hash table

Initialize a hash table to zero  T[] = {0,0,0.....0}. And then check pair.

#include<stdio.h>
 
void FindPair(int arr[],int size, int k)
{
 /* initialize all elements to 0 */
 int T[100000]={0};
 
 int i,flag=0;
 for(i=0;i<size;i++)
 {
  int temp=k-arr[i];
  if(temp>=0&&T[temp]==1)
  {
   printf("Pair is ( %d, %d )",temp,arr[i]);
   flag=1;
  }
 
  T[arr[i]]=1;
 }
 
 if(!flag)
 {
  printf("Pair is not in array \n");
 }
}
 
int main()
{
 int arr[]={12,23,21,8,34,18};
 
 int size = sizeof(arr)/sizeof(arr[0]);
 
 int num = 29;
 
 FindPair(arr,size,num);
 
 return 0;
}

Time complexity of this program is O(n) and space complexity is O(m). Here m is the size of hash table.
This program will work fine if largest element in array is less than size of hash table.

If there is negative element in array then find smallest element in array and then add its absolute value in all array elements. By adding absolute value of smallest elements, we are making all elements positive.

For example if array is {-12,-23,-28,8,0,18} then smallest element is -28 and its absolute value is 28.
Now add 28 in all element so array is

arr[] = {16, 5, 0, 36, 28, 46}; and if are searching pair for -15 then number will become  2*abs(min)+num.

Because we are adding abs(min) in both elements  so -15 will become 2*28-15 = 1.

In original array, pair is (-23, 8) and in modified array, pair is ( 5, 36 ).

C Program:

#include<stdio.h>
 
//function for absolute value
int abs(int n)
{
 return n>0?n:-n;
}
 
//function to find smallest element in array
int MinElement(int arr[], int size)
{
 int i, min=arr[0];
 
 for(i=0;i<size;i++)
 {
  if(arr[i]<min)
  min=arr[i];
 }
 
 return min;
}
 
//Function to add minimun(abs value) to all elements
void AddMin(int arr[], int size, int min)
{
 int i;
 for(i=0;i<size;i++)
 {
  arr[i] = arr[i] + min;
 }
}
 
void FindPair(int arr[],int size, int k,int min)
{
 /* initialize all elements to 0 */
 int T[100000]={0};
 
 int i,flag=0;
 
 for(i=0;i<size;i++)
 {
  int temp=k-arr[i];
 
  if(temp>=0&&T[temp]==1)
  {
   printf("Pair is ( %d, %d )",temp-min,arr[i]-min);
   flag=1;
  }
 
  T[arr[i]]=1;
 }
 
 if(!flag)
 {
  printf("Pair is not in array \n");
 }
}
 
int main()
{
 int arr[]={-12,-23,-28,28,-10,180};
 
 int size = sizeof(arr)/sizeof(arr[0]);
 
 int num = 18;
 
 int min  = MinElement(arr,size);
 
// num=abs(num);
 
 min=abs(min);
 // see the explaination for this
 num = 2*min + num;
 
 AddMin(arr,size,min);
 
 FindPair(arr,size,num,min);
 
 return 0;
}

Please comment if you find anything wrong.


Popular posts from this blog