<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by kelli davis on Medium]]></title>
        <description><![CDATA[Stories by kelli davis on Medium]]></description>
        <link>https://medium.com/@kellzcodes?source=rss-b5005cc6aedd------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*aAZGq7JTV3VCCvqLg2g_sA.jpeg</url>
            <title>Stories by kelli davis on Medium</title>
            <link>https://medium.com/@kellzcodes?source=rss-b5005cc6aedd------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 03:55:32 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kellzcodes/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[30 Day Leet Code Challenge #1 Single Number Java Solution]]></title>
            <link>https://kellzcodes.medium.com/30-day-leet-code-challenge-1-single-number-java-solution-ae72b8431ecd?source=rss-b5005cc6aedd------2</link>
            <guid isPermaLink="false">https://medium.com/p/ae72b8431ecd</guid>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[kelli davis]]></dc:creator>
            <pubDate>Fri, 03 Apr 2020 21:00:49 GMT</pubDate>
            <atom:updated>2020-04-04T02:59:44.358Z</atom:updated>
            <content:encoded><![CDATA[<p>This is my solution to <a href="https://leetcode.com/problems/single-number/">problem 136</a> from Leetcode.com.</p><p>Given a <strong>non-empty</strong> array of integers, every element appears <em>twice</em> except for one. Find that single one.</p><p><strong>Note:</strong></p><p>Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?</p><p><strong>Example 1:</strong></p><pre><strong>Input:</strong> [2,2,1]<br><strong>Output:</strong> 1</pre><p><strong>Example 2:</strong></p><pre><strong>Input:</strong> [4,1,2,1,2]<br><strong>Output:</strong> 4</pre><p>First, we count the number of integers in the array.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/282/1*iiGCNImUqS6dYS_6UZT7Ew.png" /><figcaption>Count the number of integers in the array</figcaption></figure><p>Then declare a variable named “result” and set it equal to the first integer in the array. We will use it to compare to the other integers in the array.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/232/1*7wCLJ3pjILVqNWd0qjam2w.png" /><figcaption>Isolate the first number in the array</figcaption></figure><p>Now we use a <a href="https://en.wikipedia.org/wiki/For_loop">for-loop</a> to traverse through the entire array starting at the second index, which is index 1 (remember, the first number in an array is index 0). This way, we will be able to compare the first integer to the second one and every number after.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/307/1*86iaK5AGXGFgbcI3w4Uhxg.png" /><figcaption>Traverse through the entire array starting from the second number</figcaption></figure><p>Next, we will use an <a href="https://en.wikipedia.org/wiki/XOR_gate">XOR gate</a> to find a number without a match. XOR or Exclusive Or is a logical operation that outputs true only when inputs differ (for example: one is true, the other is false). We will use xor to compare result to each number in the array. The value of result will change with each iteration of the for-loop until the single number is found.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/297/1*aW3Q5_Q8EsCqdvTTBHe66A.png" /></figure><p>In this case, xor will do a mathematical operation to isolate the single number. The program will traverse through the array adding each number to the result until it gets to a familiar digit. Instead of adding the number again, the program will subtract the number from result. When the for-loop has completed, the result will be the number that was added but not subtracted.</p><p>Lastly, return the result.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/150/1*XX0xXSWQ9_3iikxCR9vcJQ.png" /><figcaption>Return the “true” integer that does not appear twice</figcaption></figure><p>Let’s see this in action on the second array example above:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/337/1*ZtCGcwMe_aOyzSsb9J6wNA.png" /></figure><p>First we count the integers in the array and declare it in a variable named “size”. Then declare a variable named “result” and set it equal to the first number in the array.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/332/1*urBgZO8J8b4_clYvHr-yUg.png" /><figcaption>int size = nums.length; int result = nums[0];</figcaption></figure><p>Now we enter the for-loop. We start at index 1 whose value is 1 (Index is denoted by the letter i). Because result is equal to the xor comparison of all the elements in the array, the program will add 1 to the result. The new value of the result will be 5.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/316/1*Avoy-xzPVbS5thGnlNibAg.png" /></figure><p>Back to the beginning of the loop, index 2 of the array contains the number 2. The program will add 2 to the result. The new value of result is 7.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/340/1*7-cOyB-1wBZquDR-WMUAhw.png" /></figure><p>The loop starts again but this time we have approached a number that has already been added. The number 1 is at index 3. The program will subtract this number from the result.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/317/1*JEZAUTXzyv9FITEsMtb-ag.png" /></figure><p>Now we are at the last iteration of the for-loop. Index 4 contains the number 2. The xor gate will subtract the number 2 from the result. The final result is 4, which is the single number in the array.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/342/1*Gomqz6akNYPvYuC8CmxNxA.png" /></figure><p>Lastly, the program will return the result.</p><p>You can find the <a href="https://github.com/keldavis/Java-Practice/blob/master/Algorithms/30%20Day%20LeetCoding%20Challenge/SingleNumber.java">source code</a> with notes on my <a href="https://github.com/keldavis">github</a> profile.</p><p>I’m raising $7k for my college tuition. Please help me out by donating and/or sharing my <a href="https://www.gofundme.com/f/2r48r-help-me-continue-my-college-education?utm_source=customer&amp;utm_medium=copy_link-tip&amp;utm_campaign=p_cp+share-sheet">gofundme</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ae72b8431ecd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Solve Diagonal Traverse Algorithm in Java]]></title>
            <link>https://kellzcodes.medium.com/how-to-solve-diagonal-traverse-algorithm-in-java-2cd91385e156?source=rss-b5005cc6aedd------2</link>
            <guid isPermaLink="false">https://medium.com/p/2cd91385e156</guid>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[data-structures]]></category>
            <dc:creator><![CDATA[kelli davis]]></dc:creator>
            <pubDate>Wed, 01 Apr 2020 18:24:28 GMT</pubDate>
            <atom:updated>2020-04-01T18:29:14.700Z</atom:updated>
            <content:encoded><![CDATA[<p>This is my solution to <a href="https://leetcode.com/problems/diagonal-traverse/">problem 498</a> on Leet Code.</p><p>Given a matrix of M x N elements (M rows, N columns), return all elements of the matrix in diagonal order as shown in the below image.</p><p><strong>Example:</strong></p><pre><strong>Input:</strong><br>[<br> [ 1, 2, 3 ],<br> [ 4, 5, 6 ],<br> [ 7, 8, 9 ]<br>]</pre><pre><strong>Output:</strong>  [1,2,4,7,5,3,6,8,9]</pre><pre><strong>Explanation:</strong></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/399/0*8Vx99pNNEn7jfY6Q.png" /></figure><p><strong>Note: </strong>The total number of elements of the given matrix will not exceed 10,000.</p><p>First, we check if the matrix is empty then return an empty array. This is our first <a href="https://en.wikipedia.org/wiki/Edge_case">edge case</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/272/1*NIPU8J1KLsjuev0q1qg_kw.png" /><figcaption>Return an empty array if the matrix is empty</figcaption></figure><p>Next, we initialize the matrix variables. <a href="https://programming.guide/java/matrices-and-multidimensional-arrays.html">Matrices</a> have rows and columns that are treated just like indices in an array. Our first variables are called row and column and are initialized to 0 (the first column and first row of our matrix). This will help us to pinpoint the exact index in our matrix.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/251/1*qo42IXZQs6jS5F4f8QEJNQ.png" /></figure><p>Initialize variables that represent the column and row lengths. This will help us to traverse through the matrix.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/317/1*-m75Lp6MqKv7m3kADvU8Lw.png" /></figure><p>Initialize a new array to store the matrix elements. We want the array to be big enough so we multiply the matrix row length by column length to find the right size for our new array.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/364/1*-ssKCrtxOAxHlHa3pW9TtA.png" /></figure><p>Next, we create a <a href="https://www.cs.utah.edu/~germain/PPS/Topics/for_loops.html">for-loop</a> to traverse through the new array so we can store the elements from our matrix.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/661/1*MQzzN3nQrcRm0K9dpm4aFA.png" /><figcaption>Go through the whole array starting at the beginning</figcaption></figure><p>At each iteration of the for-loop, the current array index will be set to the current matrix index. The current matrix index will be indicated by the row and column variables.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/412/1*Sx6jG4KsUnDhxot6gRIKZw.png" /></figure><p>Now, for our first <a href="https://www.computerhope.com/jargon/i/ifstatme.htm">if-statement</a>: if the sum of the current row and column index is even…..</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/276/1*fX05dc2Y--lnIWyzgI1jYQ.png" /></figure><p>If the current column is the last column, in the matrix, move to the next row.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/364/1*xqeJdUs9-EZc2DMN2Oxc_w.png" /></figure><p>If the the current row is the first row in the matrix, go to the next column.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/319/1*9hpvJ6zLR6RcPY_3gHi2-w.png" /></figure><p>Otherwise, go back a row and move to the next column.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/361/1*ZZBfS-s2BM4lLHw95l-5TA.png" /></figure><p>If the sum of the current row and column index is not even, we can just write else. Inside our else statement….</p><p>If the current row is the last row in the matrix, move to the next column.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/277/1*NNHHsQyovuSLsU02X8B6jA.png" /></figure><p>If the current column is the first column in the matrix, move to the next row.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/292/1*SVLrkWGgaloV8yxSpjRaeQ.png" /></figure><p>Otherwise, go to the next row then go to the previous column.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/269/1*deF32OXa1W5G8Q6aHKxMWA.png" /></figure><p>Finally, we return the array.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/147/1*YUUY8rzHBn_zsYsXpCQSlA.png" /></figure><p>Let’s see this in action on the example matrix from above.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/307/1*cyoSBDiDqrT9xCxIRgF63Q.png" /></figure><p>First we check to see if the matrix is empty. The matrix has 9 integers, so it is not empty. Now we move to the next line where we initialize the variables for the rows and columns. Remember, row and column will indicate the specific index of the matrix. The length of each row and column will be indicated by the variables called mRow and mColumn.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/346/1*7PepgniYh3yt-LmPwyrbQA.png" /><figcaption>Initialize matrix variables</figcaption></figure><p>Next, we start a for-loop to build our array.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/451/1*JgXMEGrJZbEZyCudd8iZCw.png" /><figcaption>for (int currentIndex = 0; currentIndex &lt; array.length; currentIndex++)</figcaption></figure><p>The current index of the array is equal to the current matrix index. The current matrix index is indicated by the row and column variables. Both row and column were initialized to 0. The current index of the array is zero. So we change the integer at the first index of the array to the integer at the first column and row in the matrix, which is 1.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/764/1*uAOGh35U2jxtILokyQY15g.png" /><figcaption>array[currentIndex] = matrix[row][column];</figcaption></figure><p>Next, we go through the matrix to determine the next integer to add the array. We check to see if the sum of the current row and column indices is even or odd. Both column and row are equal to 0. Zero is an even number.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/341/1*TWHOekQGfkzNPGkqMx_-rA.png" /><figcaption>if ((row + column) % 2 == 0)</figcaption></figure><p>We then check to see if the current column is the last column in the matrix. It is not. It is the first column. Is the current row the first row? Yes! So we move to the next column by an increment of 1.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/321/1*fP7cf3dn1HZxyl-BdAD97A.png" /><figcaption>if (row == 0) column++;</figcaption></figure><p>Now, we go through the loop again. The current array index is 1.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/135/1*VUjwupiB25DJPqrbt5uObg.png" /></figure><p>The value of the value of the current array index is equal to the value of the current matrix index.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/446/1*x8U6_lc4srXUrxaespp5kA.png" /><figcaption>array[currentIndex] = matrix[row][column];</figcaption></figure><p>The currentIndex of the array is 1. We are at row[0] and column[1] in our matrix. Let’s change the value at array[1] from 0 to the value at matrix[0][1], which is 2.</p><p>Next, we check to see if the sum of the current row and column is even or odd.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/215/1*LEXSALGDmM2dXQvFmiFhJA.png" /><figcaption>if ((row + column) % 2 == 0) OR else</figcaption></figure><p>The sum of the current row and column is 1. Since 1 is an odd number, we skip down to the else statement.</p><p>Now we check to see if the current row is the last row or if the current column is the is the first column. Since it is neither, we go to the the final else statement which says to go to the next row and previous column.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/222/1*i4NYatVbqaO5Ljh4vcaTHA.png" /><figcaption>else{row++; column - -;}</figcaption></figure><p>Back to the top, we set the current array index to the value at the current matrix index.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/433/1*Mxm9octalHUe_755a656Uw.png" /><figcaption>array[currentIndex] = matrix[row][column];</figcaption></figure><p>Because currentIndex equals 2, we are changing the value of the number in array[2]. To find this value, we go to the matrix at row 1, column 0. The value at matrix[1][0] is 4. Now we put the number 4 in array[2].</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/463/1*ADHJREOSKBqYblXm1u-iHQ.png" /></figure><p>Again, we check to see if the sum of the current row and column are even or odd. The sum of row and column is 1, which is an odd number. Then we check to see if we are either at the last row or the first column in the matrix.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/225/1*-BKxd3qeWcgQ0RNV7ibO8g.png" /><figcaption>if(row == mRow — 1)…else if (column == 0)</figcaption></figure><p>We aren’t at the last row, but we are at the first column. In this case, we go to the next row and stay in the same column.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/222/1*jLScfwwHEX0dnUpjB8S6WQ.png" /><figcaption>else if (column == 0){row++;}</figcaption></figure><p>Now we are at the 3rd index of the array. We are going to put whatever value that’s in the current matrix slot into array[3].</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/427/1*H4_sCdSX4drapHNfjSFZ2A.png" /></figure><p>Lets go to row 2, column 0 in the matrix. There, we find the number 7. So 7 is what will go in array[3].</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/451/1*9HN9IV8RAQLh-lnNsORJ5Q.png" /><figcaption>array[currentIndex] = matrix[row][column];</figcaption></figure><p>Again, we check to see if the sum of the current row and column is even or odd. The sum is 2 which is even. Then we check to see if we are either in the last column or first row.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/218/1*4wwxf5hwOeKwAtnwmb0t4Q.png" /><figcaption>if ((row + column) % 2 == 0)</figcaption></figure><p>Since we are in neither the first row nor the last column, we go back a row and move to the next column.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/217/1*tCpoWtwR6Vjs-vSP_8S4Tw.png" /><figcaption>else{row- -; column++}</figcaption></figure><p>Back to the beginning of the for-loop. We are at the 4th index of the array. This means we change the value of array[4] to matrix[1][1]. The new value at array[4] is 5.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/458/1*ON9sAAjEldQqqS4_VVKP2A.png" /><figcaption>array[currentIndex] = matrix[row][column];</figcaption></figure><p>Next, we check to see if the sum of the current row and column is even or odd. The sum is even. Then we check to see if we are at either the last column or the first row. We are at neither so we go back one row and move forward one column.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/223/1*wx5WE4jTANUJ-BoC38yAKA.png" /><figcaption>else{row- -; column++}</figcaption></figure><p>At matrix[0][2], our current matrix index, we find the number 3. The number 3 will go into our current array index which is 5.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/453/1*MLX58ACkXXlbkqBBUw8eeA.png" /><figcaption>array[currentIndex] = matrix[row][column];</figcaption></figure><p>The sum of the current row and column is even. We are at the last column in the matrix. Now we move to the next row and stay in the same column.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/237/1*iydpTkkt5X462p4McRKTzA.png" /><figcaption>if (column == nColumn — 1){row++;}</figcaption></figure><p>The value at matrix[1][2] is 6. This will go into array[6].</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/443/1*rlgjMg4ZCqCHjEl8udN3Ug.png" /><figcaption>array[currentIndex] = matrix[row][column];</figcaption></figure><p>The sum of the current row and column is odd. We are at neither the last row nor the first column in the matrix. So we go to the next row and the previous column.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/225/1*GnefJbmyQRMVQilqA1QuBQ.png" /><figcaption>else{row- -; column++;}</figcaption></figure><p>The value at matrix[2][1] is 8, which will go into array[7].</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/445/1*t7w1PEzXvEHazT5Fm4p88g.png" /><figcaption>array[currentIndex] = matrix[row][column];</figcaption></figure><p>The sum of the current row and column is odd. We are at the last row so we move to the next column and stay in the same row.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/227/1*7Oo6mPWLpvw9l3BwCg7hOw.png" /><figcaption>if(row == mRow — 1){column++;}</figcaption></figure><p>We are now at the final index. The value at matrix[2][2] is 9. The number 9 goes into array[8].</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/466/1*Q2q96LoAm1YUB8Y8NjsjfA.png" /><figcaption>array[currentIndex] = matrix[row][column];</figcaption></figure><p>The sum of the current row and column is even. We are at the last column in the matrix. The row variable will increment by 1.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/225/1*Q-TUzib7gfaswHRwaSikgQ.png" /><figcaption>if (column == nColumn — 1){row++;}</figcaption></figure><p>The program will go back to the beginning of the for-loop. The array is finished so we skip down to the line of code that calls for the return of the array.</p><p>Thanks for reading. Check out the source code on my <a href="https://github.com/keldavis/Java-Practice/blob/master/Algorithms/Arrays/DiagonalTraverse.java">github</a>.</p><p>I’m raising $7k to for my college tuition. Please help by donating and/or sharing my <a href="http://gf.me/u/xuiny7">gofundme</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2cd91385e156" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Solve Plus One Algorithm in Java]]></title>
            <link>https://kellzcodes.medium.com/how-to-solve-plus-one-algorithm-in-java-a7e527cb85d5?source=rss-b5005cc6aedd------2</link>
            <guid isPermaLink="false">https://medium.com/p/a7e527cb85d5</guid>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[coding]]></category>
            <dc:creator><![CDATA[kelli davis]]></dc:creator>
            <pubDate>Mon, 25 Feb 2019 18:29:46 GMT</pubDate>
            <atom:updated>2021-06-23T02:10:38.955Z</atom:updated>
            <content:encoded><![CDATA[<p>This is my solution to <a href="https://leetcode.com/problems/plus-one/">problem 66</a> on Leet Code.</p><p>Given a <strong>non-empty</strong> array of digits representing a non-negative integer, plus one to the integer.</p><p>The digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a single digit.</p><p>You may assume the integer does not contain any leading zero, except the number 0 itself.</p><p><strong>Example 1:</strong></p><pre><strong>Input:</strong> [1,2,3]<br><strong>Output:</strong> [1,2,4]<br><strong>Explanation:</strong> The array represents the integer 123.</pre><p><strong>Example 2:</strong></p><pre><strong>Input:</strong> [4,3,2,1]<br><strong>Output:</strong> [4,3,2,2]<br><strong>Explanation:</strong> The array represents the integer 4321.</pre><p>First, we find out how many elements are in the array.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/323/1*LdF9ZL6pMsXsq8vHRZCQag.png" /><figcaption>find out how long the array is</figcaption></figure><p>Our objective is to add 1 to the last digit. So we traverse through the array backwards using a <a href="https://www.cs.utah.edu/~germain/PPS/Topics/for_loops.html"><strong>for loop</strong></a><strong>. </strong>Let’s call the variable of our <strong>for loop</strong> currentIndex.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/730/1*om-yHnI3LD8lazS4BJaqyQ.png" /><figcaption>Traverse through the array backward</figcaption></figure><p>Every element in the array must be a single digit. So we need to see if the digit is less than 9. If the digit at the currentIndex is less than 9, add (increment) 1, then return the array.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/328/1*SkG71Cuzi2HPsWpNb29c3w.png" /><figcaption>if the digit at the current index is less than 9, add 1 to it then return the array</figcaption></figure><p>The objective is to return the digits incremented by one. If the digit at the currentIndex is not less than 9, change the number to zero.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/278/1*YF4YniKxUEKzW1mIvXqPZQ.png" /><figcaption>If the number at the current index is not less than 9, change the digit to zero</figcaption></figure><p>Just like a regular math problem, we change 9 to 0 then carry the one to the next digit. This means we go through the for loop again and check the next number. If the next number is less than 9, we will just add 1 and return the entire array.</p><p>What if every number in the array is 9? This is our <a href="https://en.wikipedia.org/wiki/Edge_case">edge case</a>. If every number in the array is 9, we change every number to zero and put them in a new array with space for one more element.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/452/1*zwUaZInLgUiYA93nHAIVWw.png" /><figcaption>array.length() will return all zeros by default</figcaption></figure><p>Next, we change the first element to 1. That is how we carry the one.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/205/1*uokbQVN_Aoe8VTqkb7oQYw.png" /><figcaption>Make the digit at the first index 1</figcaption></figure><p>Finally, we return the new array.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/186/1*HbIOxwj5lTg9Hde_1gJCfA.png" /><figcaption>return the new array</figcaption></figure><p>Let’s see this in action on the first array example above:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/245/1*fhI9wW0gqlhUV-oAHva4kw.png" /><figcaption>int[] digits = new int[]{1,2,3};</figcaption></figure><p>First, we find out how many elements are in the array and save this value in a variable called arrayLength.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/227/1*tiJGcWAyK6GDoRSz2jvxaw.png" /><figcaption>int arrayLength = digits.length;</figcaption></figure><p>Next, we enter the for loop by traversing backwards. So we start at index 2.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/269/1*9vegLDv3Uv5V5eIu1KmP5w.png" /><figcaption>for (int currentIndex = arrayLength — 1; currentIndex &gt;= 0; currentIndex- -)</figcaption></figure><p>Now we ask if the number at the currentIndex is less than 9. It is. The number at the currentIndex is 3.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/209/1*dN7WWxj_7KJv8jwXcGwePA.png" /><figcaption>if (digits[currentIndex] &lt; 9)</figcaption></figure><p>Next, we add one to the number at the currentIndex then return the array. The digits array is now [1, 2, 4].</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/216/1*7zXJvTZPm0Qv4EPiHnHN4A.png" /><figcaption>digits[currentIndex]++;</figcaption></figure><p>Let’s see this in action on our edge case with an array of [9,9]. We want to return array [1, 0, 0].</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/189/1*9KvkubUdNdRpEDjlxwXi5g.png" /><figcaption>int[] digits = new int[]{9, 9};</figcaption></figure><p>First, we find that the array has 2 elements and save the value in our arrayLength variable.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/139/1*AqqlgmV6bja-yRw7PwLxpQ.png" /><figcaption>int arrayLength = digits.length;</figcaption></figure><p>Now we use the <strong>for loop</strong> to traverse through the array backwards starting at index 1.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/272/1*Tzt_0pExXCF89yvfxpt8qA.png" /><figcaption>for (int currentIndex = arrayLength — 1; currentIndex &gt;= 0; currentIndex- -)</figcaption></figure><p>Next, we check to see if the digit at the currentIndex is less than 9. It is not. so we change the digit to 0.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/191/1*E3FW8hGVca2nQMmZbMuEig.png" /><figcaption>digits[currentIndex] = 0;</figcaption></figure><p>We go through the <strong>for loop</strong> again at index 0.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/199/1*CN0Uj3PZutJEtgqsw7U16A.png" /></figure><p>The number at currentIndex is not less than 9. So we change it to zero.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/178/1*CKCloJMxs7HMAbBZIHpe-g.png" /></figure><p>We have reached the end of our <strong>for loop</strong>. Now we make a new array with the numbers from digits and add space for one more element.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/327/1*Vq2Fg7_uADpcwuVxUlIELQ.png" /><figcaption>int[] newDigits = new int [arrayLength + 1];</figcaption></figure><p>Next, we carry the one by changing the first digit in the new array to 1. Then we return the new array which is [1, 0, 0]</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/211/1*dZJDk5_KSBaBswgXbW46Pg.png" /></figure><p>You can find the <a href="https://github.com/keldavis/Java-Algorithm-Practice/blob/master/src/Leet%20Code/src/Arrays/plusOne/PlusOne.java">source code</a> with notes on my github profile..</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a7e527cb85d5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to solve Largest Number At Least Twice of Others Algorithm in Java]]></title>
            <link>https://kellzcodes.medium.com/how-to-solve-largest-number-at-least-twice-of-others-algorithm-in-java-b41323428684?source=rss-b5005cc6aedd------2</link>
            <guid isPermaLink="false">https://medium.com/p/b41323428684</guid>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[kelli davis]]></dc:creator>
            <pubDate>Sun, 24 Feb 2019 08:48:50 GMT</pubDate>
            <atom:updated>2020-04-01T18:50:09.362Z</atom:updated>
            <content:encoded><![CDATA[<p>This is my solution to <a href="https://leetcode.com/problems/largest-number-at-least-twice-of-others/">problem 747</a> on Leet Code.</p><p>In a given integer array nums, there is always exactly one largest element.</p><p>Find whether the largest element in the array is at least twice as much as every other number in the array.</p><p>If it is, return the <strong>index</strong> of the largest element, otherwise return -1.</p><p><strong>Example 1:</strong></p><pre><strong>Input:</strong> nums = [3, 6, 1, 0]<br><strong>Output:</strong> 1<br><strong>Explanation:</strong> 6 is the largest integer, and for every other number in the array x,<br>6 is more than twice as big as x.  The index of value 6 is 1, so we return 1.</pre><p><strong>Example 2:</strong></p><pre><strong>Input:</strong> nums = [1, 2, 3, 4]<br><strong>Output:</strong> -1<br><strong>Explanation:</strong> 4 isn&#39;t at least as big as twice the value of 3, so we return -1.</pre><p><strong>Note:</strong></p><ol><li>nums will have a length in the range [1, 50].</li><li>Every nums[i] will be an integer in the range [0, 99].</li></ol><p>First, we check to see if the array is empty. If so, return -1. This is the first <a href="https://en.wikipedia.org/wiki/Edge_case">edge case</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/251/1*NctAIJ0W6tJVFUAWUjBWLQ.png" /><figcaption>return -1 if the array is empty</figcaption></figure><p>Next, we check to see if there is only one element in the array. If so, we return 0 because the number at index zero would technically be twice as large as any other element in the array.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/316/1*Hd2PnJRcuM89Z1QeP2Bc2Q.png" /><figcaption>return 0 if the array has one element</figcaption></figure><p>Now, we need a <a href="https://www.cs.utah.edu/~germain/PPS/Topics/for_loops.html"><strong>for loop</strong></a> to traverse through the array while comparing the numbers to each other. We want to find the largest number so we need to make a variable called largestNumber. The default position of largestNumber will be at index 0 so we can compare it to the other integers in the array.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/293/1*WEYUYEn18XTSdZ5PPpbTeQ.png" /><figcaption>The largest number in the array starts at index 0</figcaption></figure><p>We want to compare largestNumber to the other numbers. We also want to make sure that it is at least twice the value of every other number. This means it has to be at least twice the value as the second largest integer in the array. We can store this number in a variable called secondLargestNumber. Let’s make its initial value the smallest number an integer can be. This will be a placeholder until we give it a real value in the <strong>for loop</strong>. Since the integer array cannot contain a value smaller than Integer.MIN_VALUE, the code works across all edge cases. The default position of secondLargestNumber is index 1 so we can compare it to largestNumber at index 0.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/426/1*fxIgA5YcLxBipHdIL-VK_w.png" /><figcaption>This number will be compared to the largest number, its current <a href="https://stackoverflow.com/a/30685679/11019241">value</a> is a placeholder</figcaption></figure><p>The objective is to return the <strong>index </strong>with the largest number. We can track that index in a variable called dominantIndex. The default position of dominantIndex will be index 0.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/234/1*z1ZHg9NJxRMfKeW4kMgb8A.png" /><figcaption>The dominant index contains the largest number in the array</figcaption></figure><p>Now we can start our <strong>for loop. </strong>Let’s call the variable of the loop currentIndex. The default position of currentIndex will be 1 so we can compare its value to largestNumber.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/699/1*JD1KwnCulsWv0XfRVshJew.png" /><figcaption>Traverse through the array starting at index 1</figcaption></figure><p>At the start of each loop, we want to save the value of currentIndex so we can compare it to largestNumber. We will store this value in a variable called currentNumber.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/385/1*G-O1fBZC5t3SICWZ0Qw8Pg.png" /><figcaption>The current number is the value at the current index</figcaption></figure><p>If the currentNumber is greater than the largestNumber:</p><ol><li>The largestNumber becomes the secondLargestNumber</li><li>The currentNumber becomes the largestNumber</li><li>The currentIndex becomes the dominantIndex</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/417/1*gjY0aRaLEvWh6Car9w2Gfg.png" /><figcaption>If the current number is greater than the largest number, it then becomes the largest number</figcaption></figure><p>If the currentNumber is less than the largestNumber but is greater than the secondLargestNumber, the currentNumber becomes the secondLargestNumber.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/479/1*fWmtwLiN8iMXXMHhxRnWCA.png" /><figcaption>If the current number is greater than the second largest number, The current number becomes the second largest number</figcaption></figure><p>When the <strong>for loop </strong>is finished<strong> </strong>with the array, return the dominant index only if the largestNumber is twice as much as the secondLargestNumber.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/481/1*3KPu8Adl_MzqdBvetRBajA.png" /><figcaption>Return the dominant index if the largest number is greater than or equal to twice the second largest number</figcaption></figure><p>Finally, just return -1 if the largest number in the array is not twice the size as the second largest number.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/125/1*Hn4Wj-o4-Mr1yucjFii3Zg.png" /><figcaption>Otherwise, return -1</figcaption></figure><p>Let’s see it in action on the first array example above.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/268/1*486sANCuv5kzXCc-QNvNWw.png" /></figure><p>First, we ask if the array is empty. It is not empty so we move on. Then we ask if the array contains one integer. The array contains 4 integers so we move on again.</p><p>The largestNumber integer is set to the number at index 0, which is 3.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/371/1*PcMgDeKeX0vSSMN0bosWfQ.png" /><figcaption>int largestNumber = nums[0];</figcaption></figure><p>The secondLargestNumber is set to the smallest possible integer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/221/1*7Z6SjIfQbxxo_P2rmsbHoQ.png" /><figcaption>int secondLargestNumber = Integer.MIN_VALUE;</figcaption></figure><p>The dominantIndex is set to 0 by default. Remember, this is the index that holds the largestNumber.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/304/1*ILEyJoldGnGOxuh-tg7eVQ.png" /><figcaption>int dominantIndex = 0;</figcaption></figure><p>Now we are at the for loop. We start the loop at index 1. The currentIndex is 1.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/130/1*myAIxN1rKkj0oPlIgWIpMA.png" /></figure><p>We save the currentNumber as the value at the currentIndex. The currentIndex is 1 which makes the currentNumber 6.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/275/1*P-Fa7kX716uNPW-KlQ2HOA.png" /><figcaption>int currentNumber = nums[currentIndex];</figcaption></figure><p>If the currentNumber is greater than the largestNumber:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/295/1*W5F2O-SWuyBQJnOzognsuw.png" /><figcaption>The current number is greater than the largest number</figcaption></figure><ol><li>The largestNumber becomes the secondLargestNumber</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/291/1*KCilexC4_ZPaLbIC2rtlqQ.png" /><figcaption>secondLargestNumber = largestNumber;</figcaption></figure><p>2. The currentNumber becomes the largestNumber</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/275/1*95qfRoeh70ABbbnQ3Hbpgg.png" /><figcaption>largestNumber = currentNumber;</figcaption></figure><p>3. The currentIndex becomes the dominantIndex</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/265/1*lZMvco7AWtShKg51ZtVrGw.png" /><figcaption>dominantIndex = currentIndex;</figcaption></figure><p>We start the for loop over again. This time, we are at index 2.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/120/1*0HssF3b-DfovzVQm9C0SiQ.png" /></figure><p>The currentNumber is set to the value at the currentIndex. The currentIndex is 2. The number at index 2 is 1.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/276/1*hDPNzxYAtlLw-IYwPegNZg.png" /><figcaption>int currentNumber = nums[currentIndex];</figcaption></figure><p>Next we ask if the currentNumber is greater than the largestNumber. It is not. The currentNumber is 1. The largestNumber is 6. We move on to the next if statement.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/281/1*pDmOtlw0iimLMn7isCQSvw.png" /><figcaption>if (currentNumber &gt; largestNumber)</figcaption></figure><p>If the currentNumber is greater than the secondLargestNumber, the currentNumber becomes the secondLargestNumber. It’s not. The currentNumber is 1. The secondLargestNumber is 3. We go to the beginning of the <strong>for loop</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/336/1*Q97iIREJBF4DtXMbQ1lVyg.png" /><figcaption>else if (currentNumber &gt; secondLargestNumber)</figcaption></figure><p>Back to the beginning of the for loop where we are now at index 3.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/128/1*aWa0Eh4mGErnb_mPSZPKmA.png" /></figure><p>The currentNumber is set to the number at the currentIndex. The number at the currentIndex is 0.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/261/1*6MBB0AujPPblNkBUgiwu2g.png" /><figcaption>int currentNumber = nums[currentIndex];</figcaption></figure><p>Next, we ask if the currenNumber is greater than the largestNumber. It’s not. The currentNumber is 0. The largestNumber is 6. We move on to the next if-statement.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/264/1*LQq5leJMeK06UwKOqLj73g.png" /><figcaption>if (currentNumber &gt; largestNumber)</figcaption></figure><p>Then we ask if the currentNumber is greater than the secondLargestNumber. It’s not. We finished the for loop. Now, we move on to the final if statement.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/338/1*3x8GRGwXY9M3zich6l1JHg.png" /><figcaption>else if (currentNumber &gt; secondLargestNumber)</figcaption></figure><p>If the largestNumber is twice as much as the secondLargestNumber, return the dominantIndex. It is. The largestNumber is 6. The secondlargestNumber is 3. The dominantIndex is 1. The answer is 1.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/443/1*kJka-s6in8Qcja5ka_Zykg.png" /><figcaption>if (largestNumber &gt;= secondLargestNumber * 2)</figcaption></figure><h3>Complexity Analysis</h3><p>Time Complexity: O(N) where N is the number of integers</p><p>Space Complexity: O(1), the space used by the int variables</p><p>You can find the <a href="https://github.com/keldavis/Java-Algorithm-Practice/blob/master/src/Leet%20Code/src/Arrays/largestNumberAtLeastTwiceAsOthers/LargestNumberAtLeastTwiceAsOthers.java">source code</a> on <a href="https://github.com/keldavis">my github</a> profile.</p><p>I’m raising $7k for my college tuition. Please help me out by donating and/or sharing my <a href="https://www.gofundme.com/f/2r48r-help-me-continue-my-college-education?utm_source=customer&amp;utm_medium=copy_link-tip&amp;utm_campaign=p_cp+share-sheet">gofundme</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b41323428684" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Solve Pivot Index Algorithm in Java]]></title>
            <link>https://kellzcodes.medium.com/how-to-solve-pivot-index-algorithm-in-java-154e11047a58?source=rss-b5005cc6aedd------2</link>
            <guid isPermaLink="false">https://medium.com/p/154e11047a58</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[algorithms]]></category>
            <dc:creator><![CDATA[kelli davis]]></dc:creator>
            <pubDate>Wed, 13 Feb 2019 07:00:51 GMT</pubDate>
            <atom:updated>2020-04-01T18:53:25.474Z</atom:updated>
            <content:encoded><![CDATA[<p>This is my solution to <a href="https://leetcode.com/articles/find-pivot-index/">problem 724</a> on Leet Code.</p><p>Given an <a href="https://www.tutorialspoint.com/computer_programming/computer_programming_arrays.htm">array</a> of integers nums, write a method that returns the &quot;pivot&quot; index of this array.</p><p>We define the pivot index as the index where the sum of the numbers to the left of the index is equal to the sum of the numbers to the right of the index.</p><p>If no such index exists, we should return -1. If there are multiple pivot indexes, return the left-most pivot index.</p><p><strong>Example 1:</strong></p><pre><strong>Input:</strong> <br>nums = [1, 7, 3, 6, 5, 6]<br><strong>Output:</strong> 3<br><strong>Explanation:</strong> <br>The sum of the numbers to the left of index 3 (nums[3] = 6) is equal to the sum of numbers to the right of index 3.<br>Also, 3 is the first index where this occurs.</pre><p><strong>Example 2:</strong></p><pre><strong>Input:</strong> <br>nums = [1, 2, 3]<br><strong>Output:</strong> -1<br><strong>Explanation:</strong> <br>There is no index that satisfies the conditions in the problem statement.</pre><p><strong>Note:</strong></p><ul><li>The length of nums will be in the range [0, 10000].</li><li>Each element nums[i] will be an integer in the range [-1000, 1000].</li></ul><p>First, we check to see if the array is empty. If so, return -1. This is the first <a href="https://en.wikipedia.org/wiki/Edge_case">edge case</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/293/1*iim1PvBHoqa13ONFjDmsLA.png" /><figcaption>Return -1 if the array is empty</figcaption></figure><p>Next, we need to find the sum of all the numbers in the array. The leet code solution adds all the numbers into one sum. I chose to go a slightly different route.</p><p>Let’s use a <a href="https://www.cs.utah.edu/~germain/PPS/Topics/for_loops.html">for loop</a> to traverse through the whole array starting at the second index, which is index 1 (remember, the first number in an array is index 0). The number at the current index (i) is equal to the sum of the current and previous values. This way, we will always have access to the sum of the numbers before the current index.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/431/1*z1ihREc8FCaJyq96v4ZeHQ.png" /><figcaption>i equals the sum of the current and previous numbers in the array</figcaption></figure><p>When the for loop has finished, the number at the last index of the array will be the sum of all the numbers in the array.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/310/1*t0ZGaiYLxXcRXehsnvE0QA.png" /><figcaption>the last index is nums.length -1 because array indices start at 0</figcaption></figure><p>Now for the next edge case: if the first and last integer in the array are equal, return 0. This bit of code comes in handy in case the array has only one integer. In that case, the pivot index will be 0.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/353/1*T49Fikn-ra4b31PpfaOolA.png" /><figcaption>Return 0 if the array has only one number</figcaption></figure><p>It’s time to go through the revised array, starting at index 1. Remember, each index contains the sum of all the values in the array up to that point. If the last index value minus the current index value is equal to the previous index value, return the current index (i). This will be the pivot index.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/445/1*I1AK3zUVNuA4z54fqqvedA.png" /></figure><p>Finally, return -1. This is just in case the input does not pass our previous <a href="https://www.computerhope.com/jargon/i/ifstatme.htm">if statements</a> and test cases.</p><p>Let’s see it in action on the first array example above:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/331/1*9C9SrX2tgyqFp62_z3_bow.png" /></figure><p>First we ask if the array is empty. It’s not. Now we move to the for loop. Remember, the index starts at 1, so i is equal to 1.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/502/1*47fPDqUG7IJt9fvkqVDLxg.png" /></figure><p>The value at the current index is equal to the sum of the current and previous value. The current index is 1, the current value is 7 and the previous value is 1. So the new number at index 1 will be 8.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/494/1*xXnKbURu1dDwc7Fcq2sOjw.png" /></figure><p>We go through the for loop again at the next index, which is two. That means i is equal to 2. At index 2, we add the current value to the previous value. The current value is 3 and the previous value is 8. The new number at index two is 11.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/496/1*1ZH-Gr8wj7oItWefPA4h9g.png" /></figure><p>Back to the beginning of the for loop. Go to the next index. i equals three. The current value is 6 and the previous value is 11. The new value at index 3 is 17.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/547/1*ugkxg2jrh9GD1Ga8ss0B6A.png" /></figure><p>Go through the for loop again. The next index is 4. i equals 4. The current value is 5 and the previous value is 17. The new value of index 4 is 22.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/497/1*BbHg_9oIC362u2jNWQLQ3Q.png" /></figure><p>Here we are at the end of the array, going through the for loop for that last time. The index is 5. i equals 5. The current value is 6 and the previous value is 22. The new value at index 5 is 28.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/494/1*fBMsYndQu12J_w0cITL2dg.png" /></figure><p>Keep in mind, the last index contains the sum of all the numbers in the original array. Now the code will ask if the last number is equal to the first number. It’s not. So let’s move on.</p><p>We use another for loop to go through the revised array starting at index 1. We are only looking for the pivot index. If the last value (28) minus the current value is equal to the previous value, return the current index (i). This will be the pivot index.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/473/1*KnOKox_heiTCddbMiFV3KQ.png" /></figure><p>The first iteration of the for loop: i equals 1. The current value is 8 and the previous value is 1. 28- 8 equals 20, not 1. The for loop iterates again.</p><p>We do the same thing for index 2. The current value is 11 and the previous value is 8. 28- 11 equals 17, not 8. Back to the for loop.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/486/1*JrAyP_XZ8NedT1D_WhhlGw.png" /></figure><p>Now, i equals 3. The current value is 17 and the previous value is 11. 28- 17 equals 11, which is the previous value. The program is instructed to return the current index (i), which is 3. 3 is the pivot index.</p><pre><strong>Time Complexity: This will take O(N) time. N is the number of integers.</strong></pre><pre><strong>Space Complexity: This will take O(1) space.</strong></pre><p>You can find the <a href="https://github.com/keldavis/Java-Algorithm-Practice/blob/master/src/Leet%20Code/src/Arrays/pivotIndex/PivotIndex.java">source code</a> with notes on my github profile.</p><p>I’m raising $7k for my college tuition. Please help me out by donating and/or sharing my <a href="https://www.gofundme.com/f/2r48r-help-me-continue-my-college-education?utm_source=customer&amp;utm_medium=copy_link-tip&amp;utm_campaign=p_cp+share-sheet">gofundme</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=154e11047a58" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>