Dada una array que contiene N elementos y un número K. La tarea es encontrar la suma de todos esos elementos que son divisibles por K.
Ejemplos :
Input : arr[] = {15, 16, 10, 9, 6, 7, 17} K = 3 Output : 30 Explanation: As 15, 9, 6 are divisible by 3. So, sum of elements divisible by K = 15 + 9 + 6 = 30. Input : arr[] = {5, 3, 6, 8, 4, 1, 2, 9} K = 2 Output : 20
La idea es recorrer la array y verificar los elementos uno por uno. Si un elemento es divisible por K, agregue el valor de ese elemento con la suma hasta el momento y continúe este proceso hasta que se alcance el final de la array.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to find sum of all the elements // in an array divisible by a given number K #include <iostream> using namespace std; // Function to find sum of all the elements // in an array divisible by a given number K int findSum(int arr[], int n, int k) { int sum = 0; // Traverse the array for (int i = 0; i < n; i++) { // If current element is divisible by k // add it to sum if (arr[i] % k == 0) { sum += arr[i]; } } // Return calculated sum return sum; } // Driver code int main() { int arr[] = { 15, 16, 10, 9, 6, 7, 17 }; int n = sizeof(arr) / sizeof(arr[0]); int k = 3; cout << findSum(arr, n, k); return 0; }
C
// C program to find sum of all the elements // in an array divisible by a given number K #include <stdio.h> // Function to find sum of all the elements // in an array divisible by a given number K int findSum(int arr[], int n, int k) { int sum = 0; // Traverse the array for (int i = 0; i < n; i++) { // If current element is divisible by k // add it to sum if (arr[i] % k == 0) { sum += arr[i]; } } // Return calculated sum return sum; } // Driver code int main() { int arr[] = { 15, 16, 10, 9, 6, 7, 17 }; int n = sizeof(arr) / sizeof(arr[0]); int k = 3; printf("%d",findSum(arr, n, k)); return 0; } // This code is contributed by kothavvsaakash.
Java
// Java program to find sum of all the elements // in an array divisible by a given number K import java.io.*; class GFG { // Function to find sum of all the elements // in an array divisible by a given number K static int findSum(int arr[], int n, int k) { int sum = 0; // Traverse the array for (int i = 0; i < n; i++) { // If current element is divisible by k // add it to sum if (arr[i] % k == 0) { sum += arr[i]; } } // Return calculated sum return sum; } // Driver code public static void main (String[] args) { int arr[] = { 15, 16, 10, 9, 6, 7, 17 }; int n = arr.length; int k = 3; System.out.println( findSum(arr, n, k)); } } // this code is contributed by anuj_67..
Python3
# Python3 program to find sum of # all the elements in an array # divisible by a given number K # Function to find sum of all # the elements in an array # divisible by a given number K def findSum(arr, n, k) : sum = 0 # Traverse the array for i in range(n) : # If current element is divisible # by k add it to sum if arr[i] % k == 0 : sum += arr[i] # Return calculated sum return sum # Driver code if __name__ == "__main__" : arr = [ 15, 16, 10, 9, 6, 7, 17] n = len(arr) k = 3 print(findSum(arr, n, k)) # This code is contributed by ANKITRAI1
C#
// C# program to find sum of all the elements // in an array divisible by a given number K using System; public class GFG{ // Function to find sum of all the elements // in an array divisible by a given number K static int findSum(int []arr, int n, int k) { int sum = 0; // Traverse the array for (int i = 0; i < n; i++) { // If current element is divisible by k // add it to sum if (arr[i] % k == 0) { sum += arr[i]; } } // Return calculated sum return sum; } // Driver code static public void Main (){ int []arr = { 15, 16, 10, 9, 6, 7, 17 }; int n = arr.Length; int k = 3; Console.WriteLine( findSum(arr, n, k)); } } //This code is contributed by anuj_67..
PHP
<?php // PHP program to find sum of all // the elements in an array divisible // by a given number K // Function to find sum of all // the elements in an array // divisible by a given number K function findSum($arr, $n, $k) { $sum = 0; // Traverse the array for ($i = 0; $i < $n; $i++) { // If current element is divisible // by k add it to sum if ($arr[$i] % $k == 0) { $sum += $arr[$i]; } } // Return calculated sum return $sum; } // Driver code $arr = array(15, 16, 10, 9, 6, 7, 17); $n = sizeof($arr); $k = 3; echo findSum($arr, $n, $k); // This code is contributed // by Akanksha Rai(Abby_akku)
Javascript
<script> // Javascript program to find sum of all the elements // in an array divisible by a given number K // Function to find sum of all the elements // in an array divisible by a given number K function findSum(arr, n, k) { let sum = 0; // Traverse the array for (let i = 0; i < n; i++) { // If current element is divisible by k // add it to sum if (arr[i] % k == 0) { sum += arr[i]; } } // Return calculated sum return sum; } let arr = [ 15, 16, 10, 9, 6, 7, 17 ]; let n = arr.length; let k = 3; document.write(findSum(arr, n, k)); </script>
Producción:
30
Complejidad de tiempo : O(N), donde N es el número de elementos en la array.
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por VishalBachchas y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA