In: Computer Science
Given an array of integers, and given a specific value k (not equal to 0), produce all unique pairs of values in the array which differ by k. For example, if the array has [1,4,9,12, 6, 15, 5, 13,17] and k=3, the answer would be (1,4 ) ( 9,12), ( 9,6), (12,15). If k=4, the answer would be (1,5), (9,5), (13,17), (9.13). Notice that you do not print the same answer twice, for instance (9,13), and (13,9).
#include <bits/stdc++.h>
using namespace std;
void solution(int arr[], int n, int k)
{
for (int i = 0; i < n; i++)
{
for (int j = i+1; j < n; j++)
if (arr[i] - arr[j] == k || arr[j] - arr[i] == k )
cout<<"("<<arr[i]<<","<<arr[j]<<")"<<" ";
}
}
int main()
{
int i,n,k;
cout<<"Enter size of array: ";
cin>>n;
int arr[n];
for(i=0;i<n;i++)
cin>>arr[i];
cout<<"Enter value of k: ";
cin>>k;
cout << "possible pairs: ";
solution(arr, n, k);
return 0;
}
PLEASE LIKE IT RAISE YOUR THUMBS UP
IF YOU ARE HAVING ANY DOUBT FEEL FREE TO ASK IN COMMENT
SECTION