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 = 3
Output: 9
Explanation: 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 = 5
Output: 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> |
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 linear
Auxiliary Space: O(1) since no extra array is used so the space taken by the algorithm is constant
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!