Sum of array elements which are multiples of a given number Last Updated : 11 Aug, 2022 Comments Improve Suggest changes Like Article Like Report Given an array arr[] consisting of positive integers and an integer N, the task is to find the sum of all array elements which are multiples of N Examples: Input: arr[] = {1, 2, 3, 5, 6}, N = 3Output: 9Explanation: From the given array, 3 and 6 are multiples of 3. Therefore, sum = 3 + 6 = 9. Input: arr[] = {1, 2, 3, 5, 7, 11, 13}, N = 5Output: 5 Approach: The idea is to traverse the array and for each array element, check if it is a multiple of N or not and add those elements. Follow the steps below to solve the problem: Initialize a variable, say sum, to store the required sum.Traverse the given array and for each array element, perform the following operations.Check whether the array element is a multiple of N or not.If the element is a multiple of N, then add the element to sum.Finally, print the value of sum. Below is the implementation of the above approach: C++ // C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find the sum of array // elements which are multiples of N void mulsum(int arr[], int n, int N) { // Stores the sum int sum = 0; // Traverse the given array for (int i = 0; i < n; i++) { // If current element // is a multiple of N if (arr[i] % N == 0) { sum = sum + arr[i]; } } // Print total sum cout << sum; } // Driver Code int main() { // Given arr[] int arr[] = { 1, 2, 3, 5, 6 }; int n = sizeof(arr) / sizeof(arr[0]); int N = 3; mulsum(arr, n, N); return 0; } Java // Java program for the above approach import java.io.*; import java.util.*; class GFG{ // Function to find the sum of array // elements which are multiples of N static void mulsum(int arr[], int n, int N) { // Stores the sum int sum = 0; // Traverse the given array for (int i = 0; i < n; i++) { // If current element // is a multiple of N if (arr[i] % N == 0) { sum = sum + arr[i]; } } // Print total sum System.out.println(sum); } // Driver Code public static void main(String[] args) { // Given arr[] int arr[] = { 1, 2, 3, 5, 6 }; int n = arr.length; int N = 3; mulsum(arr, n, N); } } // This code is contributed by jana_sayantan. Python # Python3 program for the above approach # Function to find the sum of array # elements which are multiples of N def mulsum(arr, n, N): # Stores the sum sums = 0 # Traverse the array for i in range(0, n): if arr[i] % N == 0: sums = sums + arr[i] # Print total sum print(sums) # Driver Code if __name__ == "__main__": # Given arr[] arr = [ 1, 2, 3, 5, 6 ] n = len(arr) N = 3 # Function call mulsum(arr, n, N) C# // C# program for the above approach using System; public class GFG { // Function to find the sum of array // elements which are multiples of N static void mulsum(int[] arr, int n, int N) { // Stores the sum int sum = 0; // Traverse the given array for (int i = 0; i < n; i++) { // If current element // is a multiple of N if (arr[i] % N == 0) { sum = sum + arr[i]; } } // Print total sum Console.Write(sum); } // Driver Code static public void Main () { // Given arr[] int[] arr = { 1, 2, 3, 5, 6 }; int n = arr.Length; int N = 3; mulsum(arr, n, N); } } // This code is contributed by Dharanendra L V. JavaScript <script> // JavaScript program for the above approach // Function to find the sum of array // elements which are multiples of N function mulsum(arr, n, N) { // Stores the sum var sum = 0; // Traverse the given array for(var i = 0; i < n; i++) { // If current element // is a multiple of N if (arr[i] % N == 0) { sum = sum + arr[i]; } } // Print total sum document.write(sum); } // Driver Code // Given arr[] var arr = [ 1, 2, 3, 5, 6 ]; var n = arr.length; var N = 3; mulsum(arr, n, N); // This code is contributed by rdtank </script> Output: 9 Time Complexity: O(N) since one traversal of the array is required to complete all operations hence the overall time required by the algorithm is linearAuxiliary Space: O(1) since no extra array is used so the space taken by the algorithm is constant Comment More infoAdvertise with us Next Article Sum of array elements which are multiples of a given number T thotasravya28 Follow Improve Article Tags : Searching Mathematical Technical Scripter DSA Arrays Technical Scripter 2020 divisibility +3 More Practice Tags : ArraysMathematicalSearching Similar Reads Sum of array elements which are prime factors of a given number Given an array arr[] of size N and a positive integer K, the task is to find the sum of all array elements which are prime factors of K. Examples: Input: arr[] = {1, 2, 3, 5, 6, 7, 15}, K = 35Output: 12Explanation: From the given array, 5 and 7 are prime factors of 35. Therefore, required sum = 5 + 8 min read Sum of multiples of Array elements within a given range [L, R] Given an array arr[] of positive integers and two integers L and R, the task is to find the sum of all multiples of the array elements in the range [L, R]. Examples: Input: arr[] = {2, 7, 3, 8}, L = 7, R = 20 Output: 197 Explanation: In the range 7 to 20: Sum of multiples of 2: 8 + 10 + 12 + 14 + 16 7 min read Sum of all the elements in an array divisible by a given number K Given an array containing N elements and a number K. The task is to find the sum of all such elements which are divisible by K. Examples: 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. Inp 13 min read Count of elements which is the sum of a subarray of the given Array Given an array arr[], the task is to count elements in an array such that there exists a subarray whose sum is equal to this element.Note: Length of subarray must be greater than 1. Examples: Input: arr[] = {1, 2, 3, 4, 5, 6, 7} Output: 4 Explanation: There are 4 such elements in array - arr[2] = 3 7 min read Sum of multiples of A and B less than N Given three integers n, a, b, the task is to find the sum of all numbers in the range [1, n] that are multiples of either a or b.Examples: Input: n = 10, a = 2, b = 3Output: 42Explanation: The numbers divisible by 2 or 3 are 2, 3, 4, 6, 8, 9, 10.Input: n = 100, a = 22, b = 23Output: 450Explanation: 8 min read Sum of all numbers in the given range which are divisible by M Given three numbers A, B and M such that A < B, the task is to find the sum of numbers divisible by M in the range [A, B]. Examples: Input: A = 25, B = 100, M = 30 Output: 180 Explanation: In the given range [25, 100] 30, 60 and 90 are the numbers which are divisible by M = 30 Therefore, sum of t 15 min read Sum of all even occurring element in an array Given an array of integers containing duplicate elements. The task is to find the sum of all even occurring elements in the given array. That is the sum of all such elements whose frequency is even in the array. Examples: Input : arr[] = {1, 1, 2, 2, 3, 3, 3}Output : 6The even occurring element are 12 min read Sum of all odd frequency elements in an array Given an array of integers containing duplicate elements. The task is to find the sum of all odd occurring elements in the given array. That is the sum of all such elements whose frequency is odd in the array. Examples: Input : arr[] = {1, 1, 2, 2, 3, 3, 3} Output : 9 The odd occurring element is 3, 8 min read Sum of all the multiples of 3 and 7 below N Given a number N, the task is to find the sum of all the multiples of 3 and 7 below N. Note: A number must not repeat itself in the sum. Examples: Input: N = 10 Output: 25 3 + 6 + 7 + 9 = 25 Input: N = 24 Output: 105 3 + 6 + 7 + 9 + 12 + 14 + 15 + 18 + 21 = 105 Brute Force Approach: A brute force ap 8 min read Find elements in given Array that are a factor of sum of remaining elements Given an array A[] of size N, the task is to find the elements in the array which are factors of the sum of the remaining element. So just select an element from an array and take the sum of the remaining elements and check whether the sum is perfectly divisible by the selected element or not. If it 11 min read Like