Sum of length | Practice | GeeksforGeeks

Given an array. Calculate the sum of lengths of contiguous subarrays having all distinct elements.
 
Example 1:
Input:
N=3
arr[] = { 1,2,3 }
Output: 10
Explanation: 
{1, 2, 3} is a subarray of length 3 with 
distinct elements. Total length of length
three = 3. {1, 2}, {2, 3} are 2 subarray 
of length 2 with distinct elements. Total 
length of lengths two = 2 + 2 = 4
{1}, {2}, {3} are 3 subarrays of length 1
with distinct element. Total lengths of 
length one = 1 + 1 + 1 = 3
Sum of lengths = 3 + 4 + 3 = 10
Example 2:
Input:
N=1
arr[] = { 1 }
Output: 1
Explanation: 
{1} is the only subarray with distinct 
elements of length 1.  


Your Task:
You don't need to read input or print anything. Your task is to complete the function sumoflength() that takes array arr and integer N as input parameters and returns the sum of lengths of contiguous subarrays having all distinct elements.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
 
Constraints:
1 ≤ N ≤ 106
+1
sejalxz4 months ago
Here's my solution using sliding window.TC : O(N)
SC : O(N)
 
class Solution{
   public:
   int sumoflength(int arr[], int size)
   {
       int left=0, sum = 0, n =0;
       unordered_map<int,int>mem;
       
       for(int right = 0; right < size ; right++){
           int currentVal = arr[right];
           
           if(mem.find(currentVal) != mem.end()){
               int index = mem[currentVal];
               while(left <= right && left <= index){
                   int discard = arr[left];
                   mem[discard]--;
                   
                   if(mem[discard]==0)
                       mem.erase(discard);
                       
                   left++;
               }
               mem[currentVal] = right;
           }
           else{
               mem[currentVal]=right;
           }
           n = right - left + 1;
           sum += ((n*(n+1))/2);
       }
       
       return sum;
   }
}; 
0
imranwahid4 months ago
Easy C++ solution using sliding window technique
 
The basic idea is to grow the window until we are not getting a visited character and while growing consider every element as end point of a subarray and add the lengths of the subarray ending at that element.
+1
negilokesh3477 months ago
why have u m guys marked this as a easy level problem
0
Shivam Bhardwaj2 years ago
Shivam Bhardwaj
Easy c++ solutionthanks me laterhttps://ide.geeksforgeeks.o...
0
JAHANVI SINGH2 years ago
JAHANVI SINGH
https://ide.geeksforgeeks.o... Java solution
0
SHUBHAM TANDAN2 years ago
SHUBHAM TANDAN
for _ in range(int(input())):    n  = int(input())    arr = list(map(int,input().split()))
    counter = 0    for i in range(n):        h = {}        for j in range(i,n):            if arr[j] not in h:                h[arr[j]] = 1                counter += len(h)            else:                break
    print(counter)
0
Ritik Sunita Jain2 years ago
Ritik Sunita Jain
https://uploads.disquscdn.c...
O(n).  0.01 solution fastest with pre computation
0
Kamal Meena2 years ago
Kamal Meena
Python solution Using set Ex.Time: 0.02 sechttps://ide.geeksforgeeks.o...
0
Nikhil Pachkor2 years ago
Nikhil Pachkor
Can anyone suggest how to improve time complexity of the following code:for _ in range(int(input())):    n=int(input())    a=list(map(int,input().split()))    count=0    for i in range(n+1):        for j in range(i+1,n+1,1):            p=a[i:j]            if len(set(p))==len(p):                count+=len(p)    print(count)
0
ultrainstinct2 years ago
ultrainstinct
Easy python solution:
https://ide.geeksforgeeks.o...
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.