Obtaining a pair of elements in the array whose sum = k.
For Sorted Arrays:
voidfindPair(int a[],int n,int key) {// Set i to starting index and j to last indexint i =0, j = n-1;while (i < j) {// If pair is found, increment i and decrement jif (a[i] + a[j] == key) {printf("Pair : %d and %d\n", a[i], a[j]); i++; j--; }// If sum is greater than key, decrement jelseif (a[i] + a[j] > key) { j--; }// If sum is smaller than key, increment ielse { i++; } }}
For Unsorted Arrays:
voidfindPair(int a[],int n,int max,int key) {// Create and initialize hash array with 0int h[max];for (int i =0; i < max; i++) { h[i] =0; }for (int i =0; i < n; i++) {// Check if the suitable pair is presentif (h[key - a[i]] !=0) {printf("Pair : %d and %d\n", a[i], key-a[i]); } h[a[i]]++; }}