Sum of distinct elements for a limited range
Basic Accuracy: 40.02% Submissions: 30 Points: 1

Given an array, arr[] of N elements such that every element of the array is an integer in the range 1 to n, the task is to find the sum of all the distinct elements of the array.


Example 1:

Input: N = 9
arr[] = {5, 1, 2, 4, 6, 7, 3, 6, 7}
Output: 28
Explanation: The distinct elements in 
the array are 1, 2, 3, 4, 5, 6, 7.

Example 2:

Input: N = 3, arr[] = {1, 1, 1}
Output: 1

Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function sumOfDistinct() that takes array arr[]  and integer N as parameters and returns the sum of a distinct elements of the array.


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


1 ≤ N ≤ 106

to report an issue on this page.


We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?


All Submissions

My Submissions:

Login to access your submissions.