Right most non-zero digit in multiplication of array elements
Last Updated :
01 Feb, 2023
Given an array arr[] of N non-negative integers. The task is to find the rightmost non-zero digit in the product of array elements.
Examples:
Input: arr[] = {3, 5, 6, 90909009}
Output: 7
Input: arr[] = {7, 42, 11, 64}
Output: 6
Result of multiplication is 206976
So the rightmost digit is 6
Approach:
- The question is too simple if you know basic maths. It is given that you have to find the rightmost positive digit. Now a digit is made multiple of 10 if there are 2 and 5. They produce a number with last digit 0.
- Now what we can do is divide each array element into its shortest divisible form by 5 and increase count of such occurrences.
- Now divide each array element into its shortest divisible form by 2 and decrease count of such occurrences. This way we are not considering the multiplication of 2 and a 5 in our multiplication.
- Set the multiplier value as either 1 or 5 in case count of 5 is not 0 after above two loops.
- Multiply each array variable now and store just last digit by taking remainder by 10
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int rightmostNonZero( int a[], int n)
{
int c5 = 0;
for ( int i = 0; i < n; i++) {
while (a[i] > 0 && a[i] % 5 == 0) {
a[i] /= 5;
c5++;
}
}
for ( int i = 0; i < n; i++) {
while (c5 && a[i] > 0 && !(a[i] & 1)) {
a[i] >>= 1;
c5--;
}
}
long long ans = 1;
for ( int i = 0; i < n; i++) {
ans = (ans * a[i] % 10) % 10;
}
if (c5)
ans = (ans * 5) % 10;
if (ans)
return ans;
return -1;
}
int main()
{
int a[] = { 7, 42, 11, 64 };
int n = sizeof (a) / sizeof (a[0]);
cout << rightmostNonZero(a, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int rightmostNonZero( int a[], int n)
{
int c5 = 0 ;
for ( int i = 0 ; i < n; i++)
{
while (a[i] > 0 && a[i] % 5 == 0 )
{
a[i] /= 5 ;
c5++;
}
}
for ( int i = 0 ; i < n; i++)
{
while (c5 != 0 && a[i] > 0 &&
(a[i] & 1 ) == 0 )
{
a[i] >>= 1 ;
c5--;
}
}
int ans = 1 ;
for ( int i = 0 ; i < n; i++)
{
ans = (ans * a[i] % 10 ) % 10 ;
}
if (c5 != 0 )
ans = (ans * 5 ) % 10 ;
if (ans != 0 )
return ans;
return - 1 ;
}
public static void main(String args[])
{
int a[] = { 7 , 42 , 11 , 64 };
int n = a.length;
System.out.println(rightmostNonZero(a, n));
}
}
|
Python3
def rightmostNonZero(a, n):
c5 = 0
for i in range (n):
while (a[i] > 0 and a[i] % 5 = = 0 ):
a[i] / / = 5
c5 + = 1
for i in range (n):
while (c5 and a[i] > 0 and (a[i] & 1 ) = = 0 ):
a[i] >> = 1
c5 - = 1
ans = 1
for i in range (n):
ans = (ans * a[i] % 10 ) % 10
if (c5):
ans = (ans * 5 ) % 10
if (ans):
return ans
return - 1
a = [ 7 , 42 , 11 , 64 ]
n = len (a)
print (rightmostNonZero(a, n))
|
C#
using System;
class GFG
{
static int rightmostNonZero( int [] a, int n)
{
int c5 = 0;
for ( int i = 0; i < n; i++)
{
while (a[i] > 0 && a[i] % 5 == 0)
{
a[i] /= 5;
c5++;
}
}
for ( int i = 0; i < n; i++)
{
while (c5 != 0 && a[i] > 0 &&
(a[i] & 1) == 0)
{
a[i] >>= 1;
c5--;
}
}
int ans = 1;
for ( int i = 0; i < n; i++)
{
ans = (ans * a[i] % 10) % 10;
}
if (c5 != 0)
ans = (ans * 5) % 10;
if (ans != 0)
return ans;
return -1;
}
public static void Main()
{
int [] a = { 7, 42, 11, 64 };
int n = a.Length;
Console.WriteLine(rightmostNonZero(a, n));
}
}
|
Javascript
<script>
function rightmostNonZero(a , n)
{
var c5 = 0;
for (i = 0; i < n; i++) {
while (a[i] > 0 && a[i] % 5 == 0) {
a[i] /= 5;
c5++;
}
}
for (i = 0; i < n; i++) {
while (c5 != 0 && a[i] > 0 &&
(a[i] & 1) == 0)
{
a[i] >>= 1;
c5--;
}
}
var ans = 1;
for (i = 0; i < n; i++) {
ans = (ans * a[i] % 10) % 10;
}
if (c5 != 0)
ans = (ans * 5) % 10;
if (ans != 0)
return ans;
return -1;
}
var a = [ 7, 42, 11, 64 ];
var n = a.length;
document.write(rightmostNonZero(a, n));
</script>
|
Time Complexity: O(N)
Here, N is the number of elements in the array. The rightmostNonZero() function iterates over the array once and takes constant time for each iteration.
Space Complexity: O(1)
No extra space is required.
Share your thoughts in the comments
Please Login to comment...