Find Product - Hackerearth
Product of Array Elements Modulo
Given an array of positive integers, the task is to find the product of all the numbers in the array modulo . This is a common problem in competitive programming to handle large products and avoid overflow issues.
Problem Statement
You have been given an array of size consisting of positive integers. You need to find and print the product of all the numbers in this array modulo .
Input Format:
The first line contains a single integer denoting the size of the array.
The next line contains 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 .
Constraints:
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 .
Java: Uses a
Scanner
to read input and performs the calculation with along
to handle large numbers.Python: Defines a function
product_modulo
and uses the built-ininput
andmap
functions for handling input and conversion.C++: Utilizes
cin
for input andlong long
for handling large numbers.C: Uses
scanf
for input andlong 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 is essential for handling large number multiplications without overflow. These implementations provide efficient solutions in various programming languages.
Comments
Post a Comment