Find Product - Hackerearth

Product of Array Elements Modulo 109+710^9 + 7

Given an array of positive integers, the task is to find the product of all the numbers in the array modulo 109+710^9 + 7. This is a common problem in competitive programming to handle large products and avoid overflow issues.

Problem Statement

You have been given an array AA of size NN consisting of positive integers. You need to find and print the product of all the numbers in this array modulo 109+710^9 + 7.

Input Format:

  • The first line contains a single integer NN denoting the size of the array.

  • The next line contains NN space-separated integers denoting the elements of the array.

Output Format:

  • Print a single integer denoting the product of all the elements of the array modulo 109+710^9 + 7.

Constraints:

  • 1N1031 \leq N \leq 10^3

  • 1A[i]1031 \leq A[i] \leq 10^3

Example

Input: 5 1 2 3 4 5

Output: 120

Solutions:

Now let’s see how to solve this problem in different programming languages: Java, Python, C, and C++.

1. Java

import java.util.Scanner;

public class ProductModulo {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in); // Initialize Scanner for input
        int n = sc.nextInt(); // Read the size of the array
        int[] a = new int[n]; // Initialize the array
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt(); // Read array elements
        }
        long ans = 1; // Initialize the answer variable
        int MOD = 1000000007; // Define the modulo value
        for (int i = 0; i < n; i++) {
            ans = (ans * a[i]) % MOD; // Update the product modulo
        }
        System.out.println(ans); // Output the result
        sc.close(); // Close the Scanner
    }
}

2. Python

MOD = 1000000007

def product_modulo(arr):
    result = 1
    for num in arr:
        result = (result * num) % MOD # Update the product modulo
    return result

# Input from user
n = int(input("Enter the number of elements: "))
arr = list(map(int, input("Enter the elements: ").split()))

# Calculate and print the product modulo
print(product_modulo(arr))

3. C++

#include <iostream>
using namespace std;

int main() {
    int n;
    cin >> n; // Read the size of the array
    int arr[n];
    for (int i = 0; i < n; i++) {
        cin >> arr[i]; // Read array elements
    }
    long long ans = 1; // Initialize the answer variable
    const int MOD = 1000000007; // Define the modulo value
    for (int i = 0; i < n; i++) {
        ans = (ans * arr[i]) % MOD; // Update the product modulo
    }
    cout << ans << endl; // Output the result
    return 0;
}

4. C

#include <stdio.h>

#define MOD 1000000007

int main() {
    int n;
    scanf("%d", &n); // Read the size of the array
    int arr[n];
    for (int i = 0; i < n; i++) {
        scanf("%d", &arr[i]); // Read array elements
    }
    long long ans = 1; // Initialize the answer variable
    for (int i = 0; i < n; i++) {
        ans = (ans * arr[i]) % MOD; // Update the product modulo
    }
    printf("%lld\n", ans); // Output the result
    return 0;
}

Explanation of Code:

Each code version calculates the product of all elements in the array and then takes the result modulo 109+710^9 + 7.

  • Java: Uses a Scanner to read input and performs the calculation with a long to handle large numbers.

  • Python: Defines a function product_modulo and uses the built-in input and map functions for handling input and conversion.

  • C++: Utilizes cin for input and long long for handling large numbers.

  • C: Uses scanf for input and long long for large number calculations.


Sample Input & Output:

Input: 5 1 2 3 4 5

Output: 120


Conclusion:

Calculating the product of array elements modulo 109+710^9 + 7 is essential for handling large number multiplications without overflow. These implementations provide efficient solutions in various programming languages.


**  Please do subscribe my blog for future updates and share with your friends, if you find this informative **


Comments

Popular Posts