# Subarrays with distinct elements

Given an array, the task is to calculate the sum of lengths of contiguous subarrays having all elements distinct.

**Examples:**

Input :arr[] = {1, 2, 3}Output :10 {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 = 10Input :arr[] = {1, 2, 1}Output :7Input :arr[] = {1, 2, 3, 4}Output :20

A **simple solution** is to consider all subarrays and for every subarray check if it has distinct elements or not using hashing. And add lengths of all subarrays having distinct elements. If we use hashing to find distinct elements, then this approach takes O(n^{2}) time under the assumption that hashing search and insert operations take O(1) time.

An **efficient solution** is based on the fact that if we know all elements in a subarray arr[i..j] are distinct, sum of all lengths of distinct element subarrays in this sub array is ((j-i+1)*(j-i+2))/2. How? the possible lengths of subarrays are 1, 2, 3,……, j – i +1. So, the sum will be ((j – i +1)*(j – i +2))/2.

We first find largest subarray (with distinct elements) starting from first element. We count sum of lengths in this subarray using above formula. For finding next subarray of the distinct element, we increment starting point, i and ending point, j unless (i+1, j) are distinct. If not possible, then we increment i again and move forward the same way.

Below is the implementation of this approach:

## C++

`// C++ program to calculate sum of lengths of subarrays` `// of distinct elements.` `#include<bits/stdc++.h>` `using` `namespace` `std;` `// Returns sum of lengths of all subarrays with distinct` `// elements.` `int` `sumoflength(` `int` `arr[], ` `int` `n)` `{` ` ` `// For maintaining distinct elements.` ` ` `unordered_set<` `int` `> s;` ` ` `// Initialize ending point and result` ` ` `int` `j = 0, ans = 0;` ` ` `// Fix starting point` ` ` `for` `(` `int` `i=0; i<n; i++)` ` ` `{` ` ` `// Find ending point for current subarray with` ` ` `// distinct elements.` ` ` `while` `(j < n && s.find(arr[j]) == s.end())` ` ` `{` ` ` `s.insert(arr[j]);` ` ` `j++;` ` ` `}` ` ` `// Calculating and adding all possible length` ` ` `// subarrays in arr[i..j]` ` ` `ans += ((j - i) * (j - i + 1))/2;` ` ` `// Remove arr[i] as we pick new starting point` ` ` `// from next` ` ` `s.erase(arr[i]);` ` ` `}` ` ` `return` `ans;` `}` `// Driven Code` `int` `main()` `{` ` ` `int` `arr[] = {1, 2, 3, 4};` ` ` `int` `n = ` `sizeof` `(arr)/` `sizeof` `(arr[0]);` ` ` `cout << sumoflength(arr, n) << endl;` ` ` `return` `0;` `}` |

## Java

`// Java program to calculate sum of lengths of subarrays ` `// of distinct elements. ` `import` `java.util.*;` `class` `geeks` `{` ` ` `// Returns sum of lengths of all subarrays` ` ` `// with distinct elements.` ` ` `public` `static` `int` `sumoflength(` `int` `[] arr, ` `int` `n) ` ` ` `{` ` ` `// For maintaining distinct elements.` ` ` `Set<Integer> s = ` `new` `HashSet<>();` ` ` `// Initialize ending point and result` ` ` `int` `j = ` `0` `, ans = ` `0` `;` ` ` `// Fix starting point` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++)` ` ` `{` ` ` `while` `(j < n && !s.contains(arr[j]))` ` ` `{` ` ` `s.add(arr[j]);` ` ` `j++;` ` ` `}` ` ` `// Calculating and adding all possible length` ` ` `// subarrays in arr[i..j]` ` ` `ans += ((j - i) * (j - i + ` `1` `)) / ` `2` `;` ` ` `// Remove arr[i] as we pick new starting point` ` ` `// from next` ` ` `s.remove(arr[i]);` ` ` `}` ` ` `return` `ans;` ` ` `}` ` ` `// Driver Code` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{` ` ` `int` `[] arr = { ` `1` `, ` `2` `, ` `3` `, ` `4` `};` ` ` `int` `n = arr.length;` ` ` `System.out.println(sumoflength(arr, n));` ` ` `}` `}` `// This code is contributed by` `// sanjeev2552` |

## Python 3

`# Python 3 program to calculate sum of ` `# lengths of subarrays of distinct elements.` `# Returns sum of lengths of all subarrays ` `# with distinct elements.` `def` `sumoflength(arr, n):` ` ` `# For maintaining distinct elements.` ` ` `s ` `=` `[]` ` ` `# Initialize ending point and result` ` ` `j ` `=` `0` ` ` `ans ` `=` `0` ` ` `# Fix starting point` ` ` `for` `i ` `in` `range` `(n):` ` ` ` ` `# Find ending point for current ` ` ` `# subarray with distinct elements.` ` ` `while` `(j < n ` `and` `(arr[j] ` `not` `in` `s)):` ` ` `s.append(arr[j])` ` ` `j ` `+` `=` `1` ` ` `# Calculating and adding all possible ` ` ` `# length subarrays in arr[i..j]` ` ` `ans ` `+` `=` `((j ` `-` `i) ` `*` `(j ` `-` `i ` `+` `1` `)) ` `/` `/` `2` ` ` `# Remove arr[i] as we pick new ` ` ` `# starting point from next` ` ` `s.remove(arr[i])` ` ` `return` `ans` `# Driven Code` `if` `__name__` `=` `=` `"__main__"` `:` ` ` ` ` `arr ` `=` `[` `1` `, ` `2` `, ` `3` `, ` `4` `]` ` ` `n ` `=` `len` `(arr)` ` ` `print` `(sumoflength(arr, n))` `# This code is contributed by ita_c` |

## C#

`// C# program to calculate sum of lengths of subarrays ` `// of distinct elements` `using` `System;` `using` `System.Collections.Generic;` ` ` `public` `class` `geeks` `{` ` ` `// Returns sum of lengths of all subarrays` ` ` `// with distinct elements.` ` ` `public` `static` `int` `sumoflength(` `int` `[] arr, ` `int` `n) ` ` ` `{` ` ` `// For maintaining distinct elements.` ` ` `HashSet<` `int` `> s = ` `new` `HashSet<` `int` `>();` ` ` `// Initialize ending point and result` ` ` `int` `j = 0, ans = 0;` ` ` `// Fix starting point` ` ` `for` `(` `int` `i = 0; i < n; i++)` ` ` `{` ` ` `while` `(j < n && !s.Contains(arr[j]))` ` ` `{` ` ` `s.Add(arr[i]);` ` ` `j++;` ` ` `}` ` ` `// Calculating and adding all possible length` ` ` `// subarrays in arr[i..j]` ` ` `ans += ((j - i) * (j - i + 1)) / 2;` ` ` `// Remove arr[i] as we pick new starting point` ` ` `// from next` ` ` `s.Remove(arr[i]);` ` ` `}` ` ` `return` `ans;` ` ` `}` ` ` `// Driver Code` ` ` `public` `static` `void` `Main(String[] args) ` ` ` `{` ` ` `int` `[] arr = { 1, 2, 3, 4 };` ` ` `int` `n = arr.Length;` ` ` `Console.WriteLine(sumoflength(arr, n));` ` ` `}` `}` `/* This code is contributed by PrinciRaj1992 */` |

## Javascript

`<script>` `// Javascript program to calculate sum of lengths of subarrays ` `// of distinct elements. ` ` ` `// Returns sum of lengths of all subarrays` ` ` `// with distinct elements.` ` ` `function` `sumoflength(arr,n)` ` ` `{` ` ` `// For maintaining distinct elements.` ` ` `let s=` `new` `Set();` ` ` `// Initialize ending point and result` ` ` `let j = 0, ans = 0;` ` ` ` ` `// Fix starting point` ` ` `for` `(let i = 0; i < n; i++)` ` ` `{` ` ` `while` `(j < n && !s.has(arr[j]))` ` ` `{` ` ` `s.add(arr[i]);` ` ` `j++;` ` ` `}` ` ` ` ` `// Calculating and adding all possible length` ` ` `// subarrays in arr[i..j]` ` ` `ans += Math.floor(((j - i) * (j - i + 1)) / 2);` ` ` ` ` `// Remove arr[i] as we pick new starting point` ` ` `// from next` ` ` `s.` `delete` `(arr[i]);` ` ` `}` ` ` ` ` `return` `ans;` ` ` `}` ` ` ` ` `// Driver Code` ` ` `let arr=[1, 2, 3, 4];` ` ` `let n = arr.length;` ` ` `document.write(sumoflength(arr, n));` ` ` ` ` `// This code is contributed by avanitrachhadiya2155` ` ` `</script>` |

**Output:**

20

**Time Complexity** of this solution is **O(n). **Note that the inner loop runs n times in total as j goes from 0 to n across all outer loops. So we do O(2n) operations which is same as O(n).**Space Complexity: O(n), since n extra space has been added**

This article is contributed by **Anuj Chauhan(anuj0503)**. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.