Right most non-zero digit in multiplication of array elements
Given an array arr[] of N non-negative integers. The task is to find the right most non zero digit in the product of array elements.
Examples:
Input: arr[] = {3, 5, 6, 90909009}
Output: 7Input: arr[] = {7, 42, 11, 64}
Output: 6
Result of multiplication is 206976
So the rightmost digit is 6
Approach:
- The question is too much 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++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the rightmost non-zero // digit in the multiplication // of the array elements int rightmostNonZero(int a[], int n) { // To store the count of times 5 can // divide the array elements int c5 = 0; // Divide the array elements by 5 // as much as possible for (int i = 0; i < n; i++) { while (a[i] > 0 && a[i] % 5 == 0) { a[i] /= 5; // increase count of 5 c5++; } } // Divide the array elements by // 2 as much as possible for (int i = 0; i < n; i++) { while (c5 && a[i] > 0 && !(a[i] & 1)) { a[i] >>= 1; // Decrease count of 5, because a '2' and // a '5' makes a number with last digit '0' c5--; } } long long ans = 1; for (int i = 0; i < n; i++) { ans = (ans * a[i] % 10) % 10; } // If c5 is more than the multiplier // should be taken as 5 if (c5) ans = (ans * 5) % 10; if (ans) return ans; return -1; } // Driver code int main() { int a[] = { 7, 42, 11, 64 }; int n = sizeof(a) / sizeof(a[0]); cout << rightmostNonZero(a, n); return 0; } |
Java
// Java implementation of the approach import java.util.*; class GFG { // Function to return the rightmost non-zero // digit in the multiplication // of the array elements static int rightmostNonZero(int a[], int n) { // To store the count of times 5 can // divide the array elements int c5 = 0; // Divide the array elements by 5 // as much as possible for (int i = 0; i < n; i++) { while (a[i] > 0 && a[i] % 5 == 0) { a[i] /= 5; // increase count of 5 c5++; } } // Divide the array elements by // 2 as much as possible for (int i = 0; i < n; i++) { while (c5 != 0 && a[i] > 0 && (a[i] & 1) == 0) { a[i] >>= 1; // Decrease count of 5, because a '2' and // a '5' makes a number with last digit '0' c5--; } } int ans = 1; for (int i = 0; i < n; i++) { ans = (ans * a[i] % 10) % 10; } // If c5 is more than the multiplier // should be taken as 5 if (c5 != 0) ans = (ans * 5) % 10; if (ans != 0) return ans; return -1; } // Driver code public static void main(String args[]) { int a[] = { 7, 42, 11, 64 }; int n = a.length; System.out.println(rightmostNonZero(a, n)); } } // This code is contributed by // Surendra_Gangwar |
Python3
# Python3 implementation of the approach # Function to return the rightmost non-zero # digit in the multiplication # of the array elements def rightmostNonZero(a, n): # To store the count of times 5 can # divide the array elements c5 = 0 # Divide the array elements by 5 # as much as possible for i in range(n): while (a[i] > 0 and a[i] % 5 == 0): a[i] //= 5 # increase count of 5 c5 += 1 # Divide the array elements by # 2 as much as possible for i in range(n): while (c5 and a[i] > 0 and (a[i] & 1) == 0): a[i] >>= 1 # Decrease count of 5, because a '2' and # a '5' makes a number with last digit '0' c5 -= 1 ans = 1 for i in range(n): ans = (ans * a[i] % 10) % 10 # If c5 is more than the multiplier # should be taken as 5 if (c5): ans = (ans * 5) % 10 if (ans): return ans return -1 # Driver code a = [7, 42, 11, 64] n = len(a) print(rightmostNonZero(a, n)) # This code is contributed by Mohit Kumar |
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the rightmost non-zero
// digit in the multiplication
// of the array elements
static int rightmostNonZero(int[] a, int n)
{
// To store the count of times 5 can
// divide the array elements
int c5 = 0;
// Divide the array elements by 5
// as much as possible
for (int i = 0; i < n; i++)
{
while (a[i] > 0 && a[i] % 5 == 0)
{
a[i] /= 5;
// increase count of 5
c5++;
}
}
// Divide the array elements by
// 2 as much as possible
for (int i = 0; i < n; i++)
{
while (c5 != 0 && a[i] > 0 &&
(a[i] & 1) == 0)
{
a[i] >>= 1;
// Decrease count of 5, because a ‘2’ and
// a ‘5’ makes a number with last digit ‘0’
c5–;
}
}
int ans = 1;
for (int i = 0; i < n; i++)
{
ans = (ans * a[i] % 10) % 10;
}
// If c5 is more than the multiplier
// should be taken as 5
if (c5 != 0)
ans = (ans * 5) % 10;
if (ans != 0)
return ans;
return -1;
}
// Driver code
public static void Main()
{
int[] a = { 7, 42, 11, 64 };
int n = a.Length;
Console.WriteLine(rightmostNonZero(a, n));
}
}
// This code is contributed by
// Code_@Mech
[tabbyending]
6
Recommended Posts:
- Program for multiplication of array elements
- Arrange array elements such that last digit of an element is equal to first digit of the next element
- Count divisors of array multiplication
- Find the element whose multiplication with -1 makes array sum 0
- Find remainder of array multiplication divided by n
- Replace every array element by multiplication of previous and next
- Maximum subset sum such that no two elements in set have same digit in them
- Make the list non-decreasing by changing only one digit of the elements
- Longest subsequence such that adjacent elements have at least one common digit
- Longest subarray such that adjacent elements have at least one common digit | Set - 2
- Longest subarray such that adjacent elements have at least one common digit | Set 1
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- First digit in product of an array of numbers
- Find the most frequent digit without using array/string
- Count pairs in an array which have at least one digit common
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



