Find Product ! Hacker Earth

You have been given an array A of size N consisting of positive integers. You need to find and print the product of all the number in this array Modulo 109+7.

Input Format:
The first line contains a single integer N denoting the size of the array. The next line contains N 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+7.

Constraints:
1N103

#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);  
 }  

Comments

  1. Replies
    1. Hi Mownika,

      Below 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))

      Delete
    2. Hi Mownika,

      Apologies 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.

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Here is a solution passing all the test case in C++/14

    #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

    ReplyDelete

Post a Comment

Popular posts from this blog

Seating Arrangement

Roy and Profile picture