当元素在 1 到 n 范围内时,不同元素的总和
给定一个包含 n 个元素的数组,使得数组中的每个元素都是 1 到 n 范围内的整数,求该数组中所有不同元素的总和。
例子:
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
该问题已作为一般问题出现在这里,该解决方案也适用于上述情况。但是下面解释了一种更好的方法。
该方法是通过使那些索引处的元素为负来标记数组元素的出现。例如,a[0] = 1,a[1] = 1,a[2] = 1。
我们检查 a[abs(a[i])-1] 是否 >=0,如果是,则将 a[abs(a[i])-1] 标记为负数。即a[0] = 1 >=0,我们将a[1-1]标记为a[0] = -1。接下来,a[1],检查(abs(a[1]-1)是否为+ve。如果是-ve,则表示a[1]之前已经出现过,否则为该元素的第一次出现。请参考以下代码。
C++
// C++ program to find sum of distinct elements
#include
using namespace std;
// Returns sum of distinct elements in arr[] assuming
// that elements in a[] are in range from 1 to n.
int sumOfDistinct(int a[], int n)
{
int sum = 0;
for (int i = 0; i < n; i++) {
// If element appears first time
if (a[abs(a[i]) - 1] >= 0) {
sum += abs(a[i]);
a[abs(a[i]) - 1] *= -1;
}
}
return sum;
}
// Driver code
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
// JAVA program to find sum of distinct
// elements in sorted order
import java.io.*;
import java.util.*;
import java.math.*;
class GFG{
// Returns sum of distinct elements in arr[]
// assuming that elements in a[] are in
// range from 1 to n.
static int sumOfDistinct(int a[], int n)
{
int sum = 0;
for (int i = 0; i < n; i++) {
// If element appears first time
if (a[Math.abs(a[i]) - 1] >= 0) {
sum += Math.abs(a[i]);
a[Math.abs(a[i]) - 1] *= -1;
}
}
return sum;
}
// Driver code
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) );
}
}
// This code is contributed by Nikita Tiwari.
Python3
# Python program to find sum of distinct elements
# in sorted order
import math
# Returns sum of distinct elements in arr[]
# assuming that elements in a[] are in
# range from 1 to n.
def sumOfDistinct(a , n) :
sum = 0
i = 0
while i < n:
# If element appears first time
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;
# Driver code
a = [ 5, 1, 2, 4, 6, 7, 3, 6, 7 ]
n = len(a)
print (sumOfDistinct(a, n))
# This code is contributed by Nikita Tiwari.
C#
// C# program to find sum of distinct
// elements in sorted order
using System;
class GFG{
// Returns sum of distinct elements
// in arr[] assuming that elements
// in a[] are in range from 1 to n
static int sumOfDistinct(int []a, int n)
{
int sum = 0;
for (int i = 0; i < n; i++) {
// If element appears first time
if (a[Math.Abs(a[i]) - 1] >= 0) {
sum += Math.Abs(a[i]);
a[Math.Abs(a[i]) - 1] *= - 1;
}
}
return sum;
}
// Driver code
public static void Main()
{
int []a = {5, 1, 2, 4, 6, 7, 3, 6, 7};
int n = a.Length;
Console.Write(sumOfDistinct(a, n));
}
}
// This code is contributed by Nitin Mittal
PHP
= 0)
{
$sum += abs($a[$i]);
$a[abs($a[$i]) - 1] *= -1;
}
}
return $sum;
}
// Driver code
$a = array(5, 1, 2, 4, 6, 7, 3, 6, 7);
$n = sizeof($a);
echo sumOfDistinct($a, $n) ;
// This code is contributed by nitin mittal
?>
Javascript
输出:
28