# Find last k digits in product of an array numbers

Given a array size of n, find the last k digits (1 <= k < 10) of product of array numbers

Examples:

```Input  : a[] = {22, 31, 44, 27, 37, 43}
Output : 56

Input  : a[] = {24, 7, 144, 77, 29, 19}
Output : 84
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

A simple solution is to multiply all numbers, then find last k digits of product. This solution may lead overflow as the array product may be high.

A better solution is to multiply array elements under modulo of 10k

## C++

 `// CPP program to find the last k digits in ` `// product of array ` `#include ` `using` `namespace` `std; ` ` `  `// Returns last k digits in product of a[] ` `int` `lastKDigits(``int` `a[], ``int` `n, ``int` `k) ` `{ ` `    ``int` `num = (``int``)``pow``(10, k); ` ` `  `    ``// Multiplying array elements under ` `    ``// modulo 10^k. ` `    ``int` `mul = a % num; ` `    ``for` `(``int` `i = 1; i < n; i++) { ` `        ``a[i] = a[i] % num; ` `        ``mul = (a[i] * mul) % num; ` `    ``} ` `    ``return` `mul; ` `} ` ` `  `// Driven program ` `int` `main() ` `{ ` `    ``int` `a[] = { 22, 31, 44, 27, 37, 43 }; ` `    ``int` `k = 2; ` `    ``int` `n = ``sizeof``(a) / ``sizeof``(a); ` `    ``cout << lastKDigits(a, n, k); ` `    ``return` `0; ` `} `

## Java

 `// Java program to find  ` `// the last k digits in  ` `// product of array ` `import` `java.io.*; ` `import` `java.math.*; ` ` `  `class` `GFG { ` `     `  `    ``// Returns last k digits in product of a[] ` `    ``static` `int` `lastKDigits(``int` `a[], ``int` `n, ``int` `k) ` `    ``{ ` `        ``int` `num = (``int``)(Math.pow(``10``, k)); ` `     `  `        ``// Multiplying array elements  ` `        ``// under modulo 10^k. ` `        ``int` `mul = a[``0``] % num; ` `         `  `        ``for` `(``int` `i = ``1``; i < n; i++) { ` `            ``a[i] = a[i] % num; ` `            ``mul = (a[i] * mul) % num; ` `        ``} ` `        ``return` `mul; ` `    ``} ` `     `  `// Driven program ` `public` `static` `void` `main(String args[]) ` `{ ` `    ``int` `a[] = { ``22``, ``31``, ``44``, ``27``, ``37``, ``43` `}; ` `    ``int` `k = ``2``; ` `    ``int` `n = a.length; ` `     `  `    ``System.out.println(lastKDigits(a, n, k)); ` `} ` ` `  `} ` ` `  ` `  `/*This code is contributed by Nikita Tiwari.*/`

## Python 3

 `# Python 3 program to find the last ` `# k digits inproduct of array ` `import` `math ` ` `  `# Returns last k digits  ` `# in product of a[] ` `def` `lastKDigits(a, n, k) : ` `     `  `    ``num ``=` `(``int``)(math.``pow``(``10``, k)) ` `     `  `    ``# Multiplying array elements ` `    ``# under modulo 10^k. ` `    ``mul ``=` `a[``0``] ``%` `num ` `     `  `    ``for` `i ``in` `range``(``1``,n) : ` `        ``a[i] ``=` `a[i] ``%` `num ` `        ``mul ``=` `(a[i] ``*` `mul) ``%` `num ` `     `  `    ``return` `mul ` `     `  ` `  `# Driven program ` `a ``=` `[ ``22``, ``31``, ``44``, ``27``, ``37``, ``43` `] ` `k ``=` `2` `n ``=` `len``(a) ` `print``(lastKDigits(a, n, k)) ` ` `  ` `  `# This code is contributed by Nikita Tiwari. `

## C#

 `// C# program to find  ` `// the last k digits in  ` `// product of array ` `using` `System; ` ` `  `class` `GFG { ` `     `  `    ``// Returns last k digits in product of a[] ` `    ``static` `int` `lastKDigits(``int` `[]a, ``int` `n, ``int` `k) ` `    ``{ ` `        ``int` `num = (``int``)(Math.Pow(10, k)); ` `     `  `        ``// Multiplying array elements  ` `        ``// under modulo 10^k. ` `        ``int` `mul = a % num; ` `         `  `        ``for` `(``int` `i = 1; i < n; i++) { ` `            ``a[i] = a[i] % num; ` `            ``mul = (a[i] * mul) % num; ` `        ``} ` `        ``return` `mul; ` `    ``} ` `     `  `    ``// Driven program ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int` `[]a = { 22, 31, 44, 27, 37, 43 }; ` `        ``int` `k = 2; ` `        ``int` `n = a.Length; ` `         `  `        ``Console.WriteLine(lastKDigits(a, n, k)); ` `    ``} ` ` `  `} ` ` `  ` `  `// This code is contributed by vt_m. `

## PHP

 ` `

Output:

``` 56
```

Time Complexity : O(n)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Improved By : jit_t

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.