Given an array, count number of pairs that can be formed from all possible contiguous sub-arrays containing distinct numbers. The array contains positive numbers between 0 to n-1 where n is the size of the array.
Input: [1, 4, 2, 4, 3, 2] Output: 8 The subarrays with distinct elements are [1, 4, 2], [2, 4, 3] and [4, 3, 2]. There are 8 pairs that can be formed from above array. (1, 4), (1, 2), (4, 2), (2, 4), (2, 3), (4, 3), (4, 2), (3, 2) Input: [1, 2, 2, 3] Output: 2 There are 2 pairs that can be formed from above array. (1, 2), (2, 3)
The idea is to use Sliding Window for the given array. Let us use a window covering from index left to index right and an Boolean array visited to mark elements in current window. The window invariant is that all elements inside the window are distinct. We keep on expanding the window to the right and if a duplicate is found, we shrink the window from left till all elements are distinct again. We update the count of pairs in current window along the way. An observation showed that in an expanding window, number of pairs can be incremented by value equal to window size – 1. For example,
Expanding Window Count  Count = 0 [1, 2] Count += 1 pair i.e. (1, 2) [1, 2, 3] Count += 2 pairs i.e. (1, 3) and (2, 3) [1, 2, 3, 4] Count += 3 pairs i.e. (1, 4), (2, 4) and (3, 4)
So, total Count for above window of size 4 containing distinct elements is 6. Nothing need to be done when the window is shrinking.
Below is the implementation of the idea.
Time Complexity: The complexity might look O(n^2) as 2 while loop are involved but note that left and right index are changing from 0 to N-1. So overall complexity is O(n + n) = O(n). Auxiliary space required in above solution is O(n) as we are using visited array to mark elements of the current window.
This article is contributed by Aditya Goel. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count of distinct possible pairs such that the element from A is greater than the element from B
- Count all distinct pairs of repeating elements from the array for every array element
- Check if an Array is made up of Subarrays of continuous repetitions of every distinct element
- Split array into maximum subarrays such that every distinct element lies in a single subarray
- Count of distinct XORs formed by rearranging two Binary strings
- Count subarrays having total distinct elements same as original array
- Count distinct Bitwise OR of all Subarrays
- Count of subarrays having exactly K distinct elements
- Queries to print count of distinct array elements after replacing element at index P by a given element
- Differences between number of increasing subarrays and decreasing subarrays in k sized windows
- Longest subsequence such that every element in the subsequence is formed by multiplying previous element with a prime
- Array formed from difference of each element from the largest element in the given array
- Find Array formed by adding each element of given array with largest element in new array to its left
- Count of pairs from arrays A and B such that element in A is greater than element in B at that index
- Print all distinct integers that can be formed by K numbers from a given array of N numbers
- Length of array pair formed where one contains all distinct elements and other all same elements
- Count all distinct pairs with difference equal to k
- Count number of distinct pairs whose sum exists in the given array
- Count distinct pairs from two arrays having same sum of digits
- Count of distinct Strings possible by swapping prefixes of pairs of Strings from the Array