Digit cube | Hackerearth practice probelm solution

   Problem:-

You are provided an array 

A

 of size 

N

 that contains non-negative integers. Your task is to determine whether the number that is formed by selecting the last digit of all the N numbers is divisible by 

10

.

Note: View the sample explanation section for more clarification.

Input format

  • First line: A single integer 
    N

     denoting the size of array 

    A

  • Second line: 
    N

     space-separated integers.

Output format

If the number is divisible by 

10

, then print 

Yes

. Otherwise, print 

No

.

Constraints

1N1050A[i]105

Sample Input
5
85 25 65 21 84
Sample Output
No
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Last digit of 

85

 is 

5

.
Last digit of 

25

 is 

5

.
Last digit of 

65

 is 

5

.
Last digit of 

21

 is 

1

.
Last digit of 

84

 is 

4

.
Therefore the number formed is 

55514

 which is not divisible by 

10

.

Code:-

  Here I am going to give you two solution first one is on the basis of C language and second one is on the basis of c++ language which you can submit in c++14 and c++17 also

Solution 1 ( C language):-

#include <stdio.h>
#define MAX_N 3e6 + 14
#define LOG 50
#define MAX_S 136
int sum_of_digits(long long n) {
int sum = 0;
while (n > 0) {
sum += n % 10;
n = n / 10;
}
return sum;
}
int nxt[MAX_S][LOG];
int main() {
int t,i,j;
long long n,k;
for (j = 0; j < LOG; ++j) {
for (i = 1; i < MAX_S; ++i) {
if (j)
nxt[i][j] = nxt[nxt[i][j 1]][j 1];
else
nxt[i][0] = sum_of_digits(i * i * i);
}
}
scanf(“%d”, &t);
while (t–) {
scanf(“%lld”, &n);
scanf(“%lld”, &k);
k–;
n = sum_of_digits(n);
for (i = LOG 1; i >= 0; i)
if (k >> i & 1)
n = nxt[n][i];
printf(“%lld n”, n*n*n);
}
}

Solution 2 ( C++ language):-

 This solution is based on the c++ language and you can submit ib c++14 and c++17 also.
In this solution first three lines of the main function is only for the decreasing the time of execution of the program..
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);

This is your choice that you want to use this or not but in some cases the code may take more time in execution and that time we need it .

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define c(p) cout<<p<<“n”
#define sz(a) (ll) (a.size())
using namespace std;
const ll N = 500005;
const ll mod= 1e9 + 7;
vector<ll> v[150];
ll f(ll n)
{
    ll c=0;
    while(n>0)
    {
        c+=n%10;
        n/=10;
    }
    return c;
}
void solve()
{
    ll x=0,y=0,c=0,ans=0;
ll n,m,k;
cin>>n>>k;
assert(n>=1 and n<=1e15);
assert(k>=1 and k<=1e15);
    x=f(n);
m=sz(v[x]);
    
    if(m>k)
    {
        c(v[x][k1]);
        return;
    }
    if(m==1)
    {
        c(v[x][0]);
        return;
    }
    k -= (m2);
    k%=2;
    c(v[x][mk1]);
}
signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll n,x,c;
    for(ll i=1;i<140;++i)
    {
        n=i*i*i;
        c=0;
        std::set<ll> s;
        s.insert(n);
        v[i].pb(n);
        while(true)
        {
            x=f(n);
            v[i].pb(x*x*x);
            if(s.find(x*x*x)!=s.end())
            {
                break;
            }
            s.insert(x*x*x);
            n= x*x*x;
        }
    }
    int T;
    cin>>T;
    assert(T>=1 and T<=1e6);
    while(T–)
    {
        solve();
    }       
    return 0;                   
}

Recommended Post:-



Leave a Reply

Your email address will not be published.