<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Hussein</title>
    <description>The latest articles on DEV Community by Hussein (@hussein173).</description>
    <link>https://dev.to/hussein173</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1191646%2F6c779aa9-f0bd-4717-8738-4ad35b771fd6.jpg</url>
      <title>DEV Community: Hussein</title>
      <link>https://dev.to/hussein173</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hussein173"/>
    <language>en</language>
    <item>
      <title>simple Hacker Earth Problem Solution</title>
      <dc:creator>Hussein</dc:creator>
      <pubDate>Mon, 23 Oct 2023 05:12:42 +0000</pubDate>
      <link>https://dev.to/hussein173/simple-hacker-earth-problem-solution-3nop</link>
      <guid>https://dev.to/hussein173/simple-hacker-earth-problem-solution-3nop</guid>
      <description>&lt;p&gt;it ,s simple problem but I asked to solve it during interview &lt;/p&gt;

&lt;p&gt;so let ,s start solving the problem:&lt;/p&gt;

&lt;p&gt;I have three solution for this problem:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem Explanation&lt;/strong&gt;:&lt;br&gt;
&lt;a href="https://www.hackerearth.com/practice/basic-programming/input-output/basics-of-input-output/practice-problems/algorithm/divisible-or-not-81b86ad7/"&gt;link to the problem&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;input two line like this (the first line contain number of element , and second line contain the data)&lt;br&gt;
&lt;code&gt;5&lt;br&gt;
85 25 65 21 84&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;output : "Yes" or "No"&lt;/p&gt;

&lt;p&gt;requirements :concatenate the last digit of each number and check if accept divisible on 10 if yes print "Yes" else print "No"&lt;/p&gt;

&lt;p&gt;so in this case the last digit of 58 is 5 , last digit of 25 is 5 and so on&lt;br&gt;
the result is "55514" , and this number don ,t accept divide on 10 so the result will be "No"&lt;/p&gt;

&lt;p&gt;So before reading the solutions go and solve the &lt;a href="https://www.hackerearth.com/practice/basic-programming/input-output/basics-of-input-output/practice-problems/algorithm/divisible-or-not-81b86ad7/"&gt;problem&lt;/a&gt; and tell me how much time you take to pass all the test cases?.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The First solution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&amp;nbsp;approach to solve this problem:&lt;/p&gt;

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

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

&lt;p&gt;So this solution is not correct&amp;nbsp; for a large number , in the problem some test cases contain more than 80,000(&lt;strong&gt;Eighty Thousand&lt;/strong&gt;) 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&amp;nbsp; , and this is the reason why we take this problem&amp;nbsp; because the result in some cases will be Infinity.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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')

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The Second solution:&lt;/strong&gt;&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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")

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The Third solution:&lt;/strong&gt;&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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")

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;can write this function like this also :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function main (input){

 let arr = input.split("\n")[1].split(" ").map(String);

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

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

</description>
      <category>beginners</category>
      <category>javascript</category>
      <category>problemsolving</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
