-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathright-most-non-zero-ele.cpp
61 lines (52 loc) · 1.33 KB
/
right-most-non-zero-ele.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// written by whynespower
// linedin : whynesspower
// star mark this repo for more wonderful questions
// video explanation here https://youtu.be/27fJEAh-6wU
#include<bits/stdc++.h>
using namespace std;
class Solution{
public:
int rightmostNonZeroDigit(int n, int arr[]){
int countOf5 = 0;
long long LastDigit =1;
for(int i = 0; i < n; i++) {
while(arr[i] > 0 && arr[i] % 5 == 0) {
arr[i] = arr[i] / 5;
countOf5++;
}
}
for(int i = 0; i < n; i++) {
while(countOf5 != 0&& arr[i] > 0 && arr[i] % 2 == 0) {
arr[i] = arr[i] / 2;
countOf5--;
}
}
for(int i = 0; i < n; i++) {
LastDigit = ((LastDigit % 10)*(arr[i] % 10)) % 10;
}
if(countOf5 > 0 && LastDigit != 0)
return 5;
else if(LastDigit != 0)
return LastDigit % 10;
else
return -1;
}
};
// { Driver Code Starts.
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int A[n];
for(int i=0;i<n;++i){
cin>>A[i];
}
Solution ob;
cout << ob.rightmostNonZeroDigit(n, A) << endl;
}
return 0;
} // } Driver Code Ends