Given multiple numbers and a number n, the task is to print the remainder after multiply all the number divide by n.
Input : arr[] = {100, 10, 5, 25, 35, 14},
n = 11
Output : 9
100 x 10 x 5 x 25 x 35 x 14 = 61250000 % 11 = 9
Input : arr[] = {100, 10},
n = 5
Output : 0
100 x 10 = 1000 % 5 = 0
Naive approach: First multiple all the number then take % by n then find the remainder, But in this approach if number is maximum of 2^64 then it give wrong answer.
Implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findremainder( int arr[], int len, int n)
{
long long int product = 1;
for ( int i = 0; i < len; i++) {
product = product * arr[i];
}
return product % n;
}
int main()
{
int arr[] = { 100, 10, 5, 25, 35, 14 };
int len = sizeof (arr) / sizeof (arr[0]);
int n = 11;
cout << findremainder(arr, len, n);
return 0;
}
|
Java
public class Main {
public static int findRemainder( int [] arr, int len,
int n)
{
int product = 1 ;
for ( int i = 0 ; i < len; i++) {
product = product * arr[i];
}
return product % n;
}
public static void main(String[] args)
{
int [] arr = { 100 , 10 , 5 , 25 , 35 , 14 };
int len = arr.length;
int n = 11 ;
System.out.println(findRemainder(arr, len, n));
}
}
|
Python3
def findremainder(arr, len , n):
product = 1
for i in range ( len ):
product = product * arr[i]
return product % n
arr = [ 100 , 10 , 5 , 25 , 35 , 14 ]
len = len (arr)
n = 11
print (findremainder(arr, len , n))
|
C#
using System;
class Program
{
static int findRemainder( int [] arr, int len, int n)
{
long product = 1;
for ( int i = 0; i < len; i++)
{
product *= arr[i];
}
return ( int )(product % n);
}
static void Main()
{
int [] arr = { 100, 10, 5, 25, 35, 14 };
int len = arr.Length;
int n = 11;
Console.WriteLine(findRemainder(arr, len, n));
}
}
|
Javascript
function findRemainder(arr, len, n) {
let product = 1;
for (let i = 0; i < len; i++) {
product = product * arr[i];
}
return product % n;
}
let arr = [100, 10, 5, 25, 35, 14];
let len = arr.length;
let n = 11;
console.log(findRemainder(arr, len, n));
|
Approach that avoids overflow : First take a remainder or individual number like arr[i] % n. Then multiply the remainder with current result. After multiplication, again take remainder to avoid overflow. This works because of distributive properties of modular arithmetic. ( a * b) % c = ( ( a % c ) * ( b % c ) ) % c
C++
#include <iostream>
using namespace std;
int findremainder( int arr[], int len, int n)
{
int mul = 1;
for ( int i = 0; i < len; i++)
mul = (mul * (arr[i] % n)) % n;
return mul % n;
}
int main()
{
int arr[] = { 100, 10, 5, 25, 35, 14 };
int len = sizeof (arr) / sizeof (arr[0]);
int n = 11;
cout << findremainder(arr, len, n);
}
|
Java
import java.util.*;
import java.lang.*;
public class GfG{
public static int findremainder( int arr[],
int len, int n)
{
int mul = 1 ;
for ( int i = 0 ; i < len; i++)
mul = (mul * (arr[i] % n)) % n;
return mul % n;
}
public static void main(String argc[])
{
int [] arr = new int []{ 100 , 10 , 5 ,
25 , 35 , 14 };
int len = 6 ;
int n = 11 ;
System.out.println(findremainder(arr, len, n));
}
}
|
Python3
def findremainder(arr, lens, n):
mul = 1
for i in range (lens):
mul = (mul * (arr[i] % n)) % n
return mul % n
arr = [ 100 , 10 , 5 , 25 , 35 , 14 ]
lens = len (arr)
n = 11
print ( findremainder(arr, lens, n))
|
Javascript
<script>
function findremainder(arr, len, n)
{
let mul = 1;
for (let i = 0; i < len; i++)
mul = (mul * (arr[i] % n)) % n;
return mul % n;
}
let arr = [ 100, 10, 5, 25, 35, 14 ];
let len = 6;
let n = 11;
document.write(findremainder(arr, len, n));
</script>
|
C#
using System;
public class GfG{
public static int findremainder( int []arr,
int len, int n)
{
int mul = 1;
for ( int i = 0; i < len; i++)
mul = (mul * (arr[i] % n)) % n;
return mul % n;
}
public static void Main()
{
int [] arr = new int []{ 100, 10, 5,
25, 35, 14 };
int len = 6;
int n = 11;
Console.WriteLine(findremainder(arr, len, n));
}
}
|
PHP
<?php
function findremainder( $arr , $len , $n )
{
$mul = 1;
for ( $i = 0; $i < $len ; $i ++)
$mul = ( $mul * ( $arr [ $i ] % $n )) % $n ;
return $mul % $n ;
}
$arr = array (100, 10, 5, 25, 35, 14);
$len = sizeof( $arr );
$n = 11;
echo (findremainder( $arr , $len , $n ));
?>
|
Time Complexity: O(len), where len is the size of the given array
Auxiliary Space: O(1)
Contact Us