Sum of distinct elements when elements are in range 1 to n
Given an array of n elements such that every element of the array is an integer in the range 1 to n, find the sum of all the distinct elements of the array.
Examples:
Input: arr[] = {5, 1, 2, 4, 6, 7, 3, 6, 7}
Output: 28
The distinct elements in the array are 1, 2,
3, 4, 5, 6, 7
Input: arr[] = {1, 1, 1}
Output: 1
The problem has appeared here as a general problem and the solution will work for the above case also. But a better approach is explained below.
The approach is to mark the occurrences of the array elements by making the elements at those indices negative. Example, a[0] = 1, a[1] = 1, a[2] = 1.
We check if a[abs(a[i])-1] is >=0, if yes, mark a[abs(a[i])-1] as negative. i.e. a[0] = 1 >=0, we mark a[1-1] as a[0] = -1. Next, a[1], check if (abs(a[1]-1) is +ve or not. If -ve, it means a[1] has already occurred before, else it is the first occurrence of this element.
Implementation:
C++
#include <iostream>
using namespace std;
int sumOfDistinct( int a[], int n)
{
int sum = 0;
for ( int i = 0; i < n; i++) {
if (a[ abs (a[i]) - 1] >= 0) {
sum += abs (a[i]);
a[ abs (a[i]) - 1] *= -1;
}
}
return sum;
}
int main()
{
int a[] = { 5, 1, 2, 4, 6, 7, 3, 6, 7 };
int n = sizeof (a)/ sizeof (a[0]);
cout << sumOfDistinct(a, n) << endl;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
import java.math.*;
class GFG{
static int sumOfDistinct( int a[], int n)
{
int sum = 0 ;
for ( int i = 0 ; i < n; i++) {
if (a[(Math.abs(a[i]) - 1 )] >= 0 ) {
sum += Math.abs(a[i]);
a[(Math.abs(a[i]) - 1 )] *= - 1 ;
}
}
return sum;
}
public static void main(String args[])
{
int a[] = { 5 , 1 , 2 , 4 , 6 , 7 , 3 , 6 , 7 };
int n = a.length;
System.out.println(sumOfDistinct(a, n) );
}
}
|
Python3
import math
def sumOfDistinct(a , n) :
sum = 0
i = 0
while i < n:
if (a[ abs (a[i]) - 1 ] > = 0 ) :
sum = sum + abs (a[i])
a[ abs (a[i]) - 1 ] = a[ abs (a[i]) - 1 ] * ( - 1 )
i = i + 1
return sum ;
a = [ 5 , 1 , 2 , 4 , 6 , 7 , 3 , 6 , 7 ]
n = len (a)
print (sumOfDistinct(a, n))
|
C#
using System;
class GFG{
static int sumOfDistinct( int []a, int n)
{
int sum = 0;
for ( int i = 0; i < n; i++) {
if (a[(Math.Abs(a[i]) - 1)] >= 0) {
sum += Math.Abs(a[i]);
a[(Math.Abs(a[i]) - 1)] *= - 1;
}
}
return sum;
}
public static void Main()
{
int []a = {5, 1, 2, 4, 6, 7, 3, 6, 7};
int n = a.Length;
Console.Write(sumOfDistinct(a, n));
}
}
|
PHP
<?php
function sumOfDistinct( $a , $n )
{
$sum = 0;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $a [ abs ( $a [ $i ]) - 1] >= 0)
{
$sum += abs ( $a [ $i ]);
$a [ abs ( $a [ $i ]) - 1] *= -1;
}
}
return $sum ;
}
$a = array (5, 1, 2, 4, 6, 7, 3, 6, 7);
$n = sizeof( $a );
echo sumOfDistinct( $a , $n ) ;
?>
|
Javascript
<script>
function sumOfDistinct(a, n)
{
let sum = 0;
for (let i = 0; i < n; i++)
{
if (a[(Math.abs(a[i])) - 1] >= 0) {
sum += Math.abs(a[i]);
a[(Math.abs(a[i]) - 1)] *= -1;
}
}
return sum;
}
let a = [5, 1, 2, 4, 6, 7, 3, 6, 7];
let n = a.length;
document.write( sumOfDistinct(a, n));
</script>
|
Time complexity: O(n)
Auxiliary Space : O(1)
Last Updated :
24 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...