<?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: Antony</title>
    <description>The latest articles on DEV Community by Antony (@meshnesh).</description>
    <link>https://dev.to/meshnesh</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%2F65260%2Fff5e4cb4-e3fe-4df8-a61d-1dd7cfc3c9b6.jpeg</url>
      <title>DEV Community: Antony</title>
      <link>https://dev.to/meshnesh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/meshnesh"/>
    <language>en</language>
    <item>
      <title>Difference between sums of two diagonals of a square matrix</title>
      <dc:creator>Antony</dc:creator>
      <pubDate>Mon, 18 May 2020 07:54:13 +0000</pubDate>
      <link>https://dev.to/meshnesh/difference-between-sums-of-two-diagonals-of-a-square-matrix-1djj</link>
      <guid>https://dev.to/meshnesh/difference-between-sums-of-two-diagonals-of-a-square-matrix-1djj</guid>
      <description>&lt;p&gt;Given a square matrix, calculate the absolute difference between the sums of its diagonals.&lt;/p&gt;

&lt;p&gt;The most efficient way is to approach this is to implement a solution running linearly achieving the &lt;code&gt;0(n)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We will create a method &lt;code&gt;sumOfDiagonals&lt;/code&gt; which takes in a parameter &lt;code&gt;arr: Array&amp;lt;IntArray&amp;gt;&lt;/code&gt;. This allows us to create a 2D array. Create variables &lt;code&gt;leftToRight&lt;/code&gt;, &lt;code&gt;rightToLeft&lt;/code&gt; which will be equal to &lt;code&gt;0&lt;/code&gt; representing the diagonal lines. Variable &lt;code&gt;rows&lt;/code&gt; equal to the size of the array &lt;code&gt;arr.size&lt;/code&gt; and &lt;code&gt;columns&lt;/code&gt; equal to the size of the positions &lt;code&gt;arr[0].size&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Create the counters for the positions and assert them to 0. &lt;code&gt;var i = 0&lt;/code&gt;, &lt;code&gt;var j = 0&lt;/code&gt;, &lt;code&gt;var k = 0&lt;/code&gt;, &lt;code&gt;var l = arr.size - 1&lt;/code&gt; this a right to left column counter.&lt;/p&gt;

&lt;p&gt;Create a while loop that checks the rows and column counters and if the value is satisfied it should increment the counters and add the &lt;code&gt;leftToRight&lt;/code&gt;, &lt;code&gt;rightToLeft&lt;/code&gt; values together.&lt;/p&gt;

&lt;p&gt;To make it work return the absolute difference &lt;code&gt;return Math.abs(leftToRight - rightToLeft)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Easy Peasy, Lemon Squeezy 😄 😄.&lt;/p&gt;

&lt;p&gt;Let's look at the code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun sumOfDiagonals(arr: Array&amp;lt;IntArray&amp;gt;): Int {

    var leftToRight = 0
    var rightToLeft = 0
    val rows = arr.size
    val columns = arr[0].size

    var i = 0
    var j = 0
    var k = 0
    var l = arr.size - 1 //right to left column counter

    while (rows &amp;gt;= 0 &amp;amp;&amp;amp; columns &amp;gt;= 0 &amp;amp;&amp;amp; rows &amp;gt;= 0 &amp;amp;&amp;amp; l &amp;gt;= 0) {

        leftToRight += arr[i][j] //add the values at the position
        rightToLeft += arr[k][l] //add the values at the position

        i += 1
        j += 1
        k += 1
        l -= 1

    }

    return abs(leftToRight - rightToLeft) ///return the difference

}

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



&lt;p&gt;&lt;a href="https://github.com/meshnesh/codilityAlgorithims/blob/master/dailyAlgorithims/day_one/src/SumOfDiagonols.kt"&gt;Github Repo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thank you for reading, please share if you found it helpful 💯.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Asante Sana&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>kotlin</category>
      <category>datastructures</category>
      <category>learning</category>
    </item>
    <item>
      <title>Sum of Array the Maximum Consecutive Subarray</title>
      <dc:creator>Antony</dc:creator>
      <pubDate>Thu, 12 Mar 2020 10:07:53 +0000</pubDate>
      <link>https://dev.to/meshnesh/sum-of-array-the-maximum-consecutive-subarray-1hl2</link>
      <guid>https://dev.to/meshnesh/sum-of-array-the-maximum-consecutive-subarray-1hl2</guid>
      <description>&lt;p&gt;In the past few weeks, I have been learning a lot of algorithms and data structures. Finding Kotlin written algorithms has proven challenging. So let us fix that by with writing our own. if you can't beat them, look for a way to beat them 😅.&lt;/p&gt;

&lt;p&gt;The problem.&lt;/p&gt;

&lt;p&gt;Given an array of integers, find the maximum possible sum you can get from one of its contiguous subarrays. The subarray from which this sum comes must contain at least &lt;code&gt;1&lt;/code&gt; element.&lt;/p&gt;

&lt;p&gt;One of the methods to solve this will be through using a nested for loop, the first will loop through all the elements and calculating the sum and saving it to a variable &lt;code&gt;var max_sum&lt;/code&gt;. This solution would be correct although it would not be efficient as it runs at &lt;code&gt;0(n²)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;There are many dynamic algorithms that can be used to make this program efficient by running linearly. In this instance, we will use the popular &lt;strong&gt;Kadane's Algorithm&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;We will create a method &lt;code&gt;ArrayMaxConsecutiveSum&lt;/code&gt; which takes in a parameter &lt;code&gt;inputArray: IntArray&lt;/code&gt;. Then a variable &lt;code&gt;max_sum&lt;/code&gt; which will be equal to the first element of the array &lt;code&gt;var max_sum = inputArray[0]&lt;/code&gt;, variable &lt;code&gt;current_sum&lt;/code&gt; which will be equal to our maximum sum, &lt;code&gt;var current_sum = max_sum&lt;/code&gt;, a for loop, given the size of the array we will make the &lt;code&gt;current_sum&lt;/code&gt; equal to the result of the element input position of the array plus the &lt;code&gt;current_sum&lt;/code&gt;compared to the next element input position. Update the &lt;code&gt;max_sum&lt;/code&gt; with the &lt;code&gt;current_sum&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To make it work we will return the &lt;code&gt;max_sum&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now that wasn't hard 😄 😄.&lt;/p&gt;

&lt;p&gt;Let's look at the code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun ArrayMaxConsecutiveSum (inputArray: IntArray): Int {

    var max_sum = inputArray[0]
    var current_sum = max_sum

    for (i in inputArray.indices) {
        current_sum = (inputArray[i] + current_sum).coerceAtLeast(inputArray[i]) 
/**
* checks what is greater the current_sum
  plus the next element input position or
  the next element input position alone
 **/
        max_sum = current_sum.coerceAtLeast(max_sum) // we update the max sum with the current sum if it's greater
    }

    return max_sum
}

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



&lt;p&gt;&lt;a href="https://github.com/meshnesh/codilityAlgorithims/blob/master/dailyAlgorithims/day_one/src/ArrayMaxConsecutiveSum.kt"&gt;Github Repo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thank you for reading, please share if you found it helpful 💯.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Asante Sana&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>kotlin</category>
      <category>programming</category>
      <category>learning</category>
    </item>
  </channel>
</rss>
