<?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: Ajay k</title>
    <description>The latest articles on DEV Community by Ajay k (@ajayk).</description>
    <link>https://dev.to/ajayk</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.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F884299%2F019377e3-4299-4520-8f2f-1b13c63bf819.png</url>
      <title>DEV Community: Ajay k</title>
      <link>https://dev.to/ajayk</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ajayk"/>
    <language>en</language>
    <item>
      <title>Best Time to Buy and Sell Stock</title>
      <dc:creator>Ajay k</dc:creator>
      <pubDate>Wed, 27 Jul 2022 18:23:23 +0000</pubDate>
      <link>https://dev.to/ajayk/best-time-to-buy-and-sell-stock-2pko</link>
      <guid>https://dev.to/ajayk/best-time-to-buy-and-sell-stock-2pko</guid>
      <description>&lt;p&gt;Today lets see one of the basic array problem,&lt;/p&gt;

&lt;p&gt;for Best Time to Buy and Sell Stock problem we need to find best day to buy (low price ) and the best day to sell (high price) so we get maximum profit &lt;/p&gt;

&lt;p&gt;The rule is simple we can't sell without buying, i.e sell should be after buying &lt;/p&gt;

&lt;p&gt;lets solve this using both java and dart &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Java&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class StockBuyAndSell {
  public static void main(String[] args) {
    int[] prices = new int[] {7, 1, 5, 3, 6, 4};
    int rightIndex=0;
    int min = Integer.MAX_VALUE;
    int maxProfit = 0;
    while(rightIndex&amp;lt;prices.length){
        if(prices[rightIndex]&amp;lt;min){
            min=prices[rightIndex];
        }
        maxProfit = Math.max(maxProfit,prices[rightIndex]-min);
        rightIndex++;
    }
    System.out.println(maxProfit);
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;here we have the stock price list &lt;code&gt;int[] prices = new int[] {7, 1, 5, 3, 6, 4};&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;lets create an pointer &lt;code&gt;int rightIndex=0&lt;/code&gt;, lets create a &lt;code&gt;min&lt;/code&gt; with &lt;code&gt;Integer.MAX_VALUE&lt;/code&gt; and &lt;code&gt;maxProfit&lt;/code&gt; with zero&lt;/p&gt;

&lt;p&gt;we are going to iterate the array from the first to last(for loop ,while loop anything is fine), the idea behind this is we need to find the min value in the array and then we will find the difference between the min and current element, and we used &lt;code&gt;Math.Max()&lt;/code&gt; to find whether current difference is maximum profit or the previous one and at last we print the maxProfit &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Dart&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; import 'dart:math';
  void main(){
    List&amp;lt;int&amp;gt; price = [7,1,5,3,6,4];
    int min=double.maxFinite.toInt();
    int maxProfit=0;
    for(int i=0;i&amp;lt;price.length;i++){
      if(min&amp;gt;price[i]){
        min=price[i];
      }
      maxProfit = max(maxProfit, price[i]-min);
    }
    print(maxProfit);
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In Dart we don't have min and max values for int so we used double and converted to int &lt;/p&gt;

&lt;p&gt;Time Complexity:- O(n)&lt;br&gt;
Auxiliary Complexity:- O(1)&lt;/p&gt;

</description>
      <category>java</category>
      <category>dart</category>
      <category>tutorial</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>Maximum difference in an array</title>
      <dc:creator>Ajay k</dc:creator>
      <pubDate>Sun, 24 Jul 2022 08:42:08 +0000</pubDate>
      <link>https://dev.to/ajayk/maximum-difference-in-an-array-2g3g</link>
      <guid>https://dev.to/ajayk/maximum-difference-in-an-array-2g3g</guid>
      <description>&lt;p&gt;Today lets look into the maximum difference in an array &lt;br&gt;
problem &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;find the maximum difference in the array array[j] - array[i],where j&amp;gt;i&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;we can solve this by using &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;O(n&lt;sup&gt;2&lt;/sup&gt;) 2 for loop method &lt;/li&gt;
&lt;li&gt;O(n) efficient solution&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;lets look into first one&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int maxDif(int[] arr) {
    int maxDif = Integer.MIN_VALUE;
    for (int i = 0; i &amp;lt; arr.length; i++) {
      for (int j = i + 1; j &amp;lt; arr.length; j++) {
        if (arr[j] - arr[i] &amp;gt; maxDif) {
          maxDif = arr[j] - arr[i];
        }
      }
    }
    return maxDif;
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we are going to create maxDif variable and initialise it with &lt;br&gt;
&lt;code&gt;Integer.MIN_VALUE&lt;/code&gt;&lt;br&gt;
next we are going to iterate the array using for loop (i loop)&lt;br&gt;
inside that i loop we are going to iterate all the elements present at the right side only due to the condition j&amp;gt;i we are going to start the j loop with i+1, for each element we are going to find the difference and if the difference is greater than the maxDif we will assign that value to it &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Time complexity:- O(n&lt;sup&gt;2&lt;/sup&gt;)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;lets look into another method&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int maxDifEfficient(int[] arr) {
    int max = Integer.MIN_VALUE;
    int min = Integer.MAX_VALUE;
    for (int i = 0; i &amp;lt; arr.length - 1; i++) {
      max = Math.max(max, arr[i + 1]); 
      min = Math.min(min, arr[i]);
    }
    return max - min;
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the logic behind this method is max difference is possible if we subtract between the largest number and the smallest number &lt;br&gt;
so we need to find the largest and smallest number in the array with j&amp;gt;i condition&lt;br&gt;
here we are trying to solve the problem in O(n) for that we are using min and max variables with values &lt;code&gt;Integer.MAX_VALUE&lt;/code&gt;, &lt;code&gt;Integer.MIN_VALUE&lt;/code&gt; respectively &lt;br&gt;
we iterate the array find which is the max and min value &lt;/p&gt;

&lt;p&gt;we find max using Math.max(max,arr[i+1]); here j is i+1 since i+1 is greater than i we satisfy the j&amp;gt;i condition &lt;/p&gt;

&lt;p&gt;and we find the min using Math.min(min,arr[i])&lt;/p&gt;

&lt;p&gt;once the loop is completed we will have the largest and smallest number in the array with respect to j&amp;gt;i condition and we can subtract that to find the maximum Difference.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Time complexity:- O(n&lt;sup&gt;2&lt;/sup&gt;)&lt;/p&gt;
&lt;/blockquote&gt;

</description>
    </item>
    <item>
      <title>Leaders in an array</title>
      <dc:creator>Ajay k</dc:creator>
      <pubDate>Tue, 19 Jul 2022 11:06:01 +0000</pubDate>
      <link>https://dev.to/ajayk/leaders-in-an-array-1peb</link>
      <guid>https://dev.to/ajayk/leaders-in-an-array-1peb</guid>
      <description>&lt;p&gt;To day let's see Leaders in array Problem &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;An element is leader if it is greater than all the elements to its right side. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;we can solve this problem using &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;two for-loop method(easy)&lt;/li&gt;
&lt;li&gt;for-loop method(efficient)
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int[] findLeader(int[] arr) {
    List&amp;lt;Integer&amp;gt; result = new ArrayList&amp;lt;&amp;gt;();
    for (int i = 0; i &amp;lt; arr.length; i++) {
      boolean isLeader = true;
      for (int j = i + 1; j &amp;lt; arr.length; j++) {
        if (arr[i] &amp;lt; arr[j]) {
          isLeader = false;
          break;
        }
      }
      if (isLeader) {
        result.add(arr[i]);
      }
    }
    return result.stream().mapToInt(Integer::intValue).toArray();
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In This method we are going to traverse the array using i loop&lt;br&gt;
,we are going to check is there any element greater than the element present in the i index by using another for loop j&lt;/p&gt;

&lt;p&gt;if we find any element greater than that we don't add that to list if not we add that element to the result&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Time Complexity: O(n&lt;sup&gt;2&lt;/sup&gt;)&lt;br&gt;
&lt;/p&gt;


&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int[] findLeaderEfficient(int[] arr) {
    List&amp;lt;Integer&amp;gt; result = new ArrayList&amp;lt;&amp;gt;();
    int currentHigh = arr[arr.length - 1];
    result.add(currentHigh);
    for (int i = arr.length - 1; i &amp;gt; 0; i--) {
      if (arr[i] &amp;gt; currentHigh) {
        result.add(arr[i]);
        currentHigh=arr[i];
      }
    }
    return result.stream().mapToInt(Integer::intValue).toArray();
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The element present at the last of the array will be in the result because we don't have any element at right side to that&lt;/p&gt;

&lt;p&gt;we declare currentHigh with the last element value&lt;br&gt;
we move from right to left&lt;br&gt;
we check is the current number is greater than currentHigh&lt;br&gt;
if yes we add that to result list and set that element as currentHigh, if not we move to the next element.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Time Complexity: O(n)&lt;/p&gt;
&lt;/blockquote&gt;

</description>
    </item>
    <item>
      <title>Left Rotate The Array</title>
      <dc:creator>Ajay k</dc:creator>
      <pubDate>Mon, 18 Jul 2022 13:26:43 +0000</pubDate>
      <link>https://dev.to/ajayk/left-rotate-the-array-78c</link>
      <guid>https://dev.to/ajayk/left-rotate-the-array-78c</guid>
      <description>&lt;p&gt;Today let's see how to left rotate the array &lt;/p&gt;

&lt;p&gt;This left rotate question can have say to rotate 1 digit to left or n digit to left lets see both in this post &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Left Rotate By 1 Digit &lt;/li&gt;
&lt;li&gt;Left Rotate by N Digit&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;Bonus:- lets look into collections built-in rotate for right and left rotate &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;lets see left rotate by 1 Digit&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int[] reverseTheArrayByOne(int[] arr) {
    int temp = arr[0];
    for (int i = 1; i &amp;lt; arr.length; i++) {
      arr[i - 1] = arr[i];
    }

    arr[arr.length - 1] = temp;
    return arr;
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since we are going to move only one digit we can store the first digit in temp and moving the rest of the elements towards left and we will add the temp to the last index.&lt;/p&gt;

&lt;p&gt;Lets see N rotate &lt;/p&gt;

&lt;p&gt;We can simply perform N rotate by running the rotate 1 digit N times to get the answer&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int[] reverseTheArrayByNTimesEasy(int[] arr, int digits) {
    for (int i = 0; i &amp;lt; digits; i++) {
      arr = reverseTheArrayByOne(arr);
    }
    return arr;
  }

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

&lt;/div&gt;



&lt;p&gt;or we can use this method&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int[] reverseTheArrayByNDigits(int[] arr, int digits) {
    int[] temp = new int[digits];
    for(int i=0;i&amp;lt;digits;i++){
      temp[i]=arr[i];
    }
    for(int i=0;i&amp;lt;arr.length-digits;i++){
        arr[i]=arr[digits+i];
    }
    for(int i=0;i&amp;lt;temp.length;i++){
        arr[digits-1+i]=temp[i];
    }
    return arr;
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To rotate N digits we can make use of temp array to store the N digits and move the remaining digits to the beginning and copy the temp array to the last&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Bonus :-  we can simply use collections rotate method to perform this array Rotate&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int n = 1; 
int[] arr = {2, 5, 8, 9, 12};
    List&amp;lt;Integer&amp;gt; arrList = 
Arrays.stream(arr).mapToObj((e) -&amp;gt; Integer.valueOf(e)).collect(Collectors.toList());
    Collections.rotate(arrList,n);
    System.out.println(arrList.toString());
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here n is the number of digits to be rotated, by default Collections.rotate will perform right rotate to if you want left rotate means you can give negative value.&lt;/p&gt;

&lt;p&gt;Collections.rotate(arrList, 1) will perform Right Rotate&lt;br&gt;
Collections.rotate(arrList,-1) Will Perform Left Rotate&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Find the Largest Number in the Array</title>
      <dc:creator>Ajay k</dc:creator>
      <pubDate>Thu, 14 Jul 2022 12:12:30 +0000</pubDate>
      <link>https://dev.to/ajayk/find-the-largest-number-in-the-array-2mm2</link>
      <guid>https://dev.to/ajayk/find-the-largest-number-in-the-array-2mm2</guid>
      <description>&lt;p&gt;Today let's see how we can find the largest number in the unsorted array &lt;/p&gt;

&lt;p&gt;lets see different solution &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;using two for loops &lt;/li&gt;
&lt;li&gt;Using max variable &lt;/li&gt;
&lt;li&gt;Using built-in sort &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Lets see the two for loop method&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int findLargestNumber(int[] arr) {
    for (int i = 0; i &amp;lt; arr.length; i++) {
      boolean flag = true;
      for (int j = 0; j &amp;lt; arr.length; j++) {
        if (arr[j] &amp;gt; arr[i]) {
          flag = false;
          break;
        }
      }
      if (flag) {
        return arr[i];
      }
    }
    return -1;
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this we are taking the array as arr &lt;br&gt;
we are going to create a for loop from zero to the length of the array (arr.length)&lt;/p&gt;

&lt;p&gt;first in the i loop we are considering the element in the index i as largest element in the array , so we are setting flag to true &lt;/p&gt;

&lt;p&gt;now in the j loop we are going to check is there any element greater than the element present in the index i &lt;/p&gt;

&lt;p&gt;if its true its going to set the flag false break the loop &lt;/p&gt;

&lt;p&gt;since the flag is false it wont enter the if block and the i will be moved to next index this will happen until the i moves to the largest element in the array when i points to the largest element the j loop wont set the flag to false so it will enter if block and will return the largest element &lt;/p&gt;

&lt;p&gt;Time complexity O(n&lt;sup&gt;2&lt;/sup&gt;)&lt;/p&gt;

&lt;p&gt;Lets now see how we can make this O(n)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int findLargestNumber2(int arr[]) {
    int max = Integer.MIN_VALUE;
    for (int i = 0; i &amp;lt; arr.length; i++) {
      if (max &amp;lt; arr[i]) {
        max = arr[i];
      }
    }
    return max;
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this method we are going to declare a variable &lt;code&gt;max&lt;/code&gt; and we initialise it with &lt;code&gt;Integer.MIN_VALUE(-2147483648)&lt;/code&gt;;&lt;/p&gt;

&lt;p&gt;now we just iterate the array with for loop and will check is the current number greater than our max, if true we are going to assign that value to max, when the loop gets over we are returning the max value&lt;/p&gt;

&lt;p&gt;Time Complexity:- O(n)&lt;/p&gt;

&lt;p&gt;new let's see using built-in sort method&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int findLargestNumber3(int[] arr){
    Arrays.sort(arr);
    return arr[arr.length-1];
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;in this we are using the java built int Arrays.sort() method which will sort us in increasing order so if we need to get the largest element it will present at the last so we are accessing that using &lt;code&gt;arr[arr.length-1]&lt;/code&gt;,you can also use this method to finding second largest element, first smallest element, second smallest element , n largest or smallest element just by changing the index values  &lt;/p&gt;

&lt;p&gt;Time Complexity:- O(n log n)&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note:- Arrays.sort() uses As of Java 8, Arrays.sort uses two sorting algorithms. One is a modification of Quicksort named dual-pivot quicksort, the other an adaptation of MergeSort named Timsort. Both have a time complexity of O(n log n)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is a part of my DSA preparation series &lt;br&gt;
Day 1 to 9 : Analysis of Algorithm &lt;br&gt;
Day 10     : &lt;a href="https://dev.to/ajayk/find-the-number-of-digits-3hkd-temp-slug-262945?preview=69487b009fffa27b767ae1bc9427e062885ae877d188448c3404371dbbc62d5bac76f36de526a59ea551e40bd642fbf91421605b12faf8c21d14d35d"&gt;Find the number of digits&lt;/a&gt;&lt;br&gt;
Day 11     : &lt;a href="https://dev.to/ajayk/check-the-given-number-is-palindrome-or-not-ga4"&gt;Check the given number is palindrome or not&lt;/a&gt;&lt;br&gt;
Day 12     : Array basics&lt;br&gt;
Day 13     : &lt;a href="https://dev.to/ajayk/find-the-largest-number-in-the-array-2mm2"&gt;Find the Largest Number in the Array&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Check the given number is palindrome or not</title>
      <dc:creator>Ajay k</dc:creator>
      <pubDate>Sun, 10 Jul 2022 14:18:26 +0000</pubDate>
      <link>https://dev.to/ajayk/check-the-given-number-is-palindrome-or-not-ga4</link>
      <guid>https://dev.to/ajayk/check-the-given-number-is-palindrome-or-not-ga4</guid>
      <description>&lt;p&gt;Today we can see how to solve &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Check the given number is palindrome or not&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;we can solve this using&lt;br&gt;
       1. mod method&lt;br&gt;
       2. String builder reverse method&lt;/p&gt;

&lt;p&gt;A number is palindrome if number and the reverse of given number is same&lt;br&gt;
   for 121 the reverse of 121 is again 121 so its palindrome&lt;br&gt;
   for 123 the reverse of 123 is 321 321 is not equal to 123&lt;br&gt;
   ,so it's not palindrome&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Mod Method
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;boolean usingModMethod(int inputNumber) {
    int reverseNumber = 0;
    int temp = inputNumber;
    while (temp &amp;gt; 0) {
      int lastDigit = temp % 10;
      reverseNumber = reverseNumber * 10 + lastDigit;
      temp=temp/10;
    }
    if (reverseNumber == inputNumber) {
      return true;
    }
    return false;
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we are going to check if the given number is a single digit number because even if we reverse 7 its going to give 7 only so we are checking that&lt;/p&gt;

&lt;p&gt;if the number is not a single digit we are going to create a reverseNumber variable with 0 as initial value,Then we create a temp value and assign the input value to it &lt;/p&gt;

&lt;p&gt;we create a while loop which is going to check is the given number is greater then Zero(0),if true we are going to enter the while loop &lt;br&gt;
inside the while loop we take the last digit of the given number, so if the input is 121 and we apply mod (%) 10 to 121 it will give the last digit which is 1&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;121 % 10 =&amp;gt; 1 (last digit)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;now we multiply the reverseNumber by 10 and we add out last digit &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;reverseNumber = ( 0 * 10 ) + 1 =&amp;gt; 1&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;now we divide the temp by 10 so 121 becomes 12 &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;NOTE:- Refer &lt;a href="https://dev.to/ajayk/find-the-number-of-digits-4ba5"&gt;Find the number of digits&lt;/a&gt; note section to know about divide in java.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;now while loop checks is 12 greater than 0 this again enters while loop we get the last digit by mod &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;12 % 10 =&amp;gt; 2 (last digit)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;now we multiply the reverseNumber by 10 and we add out last digit &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;reverseNumber = ( 1 * 10 ) + 2(last digit) =&amp;gt; 12&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;now we divide the temp by 10 so 12 becomes 1, the while loop checks is 1 greater than 0, so this again enters while loop we get the last digit by mod &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;1 % 10 =&amp;gt; 1 (last digit)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;now we multiply the reverseNumber by 10 and we add out last digit &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;reverseNumber = ( 12 * 10 ) + 1(last digit) =&amp;gt; 121 &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;now we divide the temp by 10 so 1 becomes 0, so temp is not greater than 0 and wont enter the while loop&lt;/p&gt;

&lt;p&gt;we check is the inputNumber is same as reverseNumber if its same we return &lt;code&gt;true&lt;/code&gt; else we return &lt;code&gt;false&lt;/code&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Using StringBuilder reverse
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;boolean usingStringBuilder(int inputNumber){
    String str = String.valueOf(inputNumber);
    StringBuilder br = new StringBuilder(str);
    String reverse = br.reverse().toString();
    if(str.equals(reverse)){
      return true;
    }
    return false;
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In This we are using to java StringBuilder reverse method to reverse the number and check is it same as inputNumber &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;NOTE:- Remember to convert string builder to string before compare or else you will get false always even if it same &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is a part of my DSA preparation series &lt;br&gt;
Day 1 to 9 : Analysis of Algorithm &lt;br&gt;
Day 10     : &lt;a href="https://dev.to/ajayk/find-the-number-of-digits-3hkd-temp-slug-262945?preview=69487b009fffa27b767ae1bc9427e062885ae877d188448c3404371dbbc62d5bac76f36de526a59ea551e40bd642fbf91421605b12faf8c21d14d35d"&gt;Find the number of digits&lt;/a&gt;&lt;br&gt;
Day 11     : &lt;a href="https://dev.to/ajayk/check-the-given-number-is-palindrome-or-not-ga4"&gt;Check the given number is palindrome or not&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Find the number of digits</title>
      <dc:creator>Ajay k</dc:creator>
      <pubDate>Sat, 09 Jul 2022 02:53:25 +0000</pubDate>
      <link>https://dev.to/ajayk/find-the-number-of-digits-4ba5</link>
      <guid>https://dev.to/ajayk/find-the-number-of-digits-4ba5</guid>
      <description>&lt;p&gt;To day we are going to look into the basic DSA Problem&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Find the number of digits for the given number&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;let's look how we can solve this problem in different way&lt;/p&gt;

&lt;p&gt;we assume all the input given to the program is Greater than Zero and with no leading Zeros for simplicity.&lt;/p&gt;

&lt;p&gt;For number with Leading Zeros we have Different Technique which we can see later.&lt;/p&gt;

&lt;p&gt;we are going to solve the problem in following methods &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Iteration Method&lt;/li&gt;
&lt;li&gt;Recursive Method&lt;/li&gt;
&lt;li&gt;Using Log10 Method &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's look into iterative method the most common solution&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public int countByIteration(int number) {
    int count = 0;
    while (number &amp;gt; 0) {
      ++count;
      number = number / 10;
    }
    return count;
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;consider the given input is 123, we have 3 digits &lt;br&gt;
Here we are initialise a variable count to store the number of digits&lt;br&gt;
&lt;code&gt;int count =0;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;we have create a while loop which checks is the given number is greater than Zero.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;while(number&amp;gt;0){}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;123 is greater than Zero, so it enters inside the while loop&lt;/p&gt;

&lt;p&gt;Inside the while loop we are incrementing the count value since the number (123) is not Zero (now count=1)&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;NOTE:- when you divide 2 integers in java 12/10 its 1 not 1.2(result is int), when you divide 2 double in java 12.00/10.00 its 1.2 &lt;br&gt;
not 1(result is double) and if you divide one integer and one double 12.00/10 its 1.2(result is double)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;we divide the number(123) by 10, which makes 123 to 12  &lt;/p&gt;

&lt;p&gt;now while loop is going to check is 12 greater than 0 ,since 12 is greater than 0 it again enters the while loop and increment the count value (now count = 2)&lt;/p&gt;

&lt;p&gt;we divide the number(12) by 10, which makes 12 to 1 &lt;/p&gt;

&lt;p&gt;now while loop is going to check is 1 greater than 0 ,since 1 is greater than 0 it again enters the while loop and increment the count value (now count = 3)&lt;/p&gt;

&lt;p&gt;we divide the number(1) by 10, which make 1 to 0&lt;/p&gt;

&lt;p&gt;now while loop is going to check is 0 greater than 0, since its not true while loop wont execute &lt;/p&gt;

&lt;p&gt;after while loop we have the return statement which is going to return the count(count =3)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Time Complexity : O(n) 
Auxiliary Space : O(1) 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now it's time for Recursive Method&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  public int countByRecursion(int number) {
    if (number == 0) {
      return 0;
    }
    return 1 + countByRecursion(number / 10);
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The base condition is going to check is the number is 0 or not &lt;br&gt;
if its Zero its going to return us 0 and stops the recursive call &lt;/p&gt;

&lt;p&gt;if not we are making a recursive call by dividing the number by 10 and adding that call result with 1&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Time Complexity : O(log(n)) 
Auxiliary Space : O(log(n))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the Log Method&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public int countByLog(int number) {
    return (int) Math.floor(Math.log10(number) + 1);
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Looks simple right!!! and this is also the efficient method when compared to countByRecursion and countByIteration&lt;/p&gt;

&lt;p&gt;the idea behind this is when we do log10(123) its going to give us &lt;code&gt;2.089905111439398&lt;/code&gt; if we take floor we get 2.0 and then we add 1 to it so it becomes 3.0 at last we cast that to int so it becomes 3&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Time Complexity : O(1)
Auxiliary Space : O(1)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;If you are at interview you can use the Log method.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is a part of my DSA preparation series &lt;br&gt;
Day 1 to 9 : Analysis of Algorithm &lt;br&gt;
Day 10     : &lt;a href="https://dev.to/ajayk/find-the-number-of-digits-3hkd-temp-slug-262945?preview=69487b009fffa27b767ae1bc9427e062885ae877d188448c3404371dbbc62d5bac76f36de526a59ea551e40bd642fbf91421605b12faf8c21d14d35d"&gt;Find the number of digits&lt;/a&gt;&lt;/p&gt;

</description>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
