<?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: sphoorthi</title>
    <description>The latest articles on DEV Community by sphoorthi (@sphoorthi).</description>
    <link>https://dev.to/sphoorthi</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%2F346056%2F6127a7ec-a4cc-494b-8e8c-b52e16b0e253.JPG</url>
      <title>DEV Community: sphoorthi</title>
      <link>https://dev.to/sphoorthi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sphoorthi"/>
    <language>en</language>
    <item>
      <title>Leetcode: Continuous Subarray Sum</title>
      <dc:creator>sphoorthi</dc:creator>
      <pubDate>Tue, 09 Jun 2020 04:56:40 +0000</pubDate>
      <link>https://dev.to/sphoorthi/leetcode-continuous-subarray-sum-459o</link>
      <guid>https://dev.to/sphoorthi/leetcode-continuous-subarray-sum-459o</guid>
      <description>&lt;p&gt;&lt;a href="https://leetcode.com/problems/continuous-subarray-sum/"&gt;523. Continuous Subarray Sum&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem:
&lt;/h2&gt;

&lt;p&gt;Given a list of non-negative numbers and a target integer k, write a function to check if the array has a continuous subarray of size at least 2 that sums up to a multiple of k, that is, sums up to n*k where n is also an integer.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Input: [23, 2, 4, 6, 7],  k=6&lt;br&gt;
Output: True&lt;br&gt;
Explanation: Because [2, 4] is a continuous subarray of size 2 and sums up to 6.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Input: [23, 2, 6, 4, 7],  k=6&lt;br&gt;
Output: True&lt;br&gt;
Explanation: Because [23, 2, 6, 4, 7] is an continuous subarray of size 5 and sums up to 42.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Constraints:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The length of the array won't exceed 10,000.&lt;br&gt;
You may assume the sum of all the numbers is in the range of a signed 32-bit integer.&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;

&lt;p&gt;The continuous sub array problems usually ask to find if there is a continuous sub array in &lt;strong&gt;array nums&lt;/strong&gt; of a minimum length "l" which is &lt;strong&gt;&lt;em&gt;multiple of&lt;/em&gt;&lt;/strong&gt; a given target K&lt;/p&gt;

&lt;p&gt;For any &lt;strong&gt;array nums&lt;/strong&gt; with &lt;strong&gt;length n&lt;/strong&gt; and the indices in order 0, i, j - where o &amp;lt; i &amp;lt; j &amp;lt; n [0, ...., i, ...., j, ... n-1]&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Consider sumI = sum of elements from 0th index to ith index. (nums[0]+.....+nums[i]) and 
modI_K = remainder of sumI divided by K (i.e. modI_K = sumI % K)&lt;/li&gt;
&lt;li&gt;Consider sumJ = sum of elements from 0th index to jth index. (nums[0]+.....+nums[j])
modJ_K = remainder of sumJ divided by K (i.e. modJ_K = sumJ % K)&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;By &lt;a href="https://www.khanacademy.org/computing/computer-science/cryptography/modarithmetic/a/the-quotient-remainder-theorem"&gt;remainder theorem&lt;/a&gt;,&lt;br&gt;
Given any integer A, and a positive integer B, there exist unique integers Q and R such that&lt;br&gt;
A = B * Q + R where 0 ≤ R &amp;lt; B&lt;/p&gt;

&lt;p&gt;To put it in mathematical terms,&lt;br&gt;
Running sum from first element to index i (sumI) when divided by K, the modI_K and sumI can be writtern as&lt;br&gt;
                             &lt;strong&gt;sumI = K * x + modI_K&lt;/strong&gt;&lt;br&gt;
Running sum from first element to index j (sumJ) when divided by K, the modJ_K and sumJ can be writtern as&lt;br&gt;
                             &lt;strong&gt;sumJ = K * x + modJ_K&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If mods of sumI and sumJ are equal &lt;strong&gt;modI_K == modJ_K&lt;/strong&gt; it means that the difference between sumI and sumJ results in a constant multiplied by K.&lt;br&gt;
                            &lt;strong&gt;sumI - sumJ = constant * K&lt;/strong&gt;&lt;br&gt;
That means there must exist a subarray sum that is a multiple of k.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;So, to tackle these type of problems we need to save the remainders of running sum to help us identify that two running sums have a complimentary sum that is a multiple of K.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="nf"&gt;checkSubarraySum&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Map&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;remainderMap&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;HashMap&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;

        &lt;span class="n"&gt;remainderMap&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;put&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;runningSum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;minLen&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;runningSum&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;runningSum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;runningSum&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;remainderMap&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;containsKey&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;runningSum&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;remainderMap&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;runningSum&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;minLen&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;remainderMap&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;put&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;runningSum&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Thank you for reading :)&lt;/p&gt;

</description>
      <category>java</category>
      <category>challenge</category>
    </item>
    <item>
      <title>What is Dependency Injection?</title>
      <dc:creator>sphoorthi</dc:creator>
      <pubDate>Tue, 07 Apr 2020 18:20:33 +0000</pubDate>
      <link>https://dev.to/sphoorthi/what-is-dependency-injection-312c</link>
      <guid>https://dev.to/sphoorthi/what-is-dependency-injection-312c</guid>
      <description>&lt;p&gt;Hello!!&lt;/p&gt;

&lt;p&gt;Dependency Injection is not a new term for Devs with years of experience. But, for a junior developer it can be a confusing. This post aims to combine some examples I found on internet which were very useful for me in understanding and explain them in my words.&lt;/p&gt;

&lt;p&gt;Let's see how wikipedia defines Dependency Injection:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;In software engineering, dependency injection is a technique whereby one object supplies the dependencies of another object. A "dependency" is an object that can be used, for example as a service. Instead of a client specifying which service it will use, something tells the client what service to use.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Well, it perfectly states what dependency injection is.&lt;/p&gt;

&lt;p&gt;Now, let me try explaining it by comparing with a real life scenario, &lt;/p&gt;

&lt;p&gt;Consider that your work requires you to travel many times a month and your organization has preferred set of agencies to be contacted to for the travel.&lt;/p&gt;

&lt;p&gt;The travel planning can be imagined in two scenarios,&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Scenario 1&lt;/strong&gt;&lt;br&gt;
You have the complete details of the preferred airline, rental car or cab agencies to be contacted. So, &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; You call the preferred agencies for the flight and car bookings.&lt;/li&gt;
&lt;li&gt; You give them the destination and date of travel.&lt;/li&gt;
&lt;li&gt; They make the necessary bookings and give you the itinerary.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Scenario 2&lt;/strong&gt;&lt;br&gt;
There is a travel administration department which handles the travel for the employees.It provides online chat services or automated telephonic services which lets you connect with these agencies. &lt;/p&gt;

&lt;p&gt;Whenever you need to travel, you use one of those services and give them your travel details like destination, date and any preferences specific to you. The agencies do the necessary bookings and hands you the travel itinerary.&lt;/p&gt;

&lt;p&gt;Let us suppose that your organization changes the preferred agencies and the new agencies have new contact details and completely different booking mechanisms. Then,&lt;/p&gt;

&lt;p&gt;In scenario 1, you need to update all the contacts and the adapt to the new booking mechanisms of the new agencies.&lt;/p&gt;

&lt;p&gt;In scenario 2, the travel administration department would readjust its workflow behind the scenes to be able to communicate with the agencies so as to not impact you.&lt;/p&gt;

&lt;p&gt;Ok, How does this relate to dependency injection?&lt;br&gt;
In both the scenarios, you are the client and you are dependent upon the services provided by the agencies.&lt;/p&gt;

&lt;p&gt;However Scenario 2 has a few differences, the administrative department is giving you the services of the agencies as dependencies in a way that you can reuse them. If there is any change in the services there need not be&lt;br&gt;
any change from you as a client.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Dependency Injection in a software application context:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Any application can be pictured as a graph of objects which are dependent on each other.&lt;br&gt;
Each object plays either the role of client and uses other objects (services) or offers services to another component or both. &lt;/p&gt;

&lt;p&gt;Each object needs to know “which” objects to communicate with, “where” to locate them, and “how” to communicate with them. When the way such services/objects can be accessed is changed they can potentially lead to a lot changes on the client side.&lt;/p&gt;

&lt;p&gt;Transferring the task of creating the object to someone else and directly using the dependency is called dependency injection.&lt;/p&gt;

&lt;p&gt;Creating and maintaining these objects is usually done by external containers which are also responsible of injecting the dependencies into the objects. These containers are often provided by the frameworks supporting different programming languages.&lt;/p&gt;

&lt;p&gt;For the Dependency Injection to be achieved we need,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A Service which provides services.&lt;/li&gt;
&lt;li&gt;A client that want to use these services.&lt;/li&gt;
&lt;li&gt;An Interface, which is the abstraction between service and client.&lt;/li&gt;
&lt;li&gt;An injector/containers which creates a service instance and injects it into the client.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The organization example in this article is inspired from &lt;a href="https://www.theserverside.com/news/1321158/A-beginners-guide-to-Dependency-Injection"&gt;A beginners guide to Dependency Injection&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>beginners</category>
      <category>dependencyinjection</category>
      <category>spring</category>
    </item>
  </channel>
</rss>
