给定一个数组,我们必须求一个数组中重复k次的所有元素的和。总之,我们需要考虑每一个重复元素。 例如:
null
Input : arr[] = {2, 3, 9, 9} k = 1Output : 52 + 3 = 5Input : arr[] = {9, 8, 8, 8, 10, 4} k = 3Output : 8
一 简单解决方案 就是使用两个嵌套循环来计算每个元素的出现次数。在计数时,我们只需要考虑一个元素,如果它还没有被考虑。
C++
// C++ program find sum of elements that // appear k times. #include <bits/stdc++.h> using namespace std; // Function to count the sum int sumKRepeating( int arr[], int n, int k) { int sum = 0; // To keep track of processed elements vector< bool > visited(n, false ); // initializing count equal to zero for ( int i = 0; i < n; i++) { // If arr[i] already processed if (visited[i] == true ) continue ; // counting occurrences of arr[i] int count = 1; for ( int j = i + 1; j < n; j++) { if (arr[i] == arr[j]) { count++; visited[j] = true ; } } if (count == k) sum += arr[i]; } return sum; } // Driver code int main() { int arr[] = { 9, 9, 10, 11, 8, 8, 9, 8 }; int n = sizeof (arr)/ sizeof (arr[0]); int k = 3; cout << sumKRepeating(arr, n, k); return 0; } |
JAVA
// Java program find sum of // elements that appear k times. import java.util.*; class GFG { // Function to count the sum static int sumKRepeating( int arr[], int n, int k) { int sum = 0 ; // To keep track of // processed elements Vector<Boolean> visited = new Vector<Boolean>(); for ( int i = 0 ; i < n; i++) visited.add( false ); // initializing count // equal to zero for ( int i = 0 ; i < n; i++) { // If arr[i] already processed if (visited.get(i) == true ) continue ; // counting occurrences of arr[i] int count = 1 ; for ( int j = i + 1 ; j < n; j++) { if (arr[i] == arr[j]) { count++; visited.add(j, true ); } } if (count == k) sum += arr[i]; } return sum; } // Driver code public static void main(String args[]) { int arr[] = { 9 , 9 , 10 , 11 , 8 , 8 , 9 , 8 }; int n = arr.length; int k = 3 ; System.out.println(sumKRepeating(arr, n, k)); } } // This code is contributed by Arnab Kundu |
Python3
# Python 3 program find sum of elements # that appear k times. # Function to count the sum def sumKRepeating(arr, n, k): sum = 0 # To keep track of processed elements visited = [ False for i in range (n)] # initializing count equal to zero for i in range ( 0 , n, 1 ): # If arr[i] already processed if (visited[i] = = True ): continue # counting occurrences of arr[i] count = 1 for j in range (i + 1 , n, 1 ): if (arr[i] = = arr[j]): count + = 1 visited[j] = True if (count = = k): sum + = arr[i] return sum # Driver code if __name__ = = '__main__' : arr = [ 9 , 9 , 10 , 11 , 8 , 8 , 9 , 8 ] n = len (arr) k = 3 print (sumKRepeating(arr, n, k)) # This code is contributed by # Shashank_Sharma |
C#
// c# program find sum of // elements that appear k times. using System; using System.Collections.Generic; class GFG { // Function to count the sum public static int sumKRepeating( int [] arr, int n, int k) { int sum = 0; // To keep track of // processed elements List< bool > visited = new List< bool >(); for ( int i = 0; i < n; i++) { visited.Add( false ); } // initializing count // equal to zero for ( int i = 0; i < n; i++) { // If arr[i] already processed if (visited[i] == true ) { continue ; } // counting occurrences of arr[i] int count = 1; for ( int j = i + 1; j < n; j++) { if (arr[i] == arr[j]) { count++; visited.Insert(j, true ); } } if (count == k) { sum += arr[i]; } } return sum; } // Driver code public static void Main( string [] args) { int [] arr = new int [] {9, 9, 10, 11, 8, 8, 9, 8}; int n = arr.Length; int k = 3; Console.WriteLine(sumKRepeating(arr, n, k)); } } // This code is contributed // by Shrikant13 |
Javascript
<script> // Javascript program find sum of // elements that appear k times. // Function to count the sum function sumKRepeating(arr,n,k) { let sum = 0; // To keep track of // processed elements let visited = []; for (let i = 0; i < n; i++) visited.push( false ); // initializing count // equal to zero for (let i = 0; i < n; i++) { // If arr[i] already processed if (visited[i] == true ) continue ; // counting occurrences of arr[i] let count = 1; for (let j = i + 1; j < n; j++) { if (arr[i] == arr[j]) { count++; visited[j]= true ; } } if (count == k) sum += arr[i]; } return sum; } // Driver code let arr=[9, 9, 10, 11, 8, 8, 9, 8 ]; let n = arr.length; let k = 3; document.write(sumKRepeating(arr, n, k)); // This code is contributed by patel2127 </script> |
输出:
17
时间复杂性: O(n*n) 辅助空间: O(n) 一 有效解决方案 就是使用散列。我们计算所有项目的频率。然后我们遍历哈希表,对出现次数为k的项求和。
C++
// C++ program find sum of elements that // appear k times. #include <bits/stdc++.h> using namespace std; // Returns sum of k appearing elements. int sumKRepeating( int arr[], int n, int k) { int sum = 0; // Count frequencies of all items unordered_map< int , int > mp; for ( int i=0; i<n; i++) mp[arr[i]]++; // Sum items with frequencies equal to k. for ( auto x : mp) if (x.second == k) sum += x.first; return sum; } // Driver code int main() { int arr[] = { 9, 9, 10, 11, 8, 8, 9, 8 }; int n = sizeof (arr)/ sizeof (arr[0]); int k = 3; cout << sumKRepeating(arr, n, k); return 0; } |
JAVA
// Java program find sum of // elements that appear k times. import java.util.HashMap; import java.util.Map; class GfG { // Returns sum of k appearing elements. static int sumKRepeating( int arr[], int n, int k) { int sum = 0 ; // Count frequencies of all items HashMap<Integer, Integer> mp = new HashMap<>(); for ( int i= 0 ; i<n; i++) { if (!mp.containsKey(arr[i])) mp.put(arr[i], 0 ); mp.put(arr[i], mp.get(arr[i]) + 1 ); } // Sum items with frequencies equal to k. for (Integer x : mp.keySet()) if (mp.get(x) == k) sum += x; return sum; } // Driver code public static void main(String []args) { int arr[] = { 9 , 9 , 10 , 11 , 8 , 8 , 9 , 8 }; int n = arr.length; int k = 3 ; System.out.println(sumKRepeating(arr, n, k)); } } // This code is contributed by Rituraj Jain |
Python3
# Python3 program find Sum of elements # that appear k times. import math as mt # Returns Sum of k appearing elements. def sumKRepeating(arr, n, k): Sum = 0 # Count frequencies of all items mp = dict () for i in range (n): if arr[i] in mp.keys(): mp[arr[i]] + = 1 else : mp[arr[i]] = 1 # Sum items with frequencies equal to k. for x in mp: if (mp[x] = = k): Sum + = x return Sum # Driver code arr = [ 9 , 9 , 10 , 11 , 8 , 8 , 9 , 8 ] n = len (arr) k = 3 print (sumKRepeating(arr, n, k)) # This code is contributed # by Mohit kumar 29 |
C#
// C# program find sum of // elements that appear k times. using System; using System.Collections.Generic; class GfG { // Returns sum of k appearing elements. static int sumKRepeating( int []arr, int n, int k) { int sum = 0; // Count frequencies of all items Dictionary< int , int > mp = new Dictionary< int , int >(); for ( int i = 0 ; i < n; i++) { if (mp.ContainsKey(arr[i])) { var val = mp[arr[i]]; mp.Remove(arr[i]); mp.Add(arr[i], val + 1); } else { mp.Add(arr[i], 1); } } // Sum items with frequencies equal to k. foreach (KeyValuePair< int , int > entry in mp) { if (entry.Value >= k) { sum += entry.Key; } } return sum; } // Driver code public static void Main(String []args) { int []arr = { 9, 9, 10, 11, 8, 8, 9, 8 }; int n = arr.Length; int k = 3; Console.WriteLine(sumKRepeating(arr, n, k)); } } // This code contributed by Rajput-Ji |
Javascript
<script> // JavaScript program find sum of // elements that appear k times. // Returns sum of k appearing elements. function sumKRepeating(arr,n,k) { let sum = 0; // Count frequencies of all items let mp = new Map(); for (let i=0; i<n; i++) { if (!mp.has(arr[i])) mp.set(arr[i], 0); mp.set(arr[i], mp.get(arr[i]) + 1); } // Sum items with frequencies equal to k. for (let [key, value] of mp.entries()) if (mp.get(key) == k) sum += key; return sum; } // Driver code let arr=[9, 9, 10, 11, 8, 8, 9, 8]; let n = arr.length; let k = 3; document.write(sumKRepeating(arr, n, k)); // This code is contributed by unknown2108 </script> |
输出:
17
时间复杂性: O(n) 辅助空间: O(n)
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END