Header Ads Widget

Divisibility | Practice problem Hackerearth 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>
int main(){
    int N = 0;
    scanf("%d", &N);
    
    long data[N];
    for(auto i=0; i<N; i++)
     scanf("%ld", &data[i]);
    char ans[3];
    
    if (data[N-1] % 10 == 0)
        printf("Yes");
    else
        printf("No");
return 0;
}

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>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
    cout.tie(NULL);
int n,i;
cin>>n;
int a[n] , b[n];
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(i=0;i<n;i++)
{
b[i] = a[i]%10;
}
if(b[n-1] == 0)
cout<<"Yes\n";
else
cout<<"No\n";
}

Recommended Post:-




Post a Comment

1 Comments

  1. I loved the solution.
    Probably most of us were trying to build a number with the last digit of the inputs.
    But only dividing the last number, since the last digit must be zero in order to get 0 as the remainder.
    Awesome, solution.
    I really appreciate it.'

    ReplyDelete