Thursday, July 4, 2024
HomeData ModellingData Structure & AlgorithmProduct of all the elements in an array divisible by a given...

Product 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 product of all such elements of the array which are divisible by K.

Examples:  

Input : arr[] = {15, 16, 10, 9, 6, 7, 17}
        K = 3
Output : 810

Input : arr[] = {5, 3, 6, 8, 4, 1, 2, 9}
        K = 2
Output : 384 

The idea is to traverse the array and check the elements one by one. If an element is divisible by K then multiply that element’s value with the product so far and continue this process while the end of the array is reached.

Below is the implementation of the above approach:  

C++




// C++ program to find Product of all the elements
// in an array divisible by a given number K
 
#include <iostream>
using namespace std;
 
// Function to find Product of all the elements
// in an array divisible by a given number K
int findProduct(int arr[], int n, int k)
{
    int prod = 1;
 
    // Traverse the array
    for (int i = 0; i < n; i++) {
 
        // If current element is divisible by k
        // multiply with product so far
        if (arr[i] % k == 0) {
            prod *= arr[i];
        }
    }
 
    // Return calculated product
    return prod;
}
 
// Driver code
int main()
{
    int arr[] = { 15, 16, 10, 9, 6, 7, 17 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int k = 3;
 
    cout << findProduct(arr, n, k);
 
    return 0;
}


C




// C program to find Product of all the elements
// in an array divisible by a given number K
#include <stdio.h>
 
// Function to find Product of all the elements
// in an array divisible by a given number K
int findProduct(int arr[], int n, int k)
{
    int prod = 1;
 
    // Traverse the array
    for (int i = 0; i < n; i++) {
 
        // If current element is divisible by k
        // multiply with product so far
        if (arr[i] % k == 0) {
            prod *= arr[i];
        }
    }
 
    // Return calculated product
    return prod;
}
 
// 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",findProduct(arr, n, k));
 
    return 0;
}
 
// This code is contributed by kothavvsaakash.


Java




// Java program to find Product of all the elements
// in an array divisible by a given number K
 
import java.io.*;
 
class GFG {
 
// Function to find Product of all the elements
// in an array divisible by a given number K
static int findProduct(int arr[], int n, int k)
{
    int prod = 1;
 
    // Traverse the array
    for (int i = 0; i < n; i++) {
 
        // If current element is divisible by k
        // multiply with product so far
        if (arr[i] % k == 0) {
            prod *= arr[i];
        }
    }
 
    // Return calculated product
    return prod;
}
 
// 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(findProduct(arr, n, k));
    }
}
 
 
// This code is contributed by inder_verma..


Python3




# Python3 program to find Product of all
# the elements in an array divisible by
# a given number K
 
# Function to find Product of all the elements
# in an array divisible by a given number K
def findProduct(arr, n, k):
 
    prod = 1
 
    # Traverse the array
    for i in range(n):
 
        # If current element is divisible
        # by k, multiply with product so far
        if (arr[i] % k == 0):
            prod *= arr[i]
 
    # Return calculated product
    return prod
 
# Driver code
if __name__ == "__main__":
 
    arr= [15, 16, 10, 9, 6, 7, 17 ]
    n = len(arr)
    k = 3
 
    print (findProduct(arr, n, k))
 
# This code is contributed by ita_c


C#




// C# program to find Product of all
// the elements in an array divisible
// by a given number K
using System;
 
class GFG
{
 
// Function to find Product of all
// the elements in an array divisible
// by a given number K
static int findProduct(int []arr, int n, int k)
{
    int prod = 1;
 
    // Traverse the array
    for (int i = 0; i < n; i++)
    {
 
        // If current element is divisible
        // by k multiply with product so far
        if (arr[i] % k == 0)
        {
            prod *= arr[i];
        }
    }
 
    // Return calculated product
    return prod;
}
 
// Driver code
public static void Main()
{
    int []arr = { 15, 16, 10, 9, 6, 7, 17 };
    int n = arr.Length;
    int k = 3;
     
    Console.WriteLine(findProduct(arr, n, k));
}
}
 
// This code is contributed by inder_verma


PHP




<?php
// PHP program to find Product of
// all the elements in an array
// divisible by a given number K
 
// Function to find Product of
// all the elements in an array
// divisible by a given number K
function findProduct(&$arr, $n, $k)
{
    $prod = 1;
 
    // Traverse the array
    for ($i = 0; $i < $n; $i++)
    {
 
        // If current element is divisible 
        // by k multiply with product so far
        if ($arr[$i] % $k == 0)
        {
            $prod *= $arr[$i];
        }
    }
 
    // Return calculated product
    return $prod;
}
 
// Driver code
$arr = array(15, 16, 10, 9, 6, 7, 17 );
$n = sizeof($arr);
$k = 3;
 
echo (findProduct($arr, $n, $k));
 
// This code is contributed
// by Shivi_Aggarwal
?>


Javascript




<script>
// Function to find Product of all the elements
// in an array divisible by a given number K
function findProduct( arr, n,  k)
{
    var prod = 1;
 
    // Traverse the array
    for (var i = 0; i < n; i++) {
 
        // If current element is divisible by k
        // multiply with product so far
        if (arr[i] % k == 0) {
            prod *= arr[i];
        }
    }
 
    // Return calculated product
    return prod;
}
 
var arr = [15, 16, 10, 9, 6, 7, 17 ];
     
 
    document.write(findProduct(arr, 7, 3));
 
 
 
</script>


Output

810

Complexity Analysis:

  • Time Complexity: O(N), where N is the number of elements in the array.
    Auxiliary Space: O(1) 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments