<?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: Ganesh K S</title>
    <description>The latest articles on DEV Community by Ganesh K S (@gks2611).</description>
    <link>https://dev.to/gks2611</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%2F1439136%2Fe6fad816-1308-4714-9be9-cdf5876b40e2.jpg</url>
      <title>DEV Community: Ganesh K S</title>
      <link>https://dev.to/gks2611</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/gks2611"/>
    <language>en</language>
    <item>
      <title>Length of consecutive 1's in Binary representation</title>
      <dc:creator>Ganesh K S</dc:creator>
      <pubDate>Wed, 15 May 2024 02:21:21 +0000</pubDate>
      <link>https://dev.to/gks2611/length-of-consecutive-1s-in-binary-representation-3e6e</link>
      <guid>https://dev.to/gks2611/length-of-consecutive-1s-in-binary-representation-3e6e</guid>
      <description>&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let input=110
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;output=3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In order to do this first we need to check whether the binary form of input contains consecutive or not&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcasw61375qjk7dr0cgzs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcasw61375qjk7dr0cgzs.png" alt="Image description" width="800" height="376"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;n is given input if we do binary and operation between n left shift by 1 the output if it comes non zero then it is having consecutive 1 &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;In order to find the longest consecutive ones in binary representation&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxdexadccv1sf8jtmuv6m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxdexadccv1sf8jtmuv6m.png" alt="Image description" width="800" height="573"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;first step&lt;/strong&gt; - n &amp;amp; n&amp;lt;&amp;lt;1 if it comes non zero then increment the initialized variable count to 1&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;second step&lt;/strong&gt; - if non zero number comes up then previous output will become n and again continue step until n equals to zero&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function getLongestConsecutiveOnes(n){
  let count=0

  while(n &amp;gt; 0){
    count++;
    n=(n&amp;amp;(n&amp;lt;&amp;lt;1))
  }

   return count;
}

console.log(getLongestConsecutiveOnes(110))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Left Rotate array by K elements using reverse approach</title>
      <dc:creator>Ganesh K S</dc:creator>
      <pubDate>Sun, 21 Apr 2024 12:03:37 +0000</pubDate>
      <link>https://dev.to/gks2611/left-rotate-array-by-k-elements-using-reverse-approach-bel</link>
      <guid>https://dev.to/gks2611/left-rotate-array-by-k-elements-using-reverse-approach-bel</guid>
      <description>&lt;p&gt;Given array&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const arr=[1,2,3,4,5,6]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Left Rotate array by 2 elements , Output will be ---&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;arr = [3,4,5,6,1,2]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This problem can be solve with reverse array approach easily&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First devide the array with given K elements to rotate&lt;/li&gt;
&lt;li&gt;In our case K is 2 &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F96v7k8siurl0r2fa8ebf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F96v7k8siurl0r2fa8ebf.png" alt="Image description" width="800" height="224"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reverse the corresponding devision of elements&lt;/li&gt;
&lt;li&gt;So after reversing the array will be &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzjqh3lhibxdo4zwc9plm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzjqh3lhibxdo4zwc9plm.png" alt="Image description" width="800" height="214"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Now reverse the whole array that will give you the output&lt;/li&gt;
&lt;li&gt;After reversing whole array the output will be&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbko93g2ljocddj4i6c0d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbko93g2ljocddj4i6c0d.png" alt="Image description" width="800" height="126"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Implementation in Code level&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arr=[1,2,3,4,5,6]
let k=2

function reverseArray(arr,start,end){
    while(start&amp;lt;end){
        [arr[start],arr[end]]=[arr[end],arr[start]];
        start++;
        end--;
    }

    return arr;
}

//this will be the first step to reverse the first half
let reversedFirstHalf=reverseArray(arr,0,k-1)

//this will be the second step to reverse the second half
let reversedSecondHalf=reverseArray(reversedFirstHalf,k,arr.length-1)

//After reversing the whole array
let rotateArrayOutput = reverseArray(reversedSecondHalf,0,arr.length-1)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>array</category>
      <category>rotatearray</category>
      <category>javascript</category>
      <category>programming</category>
    </item>
    <item>
      <title>Second Largest Element in an array</title>
      <dc:creator>Ganesh K S</dc:creator>
      <pubDate>Sun, 21 Apr 2024 08:04:05 +0000</pubDate>
      <link>https://dev.to/gks2611/second-largest-element-in-an-array-dbb</link>
      <guid>https://dev.to/gks2611/second-largest-element-in-an-array-dbb</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;This is the given array&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;let arr=[1,2,4,7,7,5]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Declare the two variables&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;let firstMax;
let secondMax;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Then find out the which is greater in the first 2 index values&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;if(arr[0] &amp;gt; arr[1]){
  firstMax = arr[0]
}else{
  secondMax=arr[1]
}

if(arr[1] &amp;gt; arr[0]){
  firstMax = arr[1]
}else{
  secondMax=arr[0]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then begin the loop from 2 to last index of the array&lt;/p&gt;

&lt;h2&gt;
  
  
  In the first loop
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fet2t7pga5w9vaj4do0ag.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fet2t7pga5w9vaj4do0ag.png" alt="Image description" width="800" height="447"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  In the second loop
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkjk1ldce0beox7cizy6s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkjk1ldce0beox7cizy6s.png" alt="Image description" width="800" height="426"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  In the third loop
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm80izwxlm3lsmm0v7tj2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm80izwxlm3lsmm0v7tj2.png" alt="Image description" width="800" height="439"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  In the fourth loop
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fchxaexd54acmc0hs5y6v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fchxaexd54acmc0hs5y6v.png" alt="Image description" width="800" height="419"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Complete solution to find the second largest in the array&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arr=[1,2,4,7,7,5]

let firstMax;
let secondMax;

if(arr[0] &amp;gt; arr[1]){
  firstMax = arr[0]
}else{
  secondMax=arr[1]
}

if(arr[1] &amp;gt; arr[0]){
  firstMax = arr[1]
}else{
  secondMax=arr[0]
}

for(let i=2;i&amp;lt;arr.length;i++){
  if(arr[i] &amp;gt; firstMax){
    secondMax=firstMax;
    firstMax=arr[i];
  }

  if(arr[i] &amp;gt; secondMax &amp;amp;&amp;amp; arr[i] &amp;lt; firstMax){
    secondMax = arr[i]
  }

}

console.log("firstMax",firstMax)
console.log("secondMax",secondMax)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>secondlargest</category>
      <category>array</category>
      <category>javascript</category>
      <category>programming</category>
    </item>
    <item>
      <title>Check array is sorted or not</title>
      <dc:creator>Ganesh K S</dc:creator>
      <pubDate>Sun, 21 Apr 2024 04:52:20 +0000</pubDate>
      <link>https://dev.to/gks2611/check-array-is-sorted-or-not-4c6d</link>
      <guid>https://dev.to/gks2611/check-array-is-sorted-or-not-4c6d</guid>
      <description>&lt;p&gt;This is my given array&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const arr=[1,2,6,4,5]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;You need traverse through array from the index 1 &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In each loop check the current element (&lt;strong&gt;arr[i]&lt;/strong&gt;) with previous element&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;if current element is greater than previous element then continue with loop else break the loop over there&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  In the first loop
&lt;/h2&gt;

&lt;p&gt;current element is greater than previous element so you can continue with the loop&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdttn5e979n052lpy59yi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdttn5e979n052lpy59yi.png" alt="Image description" width="800" height="289"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  In the second loop
&lt;/h2&gt;

&lt;p&gt;current element is greater than previous element so you can continue with the loop&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F56q9x4y6h0ypllrxlokv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F56q9x4y6h0ypllrxlokv.png" alt="Image description" width="800" height="297"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  In the third loop
&lt;/h2&gt;

&lt;p&gt;current element is lesser than previous element so break the loop&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fojtew3fyf48eikkdjggf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fojtew3fyf48eikkdjggf.png" alt="Image description" width="800" height="294"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Solution to check array is sorted or not in javascript&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;let checkSort = true

for(let i=1;i&amp;lt;arr.length;i++){
  if(arr[i] &amp;lt; arr[i-1]]){
    checkSort=false;
    break;
  }
}

if(checkSort){
  console.log("Array is Sorted")
}else{
  console.log("Array is not sorted)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>sort</category>
      <category>array</category>
      <category>programming</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Find the largest element within the array</title>
      <dc:creator>Ganesh K S</dc:creator>
      <pubDate>Sun, 21 Apr 2024 04:16:13 +0000</pubDate>
      <link>https://dev.to/gks2611/largest-element-in-the-array-in-javascript-1l3c</link>
      <guid>https://dev.to/gks2611/largest-element-in-the-array-in-javascript-1l3c</guid>
      <description>&lt;p&gt;this is my given array&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const arr=[5,10,4,24,13,8]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;*&lt;em&gt;largest is set to first element in the array --- largest = arr[0] *&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;You need to set the first element to largest &lt;/li&gt;
&lt;li&gt;next need to loop from 1 st index to last index &lt;/li&gt;
&lt;li&gt;in each loop check if the array value is larger than that in that case largest will be the current iterating element&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F77c083dr594smy3tegy1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F77c083dr594smy3tegy1.png" alt="Image description" width="800" height="326"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Program to find the largest element in the array&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let largest = arr[0];

for(let i=1;i&amp;lt;arr.length;i++){
  if(arr[i] &amp;gt; largest){
    largest=arr[i]
  }
}

console.log(largest)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>array</category>
      <category>programming</category>
      <category>codenewbie</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
