simple Hacker Earth Problem Solution
Tags: beginners, javascript, problemsolving, programming
Published: 2023-10-23 05:12:42

it ,s simple problem but I asked to solve it during interview 

so let ,s start solving the problem:

 I have three solution for this problem:


**Problem Explanation**:
[link to the problem](https://www.hackerearth.com/practice/basic-programming/input-output/basics-of-input-output/practice-problems/algorithm/divisible-or-not-81b86ad7/)

input two line like this (the first line contain number of element , and second line contain the data)
`5
85 25 65 21 84`

 output : "Yes" or "No"

requirements :concatenate the last digit of each number and check if accept divisible on 10 if yes print "Yes" else print "No"

so in this case the last digit of 58 is 5 , last digit of 25 is 5 and so on
the result is "55514" , and this number don ,t accept divide on 10 so the result will be "No"

So before reading the solutions go and solve the [problem](https://www.hackerearth.com/practice/basic-programming/input-output/basics-of-input-output/practice-problems/algorithm/divisible-or-not-81b86ad7/) and tell me how much time you take to pass all the test cases?.

**The First solution:**

 approach to solve this problem:

-  make a variable called  `data` in this problem which contain the 
   input and convert it to array. 
-  second make a variable called `arr` to catch the the second line 
 in input or the second index from the data variable.
-  third make a variable `result` and assign it to 0.
-  loop through `arr` and update result to with the last digit of 
   number mean from e.g. 80 only take 0 and 21 only take 1.  
-  and finally check if `result` is divisible on 10 ?"Yes" :"No".

this solution pass all the test cases which the result of concat of  the last digit of all the arr numbers is not Infinity ,and also if the result is **infinity** and output is **N0**  will pass because this function will **print** "No" in all test cases which result is Infinity not because the logic of this **function** is true but because this function any way print "No" if the result is **infinity**  because **no 11** line code if divide infinity % 10 the result **NaN** mean the function will move to else condition and print "No"

So this solution is not correct  for a large number , in the problem some test cases contain more than 80,000(**Eighty Thousand**) elements ,Infinity may contain 309 digit so imagine a number of 80,000 digit will also be Infinity .and the next solution is the correct answer for a large number  , and this is the reason why we take this problem  because the result in some cases will be Infinity.

```
1.  function main (input){
2.  let data = input.split("\n");
3.  let arr = data[1].split(" ").map(String)
4.  
5.  let result = '';
6.  
7.  for(let v of arr)(
8.     result+=parseInt(v[v.length-1])
9.  ) 
10.  
11. if (parseInt(result) % 10 == 0){
12.     console.log('Yes')
13. }
14.  
15. else console.log('No')
 
}
```

**The Second solution:**

so the previous solution is not correct for large number , so instead check the whole number we check the last number 
so if for example number like this `9999999889....................1`
we only check the last digit meaning 1
instead check the whole number which in some cases infinity  
if the last number accept divide on 10 meaning the whole number accept,
and it passed all test cases.

```
function main (input){
 let data = input.split("\n");
 let arr = data[1].split(" ").map(String);
 
 let result = '';
 
 for(let v of arr){
    result += parseInt(v[v.length-1])
 } 
 /*check only the last element of number and if 
 accept divide on 10 meaning the entire 
 element accept or simply if it ,s 0*/
console.log(result[result.length-1]==0?"Yes":"No")
//console.log(result[result.length-1]%10==0?'Yes':"No")

}
```

**The Third solution:**

so in the previous solution all test cases passed ,but we can improve and reduce the code ,  we want to  check the number  accept divide on 10 but if the last digit of number accept meaning the number accept, so no need to declare a variable in previous solution called "result" and loop through arr to update the result , we only want to catch the last digit of number and check if it ,s accept divide on 10 and that is  what we did in the next code ,and it passed all the test cases.

```
function main (input){
 let data = input.split("\n");
 let arr = data[1].split(" ").map(String);

 console.log(arr[arr.length-1].slice(-1)%10==0?'Yes':"No")

}
```

can write this function like this also :
```
function main (input){
 
 let arr = input.split("\n")[1].split(" ").map(String);

console.log(arr[arr.length-1].slice(-1)==0?'Yes':"No")
 
}
```

Finally we learn how to improve  and reduce the code 
and this is a simple problem so don't spend much time on it ,
just read the solutions fast.