Header Ads Widget

Digit cube | Hackerearth practice probelm solution


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.


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

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.


  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];
nxt[i][0] = sum_of_digits(i * i * i);
scanf("%d", &t);
while (t--) {
scanf("%lld", &n);
scanf("%lld", &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..

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;
    return c;
void solve()
    ll x=0,y=0,c=0,ans=0;
ll n,m,k;
assert(n>=1 and n<=1e15);
assert(k>=1 and k<=1e15);
    k -= (m-2);
signed main() {
    ll n,x,c;
    for(ll i=1;i<140;++i)
        std::set<ll> s;
            n= x*x*x;
    int T;
    assert(T>=1 and T<=1e6);
    return 0;                   

Recommended Post:-

Post a Comment