Timer is Running

Sub-Array Pairs
Submissions: 0   Accuracy:

0%

  Difficulty: Easy   Marks: 2

Given an array of size N. Count the total number of distinct pairs that can be formed from its contiguous subarrays consisting of distinct elements only.

Example 1:

Input:
N = 6
Arr[] = {1, 4, 2, 4, 3, 2}
Output: 8
Explanation: The subarrays with only distinct 
elements are [1, 4, 2], [2, 4, 3] and [4, 3, 2].
From these, following pairs can be formed
(1, 4), (1, 2), (4, 2), (2, 4), (2, 3),
(4, 3), (4, 2), (3, 2)


Example 2:

Input:
N = 4
Arr[] = {1, 2, 2, 3}
Output: 5
Explanation: There are 2 pairs that can 
be formed from above array.
(1, 2), (2, 3)


Your Task:
You don't need to read input or print anything. Your task is to complete the function countPairs() which takes arr[] and as input parameters and returns the number of possible pairs.


Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)

Constraints:
1 <= N <= 105
0<= Arr[i] <= N - 1

 

** For More Input/Output Examples Use 'Expected Output' option **

Author: Rohan Malhotra


If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.



Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.


to report an issue on this page.