tutorialcup@gmail.com

Find all pairs with given a difference

Arrays
Reverse Array
Pair Sum Equals Number
Floor and Ceil
Largest Sum Subarray
Missing Number
Odd Occuring Number
Sort by Frequency
First and Second Smallest
Majority Element
Difference between elements
Arrange Even and Odd
Closest Sum Elements
Distinct Elements
First Repeating Number
Common Elements in Arrays
Smallest Missing Number
Max Adjacent Sum
Find Occurrences of Number
Rotate Image
Distance Between Numbers
Pair with given difference
Product Array Puzzle
Replace Elements
First Repeating Element
Move Zeroes to End
Pythagorean Triplets
Maximum Average Subarray
Subarray Sum Equal to X
Leaders in Array
Largest Pair Sum
Segregate 0s 1s and 2s
Find Duplicates
Consecutive Elements Check
Triplet with given Sum
Binary Search in Sorted Array
Fixed point in sorted array
Reorder Elements by Indexes
Merging two sorted arrays
Find next greater number
Reorder array using given indexes
Count of triplets with sum less than given value
Merge two sorted arrays
Subarray and Subsequence
Rearrange array maximum minimum form
Find the lost element from a duplicated array
Count minimum steps to get the given array
Maximum element in an array which is increasing and then decreasing
Minimum number of jumps to reach the end of an array
Subarray with given sum
Length of Longest Increasing Subsequence
Smallest positive number missing unsorted array
The Celebrity Problem
Sorted subsequence of size 3
Partition Problem
Find pair with given difference
Maximum length of chain pairs
Four elements that sum to given
Maximum circular subarray sum
Count possible triangles
Longest Increasing Subsequence
Petrol Bunks Tour
Tug of War
Counting Sort
Maximum Repeating Number
Positive Negative Arrangement
Find a Peak element
Elements More Than n/k
Max Product Subsequence
Longest Bitonic subarray in an array
Number of smaller elements on right side
Implement two stacks in an array
Maximum sum increasing subsequence
Find the two numbers with odd occurrences in an unsorted array
Largest subarray with equal number of 0's and 1's
Maximum product subarray
Replace every element with the greatest on the right side in an array
Sorting a k sorted array
Find the row with maximum number of 1's
Shuffle a given array
Iterative Implementation of quick sort
Arrange given numbers to form the biggest number
Pancake sorting
Pancake sorting Problem
Maximum Subarray Sum using Divide and Conquer
Merge Overlapping Intervals
Stock Buy Sell to Maximize Profit
Sort Elements by frequency
Print all possible combinations of r elements in a given array of size n
Monotonically increasing function
Minimum element in a sorted and rotated array
Merge k Sorted Arrays
Flip Zeroes for Consecutive 1's
Least Average Subarray
Longest span with same sum in two binary arrays
Form minimum number from given sequence of D's and I's
Number of strictly increasing subarrays
Minimum difference between any two elements in an array
Number of pairs with given sum
Make Array Palindrome
Dynamic Programming, Longest Bitonic SubSequence

Find all pair of elements in the given array with given difference.
(array without duplicates).

Example

Input array: [90, 70, 20, 80, 50, 25, 35, 15, 100, 150]
Pair of elements with difference 20 will print
90, 70 and 35, 15

Algorithm 1

Time Complexity: O(N^2)

Run two loops such that select one element from the array. Starting from the index 0, and look forward in the array to get a pair with given difference.

a)    If we find the element then print them.
b)    Else print, not found.

C++ Program

#include <bits/stdc++.h>
using namespace std;

int main()
{
	
	int arr[] = {90, 70, 20, 80, 50, 25, 35 , 15, 100, 150};//array
	int N = sizeof(arr)/sizeof(arr[0]); 
	
	int difference;
	cin>>difference; // difference required between two elements
	
	for(int i=0; i<N; i++)//select an element
	{
		for(int j=i+1; j<N; j++) // look forward in the array
			if(abs(arr[i] - arr[j]) == difference)//if we get a pair with given difference then success
				{
					cout << "Pair exists with the given difference and the elements are : " << arr[i] <<"  "<<arr[j];
					return 0;
				}
	}
	
cout <<"No such pair exists with given difference";

}
Try It

Algorithm 2:

Time Complexity: O(NlogN)

Step 1 : First sort the given array. It takes O(NlogN).

Step 2 : In the same way as the first algorithm, for every element starting from first element, find the matching pair.

i)    Here we will use binary search to find the element.
ii)    for element a and given difference n we search for a + n.
Binary search takes O(NlogN), therefore time complexity is O(NlogN).

Algorithm 2 working Example

Input array:

 

Find pair of elements with difference = 20

Apply Algorithm on the input array,

Step 1 :

Sort the array,
Sorted array:

Step 2 :

a = 15, difference (n) = 20
so, a + n = 35,
binary_search(35, input array) = True.
Print 15, 35. Move forward

Step 3 :
a = 20, difference (n) = 20
so, a + n = 40,
binary_search(40, input array) = False.
Move forward

Step 4 :
a = 35, difference (n) = 20
so, a + n = 55,
binary_search(55, input array) = False.
Move forward

Step 5 :
a = 70, difference (n) = 20
so, a + n = 90,
binary_search(90, input array) = True.
Print 70, 90. Move forward

Step 6 :
a = 90, difference (n) = 20
so, a + n = 110,
binary_search(110, input array) = False.
End Here.
Therefore, for the given input it prints:
70, 90 and 15, 35.

C++ Program

#include <bits/stdc++.h>
using namespace std;

int main()
{
	
	int arr[] = {90, 70, 20, 80, 50, 25, 35 , 15, 100, 150};//array
	int N = sizeof(arr)/sizeof(arr[0]); 
	
	int difference;
	cin>>difference; // difference required between two elements
	
	sort(arr,arr+N); //sort the array in ascending order
	
	for(int i = 0; i < N; i++)
	{
		if(binary_search(arr,arr+N,abs(difference + arr[i]))) // if b - a = X and both b and a exists in array so simply we can search for X + a to obtain b.
		{
			cout<<"Pair exists\n";
			return 0;
		}
	}
	
cout <<"No such pair exists with given difference";

}
Try It


C++ Online Test - Check Your Rank across the World


Next > < Prev
Scroll to Top