Find Product ! Hacker Earth
You have been given an array of size consisting of positive integers. You need to find and print the product of all the number 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
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 .
Print a single integer denoting the product of all the elements of the array Modulo .
Constraints:
#include <stdio.h>
int main()
{
unsigned N, i, answer = 1;
do
{
scanf("%d", &N);
}
while(N < 1 && N > 1000);
long int a[N];
for(i = 0; i < N ; i++)
{
do
{
scanf("%li", &a[i]);
}
while(a[i] < 1 && a[i] > 1000);
}
for(i = 0; i < N; i++)
{
answer = (answer * a[i]) % (1000000007);
}
printf("%i", answer);
}
if in python ?
ReplyDeleteHi Mownika,
DeleteBelow is the solution in python.
____________________
def product(A, N):
answer = 1
for i in range(0, N):
answer = answer * A[i]
return answer
A = [ 1, 2, 3, 4, 5 ]
N = len(A)
print(product(A, N))
Hi Mownika,
DeleteApologies the above solution doesn't pass all the test case.
please try below one.
______________
n = int(input())
answer = 1
arr = [int(i) for i in input().split()]
for i in arr:
answer= (answer * i)%(10**9+7)
print(answer)
Thanks.
This comment has been removed by the author.
ReplyDeleteHere is a solution passing all the test case in C++/14
ReplyDelete#include
using namespace std;
int main(){
int n;
cin>>n;
int arr[n];
for(int i=0;i>arr[i];
}
long long int pro=1;
for(int i=0;i<n;++i){
pro= (pro * arr[i]) % (1000000007);
}
cout<<fixed<<pro;
return 0;
}
//By- Priyansh Kr.
//priyansh.kumar2805@gmail.com
Thanks
Delete