<?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: Harsh Rajpal</title>
    <description>The latest articles on DEV Community by Harsh Rajpal (@harshrajpal).</description>
    <link>https://dev.to/harshrajpal</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%2F1000780%2Fc576dda2-9bc3-4d75-86ff-a59a068d42c3.jpg</url>
      <title>DEV Community: Harsh Rajpal</title>
      <link>https://dev.to/harshrajpal</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/harshrajpal"/>
    <language>en</language>
    <item>
      <title>CodiumAI's PR-Agent vs. GitHub Copilot for Pull Requests: Elevating Code Collaboration and Quality in Depth</title>
      <dc:creator>Harsh Rajpal</dc:creator>
      <pubDate>Sun, 24 Dec 2023 09:59:32 +0000</pubDate>
      <link>https://dev.to/harshrajpal/codiumais-pr-agent-vs-github-copilot-for-pull-requests-elevating-code-collaboration-and-quality-in-depth-1h0f</link>
      <guid>https://dev.to/harshrajpal/codiumais-pr-agent-vs-github-copilot-for-pull-requests-elevating-code-collaboration-and-quality-in-depth-1h0f</guid>
      <description>&lt;p&gt;In the dynamic landscape of modern software development, the tools developers choose significantly influence the efficiency, collaboration, and overall quality of the code they produce. CodiumAI's PR-Agent and GitHub Copilot for Pull Requests are two AI-powered code assistance tools that have garnered attention in this space. In this detailed comparison, we will explore the nuanced features of PR-Agent that position it as a superior choice over GitHub Copilot for Pull Requests, focusing on its command capabilities, platform support, open-source nature, and providing real-world examples to illustrate its practical advantages.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Command Capabilities: PR-Agent's Versatility vs. GitHub Copilot for PR’s Singular Focus&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;PR-Agent:&lt;/em&gt;&lt;br&gt;
     CodiumAI's PR-Agent distinguishes itself with an extensive set of commands designed to address various aspects of the development process, specifically within the context of pull requests. Developers can leverage commands like &lt;code&gt;/review&lt;/code&gt;, &lt;code&gt;/describe&lt;/code&gt;, &lt;code&gt;/ask&lt;/code&gt;, &lt;code&gt;/improve&lt;/code&gt;, and &lt;code&gt;/generate_labels&lt;/code&gt; to perform nuanced tasks, from detailed code reviews to seeking clarification and suggesting improvements. This breadth of command capabilities empowers developers with fine-grained control over the tool, facilitating a more comprehensive approach to code collaboration in pull requests.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;GitHub Copilot for PR:&lt;/em&gt;&lt;br&gt;
     GitHub Copilot for Pull Requests currently provides a single command that primarily focuses on code completion within the context of pull requests. While adept at generating code snippets, the tool lacks the multifaceted command set offered by PR-Agent, limiting its versatility in addressing diverse developer needs throughout the pull request lifecycle.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Platform Support: PR-Agent's Inclusivity vs. GitHub Copilot for PRs' Limitation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;PR-Agent:&lt;/em&gt;&lt;br&gt;
     A notable strength of PR-Agent lies in its inclusive approach to platform support. It seamlessly integrates with a variety of Git platforms, including GitHub, GitLab, Bitbucket, CodeCommit, and Azure DevOps. This broad platform compatibility ensures that development teams are not restricted to a specific version control system, promoting flexibility and collaboration across various ecosystems, specifically within the context of pull requests.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;GitHub Copilot for PR:&lt;/em&gt;&lt;br&gt;
     GitHub Copilot for Pull Requests, while powerful in its own right, is confined to the GitHub platform. This limitation may pose challenges for teams working with different Git platforms, limiting their ability to uniformly leverage the tool across their pull request workflows.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Open Source Advantage: PR-Agent's Transparency and Community Involvement&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;PR-Agent:&lt;/em&gt;&lt;br&gt;
     PR-Agent's open-source nature is a significant advantage that fosters transparency and community involvement, specifically in the realm of pull requests. The tool's code is publicly accessible on GitHub (&lt;a href="https://github.com/Codium-ai/pr-agent"&gt;PR-Agent GitHub Repository&lt;/a&gt;), allowing developers to inspect, contribute, and provide feedback. This open collaboration model encourages a sense of ownership and engagement, with developers actively participating in shaping the tool's features and capabilities within the context of pull requests.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;GitHub Copilot for PR:&lt;/em&gt;&lt;br&gt;
     In contrast, GitHub Copilot for Pull Requests is a proprietary solution, limiting the transparency and community involvement that is intrinsic to open-source projects like PR-Agent. Users depend on GitHub for updates and improvements, without the direct ability to contribute to the tool's development specifically within the context of pull requests.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Learning and Adaptability: PR-Agent's Context-Aware Commands vs. GitHub Copilot's Code Autocompletion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;PR-Agent:&lt;/em&gt;&lt;br&gt;
   CodiumAI's PR-Agent incorporates context-aware commands that leverage machine learning to understand the specific context of a pull request. This allows developers to use commands that are more intuitive and contextually relevant. For example, the &lt;code&gt;/review&lt;/code&gt; command might analyze code changes and provide feedback, taking into account the scope and impact of the changes. This level of intelligence in command execution enhances the tool's adaptability to different scenarios within the pull request workflow, providing a more tailored and nuanced user experience.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;GitHub Copilot for PR:&lt;/em&gt;&lt;br&gt;
   GitHub Copilot, on the other hand, excels at code autocompletion by generating code snippets based on patterns learned from a vast codebase. While powerful for coding efficiency, it may lack the contextual understanding that PR-Agent brings to the table. GitHub Copilot's strength lies in assisting with code generation, but it might not provide the same level of sophistication when it comes to executing context-aware commands for various pull request tasks beyond code completion.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Extensibility: PR-Agent's Customization Options vs. GitHub Copilot's Fixed Functionality&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;PR-Agent:&lt;/em&gt;&lt;br&gt;
   PR-Agent distinguishes itself with customization options, allowing development teams to tailor the tool to their specific workflow and preferences. Teams can potentially extend the functionality by adding custom commands or integrating additional tools, enhancing the tool's adaptability to diverse development environments.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;GitHub Copilot for PR:&lt;/em&gt;&lt;br&gt;
   GitHub Copilot, being a closed-source solution, may have limitations in terms of customization and extensibility. Teams using GitHub Copilot might have less flexibility in adapting the tool to their unique processes and may need to conform to the fixed functionality provided by the tool.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Feature Comparison&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--X9qPdIhG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wo60ula0jtww6ofcmmm5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--X9qPdIhG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wo60ula0jtww6ofcmmm5.png" alt="Image description" width="792" height="912"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Real-World Examples: Demonstrating PR-Agent's Practical Value&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mk-N6qpo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ievqp2p5jqdkv9d0ni2e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mk-N6qpo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ievqp2p5jqdkv9d0ni2e.png" alt="Image description" width="800" height="390"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the ever-evolving landscape of software development, CodiumAI's PR-Agent emerges as a versatile ally, empowering developers with a rich set of tools for code collaboration, review, and enhancement. Let's explore real-world examples that highlight PR-Agent's practical value, showcasing how it excels in addressing common challenges faced by development teams.&lt;/p&gt;

&lt;p&gt;PR-Agent's extensive set of commands empowers developers to address a spectrum of development tasks with precision. Unlike solutions with a singular focus, PR-Agent offers commands like /review, /describe, /ask, /improve and /generate_labels. &lt;/p&gt;

&lt;p&gt;Let's delve into the nuanced capabilities of two essential commands: &lt;code&gt;/review&lt;/code&gt; and &lt;code&gt;/generate_labels&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The Power of &lt;code&gt;/review&lt;/code&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;/review&lt;/code&gt; command in PR-Agent stands as a cornerstone for comprehensive pull request analysis. Developers can deploy this command to initiate a thorough examination of specific pull request changes. Witness its influence in action:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FJ4WWOKI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5x61htwi1a9dnjuzsxss.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FJ4WWOKI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5x61htwi1a9dnjuzsxss.gif" alt="Image description" width="600" height="257"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this scenario, the developer invokes PR-Agent to scrutinize the intricacies of the latest pull request's changes. PR-Agent, armed with its advanced language model, conducts a holistic evaluation. It offers constructive feedback, identifies potential issues, and suggests improvements. This nuanced guidance empowers developers to understand specific aspects requiring attention, fostering a collaborative environment for continuous refinement in the pull request lifecycle.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Unleashing the Potential of &lt;code&gt;/generate_labels&lt;/code&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Efficiency in pull request management hinges on streamlined labeling processes, and PR-Agent excels in this domain with the &lt;code&gt;/generate_labels&lt;/code&gt; command. Witness its seamless integration:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4VPW5eFr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rk9yy9x0sngujnph9aj3.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4VPW5eFr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rk9yy9x0sngujnph9aj3.gif" alt="Image description" width="600" height="257"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, the developer tasks PR-Agent with intelligently assigning labels to the latest pull request. PR-Agent, leveraging its contextual understanding of code changes, automatically categorizes the pull request with relevant labels. This automation accelerates the labeling process, ensuring pull requests are appropriately tagged. The outcome is an organized workflow, enhancing team productivity and preserving clarity in project management without manual intervention.&lt;/p&gt;

&lt;p&gt;PR-Agent's nuanced understanding of commands allows it to deliver targeted and relevant insights, catering to the diverse needs of developers throughout the development lifecycle.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion: PR-Agent's Holistic Approach to Code Collaboration&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In conclusion, CodiumAI's PR-Agent emerges as a robust and versatile solution for developers and teams seeking a comprehensive approach to code collaboration, review, and improvement, specifically within the context of pull requests. Its rich command set, broad platform support, open-source ethos, and availability both on Git and IDE collectively position it as a superior choice over GitHub Copilot for Pull Requests. By choosing PR-Agent, developers not only streamline their workflow but actively participate in a tool's evolution, fostering a culture of continuous improvement and collaboration. Experience the future of code quality with PR-Agent, where versatility meets transparency and community-driven development.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>26. Remove Duplicates from Sorted Array</title>
      <dc:creator>Harsh Rajpal</dc:creator>
      <pubDate>Thu, 18 May 2023 09:19:07 +0000</pubDate>
      <link>https://dev.to/harshrajpal/26-remove-duplicates-from-sorted-array-50k4</link>
      <guid>https://dev.to/harshrajpal/26-remove-duplicates-from-sorted-array-50k4</guid>
      <description>&lt;p&gt;&lt;strong&gt;Question Statement:&lt;/strong&gt;&lt;br&gt;
Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same. Then return the number of unique elements in nums.&lt;/p&gt;

&lt;p&gt;Consider the number of unique elements of nums to be k, to get accepted, you need to do the following things:&lt;/p&gt;

&lt;p&gt;Change the array nums such that the first k elements of nums contain the unique elements in the order they were present in nums initially. The remaining elements of nums are not important as well as the size of nums.&lt;br&gt;
Return k.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Custom Judge:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The judge will test your solution with the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int[] nums = [...]; // Input array
int[] expectedNums = [...]; // The expected answer with correct length

int k = removeDuplicates(nums); // Calls your implementation

assert k == expectedNums.length;
for (int i = 0; i &amp;lt; k; i++) {
    assert nums[i] == expectedNums[i];
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If all assertions pass, then your solution will be accepted.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums = [1,1,2]&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 2, nums = [1,2,_]&lt;br&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; Your function should return k = 2, with the first two elements of nums being 1 and 2 respectively.&lt;br&gt;
It does not matter what you leave beyond the returned k (hence they are underscores).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums = [0,0,1,1,1,2,2,3,3,4]&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 5, nums = [0,1,2,3,4,&lt;em&gt;,&lt;/em&gt;,&lt;em&gt;,&lt;/em&gt;,_]&lt;br&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; Your function should return k = 5, with the first five elements of nums being 0, 1, 2, 3, and 4 respectively.&lt;br&gt;
It does not matter what you leave beyond the returned k (hence they are underscores).&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;1 &amp;lt;= nums.length &amp;lt;= 3 * 104&lt;/li&gt;
&lt;li&gt;-100 &amp;lt;= nums[i] &amp;lt;= 100&lt;/li&gt;
&lt;li&gt;nums is sorted in non-decreasing order.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Solution:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
    public int removeDuplicates(int[] nums) {
int i=0;
        for(int j=1;j&amp;lt;nums.length;j++){
            if(nums[i]!=nums[j]){
                i++;
                nums[i]=nums[j];
            }    
        }

        return i+1;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>algorithms</category>
      <category>java</category>
      <category>100daysofcode</category>
      <category>leetcode</category>
    </item>
    <item>
      <title>1752. Check if Array Is Sorted and Rotated</title>
      <dc:creator>Harsh Rajpal</dc:creator>
      <pubDate>Thu, 18 May 2023 08:27:50 +0000</pubDate>
      <link>https://dev.to/harshrajpal/1752-check-if-array-is-sorted-and-rotated-2n4</link>
      <guid>https://dev.to/harshrajpal/1752-check-if-array-is-sorted-and-rotated-2n4</guid>
      <description>&lt;p&gt;Given an array nums, return true if the array was originally sorted in non-decreasing order, then rotated some number of positions (including zero). Otherwise, return false.&lt;/p&gt;

&lt;p&gt;There may be duplicates in the original array.&lt;/p&gt;

&lt;p&gt;Note: An array A rotated by x positions results in an array B of the same length such that A[i] == B[(i+x) % A.length], where % is the modulo operation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums = [3,4,5,1,2]&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; true&lt;br&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; [1,2,3,4,5] is the original sorted array.&lt;br&gt;
You can rotate the array by x = 3 positions to begin on the the element of value 3: [3,4,5,1,2].&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums = [2,1,3,4]&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; false&lt;br&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; There is no sorted array once rotated that can make nums.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 3:&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums = [1,2,3]&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; true&lt;br&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; [1,2,3] is the original sorted array.&lt;br&gt;
You can rotate the array by x = 0 positions (i.e. no rotation) to make nums.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;1 &amp;lt;= nums.length &amp;lt;= 100&lt;/li&gt;
&lt;li&gt;1 &amp;lt;= nums[i] &amp;lt;= 100&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
    public boolean check(int[] n) {
        int l= n.length; // Calculating length of given array.
        int nRotation=0;// Declaring a variable to keep a count of number of rotations.
//Running a for loop to identify any rotation in given array.
        for(int i=0;i&amp;lt;l;i++){
            if(n[i]&amp;gt;n[(i+1)%l]){
                nRotation++;
            }
        }
//If number of rotations are greater than 1 return false.
        if(nRotation&amp;gt;1)
            return false;
//Else return True.
        return true;

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

&lt;/div&gt;



</description>
      <category>algorithms</category>
      <category>java</category>
    </item>
    <item>
      <title>2306. Naming a Company</title>
      <dc:creator>Harsh Rajpal</dc:creator>
      <pubDate>Thu, 09 Feb 2023 03:53:42 +0000</pubDate>
      <link>https://dev.to/harshrajpal/2306-naming-a-company-30c2</link>
      <guid>https://dev.to/harshrajpal/2306-naming-a-company-30c2</guid>
      <description>&lt;p&gt;&lt;strong&gt;Problem Statement:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You are given an array of strings ideas that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:&lt;/p&gt;

&lt;p&gt;Choose 2 distinct names from ideas, call them ideaA and ideaB.&lt;br&gt;
Swap the first letters of ideaA and ideaB with each other.&lt;br&gt;
If both of the new names are not found in the original ideas, then the name ideaA ideaB (the concatenation of ideaA and ideaB, separated by a space) is a valid company name.&lt;br&gt;
Otherwise, it is not a valid name.&lt;br&gt;
Return the number of distinct valid names for the company.&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; ideas = ["coffee","donuts","time","toffee"]&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 6&lt;br&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; The following selections are valid:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;("coffee", "donuts"): The company name created is "doffee conuts".&lt;/li&gt;
&lt;li&gt;("donuts", "coffee"): The company name created is "conuts doffee".&lt;/li&gt;
&lt;li&gt;("donuts", "time"): The company name created is "tonuts dime".&lt;/li&gt;
&lt;li&gt;("donuts", "toffee"): The company name created is "tonuts doffee".&lt;/li&gt;
&lt;li&gt;("time", "donuts"): The company name created is "dime tonuts".&lt;/li&gt;
&lt;li&gt;("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The following are some examples of invalid selections:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.&lt;/li&gt;
&lt;li&gt;("time", "toffee"): Both names are still the same after swapping and exist in the original array.&lt;/li&gt;
&lt;li&gt;("coffee", "toffee"): Both names formed after swapping already exist in the original array.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; ideas = ["lack","back"]&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 0&lt;br&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; There are no valid selections. Therefore, 0 is returned.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;2 &amp;lt;= ideas.length &amp;lt;= 5 * 104&lt;/li&gt;
&lt;li&gt;1 &amp;lt;= ideas[i].length &amp;lt;= 10&lt;/li&gt;
&lt;li&gt;ideas[i] consists of lowercase English letters.&lt;/li&gt;
&lt;li&gt;All the strings in ideas are unique.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;&lt;strong&gt;Code:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {


    public long distinctNames(String[] ideas) {
        long ans = 0;
        Map&amp;lt;Character, Set&amp;lt;String&amp;gt;&amp;gt; map = new HashMap&amp;lt;&amp;gt;();
        for (String idea : ideas) {
            char startChar = idea.charAt(0);
            Set&amp;lt;String&amp;gt; values = map.getOrDefault(startChar, new HashSet&amp;lt;&amp;gt;());
            values.add(idea.substring(1));
            map.put(startChar, values);
        }
        for (int i = 0; i &amp;lt;= 26; i++) {
            for (int j = i + 1; j &amp;lt;= 26; j++) {
                long unique = count(map, (char) (i + 'a'), (char) (j + 'a'));
                ans += unique;
            }
        }
        return ans;


    }
     private long count(Map&amp;lt;Character, Set&amp;lt;String&amp;gt;&amp;gt; map, char a, char b) {
        if (!map.containsKey(a) || !map.containsKey(b)) {
            return 0;
        }
        long common = 0;
        Set&amp;lt;String&amp;gt; first = map.get(a);
        Set&amp;lt;String&amp;gt; second = map.get(b);
        for (String c : first) {
            if (second.contains(c)) {
                common++;
            }
        }
        long uniqueA = first.size() - common;
        long uniqueB = second.size() - common;
        return uniqueA * uniqueB * 2L;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>leetcode</category>
      <category>algorithms</category>
      <category>100daysofcode</category>
      <category>beginners</category>
    </item>
    <item>
      <title>472. Concatenated Words</title>
      <dc:creator>Harsh Rajpal</dc:creator>
      <pubDate>Fri, 27 Jan 2023 03:46:19 +0000</pubDate>
      <link>https://dev.to/harshrajpal/472-concatenated-words-4gco</link>
      <guid>https://dev.to/harshrajpal/472-concatenated-words-4gco</guid>
      <description>&lt;p&gt;&lt;strong&gt;Problem Statement:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Given an array of strings words (without duplicates), return all the concatenated words in the given list of words.&lt;/p&gt;

&lt;p&gt;A concatenated word is defined as a string that is comprised entirely of at least two shorter words in the given array.&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt; ["catsdogcats","dogcatsdog","ratcatdogcat"]&lt;br&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; "catsdogcats" can be concatenated by "cats", "dog" and "cats"; &lt;br&gt;
"dogcatsdog" can be concatenated by "dog", "cats" and "dog"; &lt;br&gt;
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; words = ["cat","dog","catdog"]&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; ["catdog"]&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;1 &amp;lt;= words.length &amp;lt;= 104&lt;/li&gt;
&lt;li&gt;1 &amp;lt;= words[i].length &amp;lt;= 30&lt;/li&gt;
&lt;li&gt;words[i] consists of only lowercase English letters.&lt;/li&gt;
&lt;li&gt;All the strings of words are unique.&lt;/li&gt;
&lt;li&gt;1 &amp;lt;= sum(words[i].length) &amp;lt;= 105&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;&lt;strong&gt;Code:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
  public List&amp;lt;String&amp;gt; findAllConcatenatedWordsInADict(String[] words) {
    List&amp;lt;String&amp;gt; ans = new ArrayList&amp;lt;&amp;gt;();
    Set&amp;lt;String&amp;gt; wordSet = new HashSet&amp;lt;&amp;gt;(Arrays.asList(words));
    Map&amp;lt;String, Boolean&amp;gt; memo = new HashMap&amp;lt;&amp;gt;();

    for (final String word : words)
      if (wordBreak(word, wordSet, memo))
        ans.add(word);

    return ans;
  }

  private boolean wordBreak(final String word, Set&amp;lt;String&amp;gt; wordSet, Map&amp;lt;String, Boolean&amp;gt; memo) {
    if (memo.containsKey(word))
      return memo.get(word);

    for (int i = 1; i &amp;lt; word.length(); ++i) {
      final String prefix = word.substring(0, i);
      final String suffix = word.substring(i);
      if (wordSet.contains(prefix) &amp;amp;&amp;amp;
          (wordSet.contains(suffix) || wordBreak(suffix, wordSet, memo))) {
        memo.put(word, true);
        return true;
      }
    }

    memo.put(word, false);
    return false;
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>discuss</category>
      <category>linux</category>
      <category>performance</category>
    </item>
    <item>
      <title>787. Cheapest Flights Within K Stops</title>
      <dc:creator>Harsh Rajpal</dc:creator>
      <pubDate>Thu, 26 Jan 2023 08:39:41 +0000</pubDate>
      <link>https://dev.to/harshrajpal/787-cheapest-flights-within-k-stops-49mc</link>
      <guid>https://dev.to/harshrajpal/787-cheapest-flights-within-k-stops-49mc</guid>
      <description>&lt;p&gt;&lt;strong&gt;Problem Statement:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There are n cities connected by some number of flights. You are given an array flights where flights[i] = [fromi, toi, pricei] indicates that there is a flight from city fromi to city toi with cost pricei.&lt;/p&gt;

&lt;p&gt;You are also given three integers src, dst, and k, return the cheapest price from src to dst with at most k stops. If there is no such route, return -1.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Input:&lt;/strong&gt; n = 4, flights = [[0,1,100],[1,2,100],[2,0,100],[1,3,600],[2,3,200]], src = 0, dst = 3, k = 1&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 700&lt;br&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;br&gt;
The graph is shown above.&lt;br&gt;
The optimal path with at most 1 stop from city 0 to 3 is marked in red and has cost 100 + 600 = 700.&lt;br&gt;
Note that the path through cities [0,1,2,3] is cheaper but is invalid because it uses 2 stops.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Input:&lt;/strong&gt; n = 3, flights = [[0,1,100],[1,2,100],[0,2,500]], src = 0, dst = 2, k = 1&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 200&lt;br&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;br&gt;
The graph is shown above.&lt;br&gt;
The optimal path with at most 1 stop from city 0 to 2 is marked in red and has cost 100 + 100 = 200.&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; n = 3, flights = [[0,1,100],[1,2,100],[0,2,500]], src = 0, dst = 2, k = 0&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 500&lt;br&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;br&gt;
The graph is shown above.&lt;br&gt;
The optimal path with no stops from city 0 to 2 is marked in red and has cost 500.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;1 &amp;lt;= n &amp;lt;= 100&lt;/li&gt;
&lt;li&gt;0 &amp;lt;= flights.length &amp;lt;= (n * (n - 1) / 2)&lt;/li&gt;
&lt;li&gt;flights[i].length == 3&lt;/li&gt;
&lt;li&gt;0 &amp;lt;= fromi, toi &amp;lt; n&lt;/li&gt;
&lt;li&gt;fromi != toi&lt;/li&gt;
&lt;li&gt;1 &amp;lt;= pricei &amp;lt;= 104&lt;/li&gt;
&lt;li&gt;There will not be any multiple flights between two cities.&lt;/li&gt;
&lt;li&gt;0 &amp;lt;= src, dst, k &amp;lt; n&lt;/li&gt;
&lt;li&gt;src != dst&lt;/li&gt;
&lt;/ul&gt;

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

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

&lt;ol&gt;
&lt;li&gt;Create a 2D array of size (k+2) * n&lt;/li&gt;
&lt;li&gt;Fill the first row with 0 and rest with Integer.MAX_VALUE&lt;/li&gt;
&lt;li&gt;Iterate over the flights array and update the dp array&lt;/li&gt;
&lt;li&gt;Return the minimum value in the last row&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Code:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
    public int findCheapestPrice(int n, int[][] flights, int src, int dst, int k) {

        int[][] dp = new int[k+2][n];
        for(int i=0; i&amp;lt;k+2; i++){
            Arrays.fill(dp[i], Integer.MAX_VALUE);
        }
        dp[0][src] = 0;
        for(int i=1; i&amp;lt;k+2; i++){
            for(int[] flight: flights){
                int from = flight[0];
                int to = flight[1];
                int price = flight[2];
                if(dp[i-1][from] != Integer.MAX_VALUE){
                    dp[i][to] = Math.min(dp[i][to], dp[i-1][from] + price);
                }
            }
        }
        int res = Integer.MAX_VALUE;
        for(int i=1; i&amp;lt;k+2; i++){
            res = Math.min(res, dp[i][dst]);
        }
        return res == Integer.MAX_VALUE ? -1 : res;
    }
}

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Time Complexity:&lt;/strong&gt;&lt;br&gt;
O(n^2)&lt;br&gt;
&lt;strong&gt;Space Complexity:&lt;/strong&gt;&lt;br&gt;
O(n^2)&lt;/p&gt;

</description>
      <category>svelte</category>
      <category>webdev</category>
      <category>programming</category>
      <category>productivity</category>
    </item>
    <item>
      <title>997. Find the Town Judge</title>
      <dc:creator>Harsh Rajpal</dc:creator>
      <pubDate>Mon, 23 Jan 2023 09:38:34 +0000</pubDate>
      <link>https://dev.to/harshrajpal/997-find-the-town-judge-4dcl</link>
      <guid>https://dev.to/harshrajpal/997-find-the-town-judge-4dcl</guid>
      <description>&lt;p&gt;&lt;strong&gt;Problem Statement:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In a town,there are n people labeled from 1 to n.There is a rumor that one of these people is secretly the town judge.&lt;/p&gt;

&lt;p&gt;If the town judge exists,then:&lt;/p&gt;

&lt;p&gt;The town judge trusts nobody.Everybody(except for the town judge)trusts the town judge.There is exactly one person that satisfies properties 1 and 2. You are given an array trust where trust[i]=[ai,bi]representing that the person labeled ai trusts the person labeled bi.&lt;/p&gt;

&lt;p&gt;Return the label of the town judge if the town judge exists and can be identified,or return-1 otherwise.&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; n=2,trust=[[1,2]]&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt;2 &lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; n=3,trust=[[1,3],[2,3]]&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt;3 &lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; n=3,trust=[[1,3],[2,3],[3,1]]&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt;-1&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;1&amp;lt;=n&amp;lt;=1000 0&amp;lt;=trust.length&amp;lt;=104 trust[i].length==2 &lt;/li&gt;
&lt;li&gt;All the pairs of trust are unique.ai!=bi 1&amp;lt;=ai,bi&amp;lt;=n&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Algorithm:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create two arrays inDegree and outDegree.&lt;/li&gt;
&lt;li&gt;Traverse the trust array and update the inDegree and outDegree arrays.&lt;/li&gt;
&lt;li&gt;Traverse the inDegree array and check if the current person is the town judge.&lt;/li&gt;
&lt;li&gt;If the current person is the town judge, return the current person.&lt;/li&gt;
&lt;li&gt;If the town judge does not exist, return -1.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Code:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Solution {
    public int findJudge(int n, int[][] trust) {
        int[] inDegree = new int[n + 1];
        int[] outDegree = new int[n + 1];
        for (int[] t : trust) {
            outDegree[t[0]]++;
            inDegree[t[1]]++;
        }
        for (int i = 1; i &amp;lt;= n; i++) {
            if (inDegree[i] == n - 1 &amp;amp;&amp;amp; outDegree[i] == 0) {
                return i;
            }
        }
        return -1;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Time Complexity:&lt;/strong&gt;&lt;br&gt;
O(n)O(n) The time complexity is linear, because we traverse the trust array once.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Space Complexity:&lt;/strong&gt;&lt;br&gt;
O(n)O(n) The space complexity is linear, because we create two arrays of size n+1.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>131. Palindrome Partitioning</title>
      <dc:creator>Harsh Rajpal</dc:creator>
      <pubDate>Sun, 22 Jan 2023 15:12:12 +0000</pubDate>
      <link>https://dev.to/harshrajpal/131-palindrome-partitioning-55pl</link>
      <guid>https://dev.to/harshrajpal/131-palindrome-partitioning-55pl</guid>
      <description>&lt;p&gt;&lt;strong&gt;Problem Statement:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s.&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; s = "aab"&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; [["a","a","b"],["aa","b"]]&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; s = "a"&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; [["a"]]&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;1 &amp;lt;= s.length &amp;lt;= 16&lt;/li&gt;
&lt;li&gt;s contains only lowercase English letters.&lt;/li&gt;
&lt;/ul&gt;

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

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

&lt;ol&gt;
&lt;li&gt;Use DFS to traverse all the possible partitions.&lt;/li&gt;
&lt;li&gt;Use a helper function to check if the current substring is a palindrome.&lt;/li&gt;
&lt;li&gt;If the current substring is a palindrome, add it to the path and continue to traverse the rest of the string.&lt;/li&gt;
&lt;li&gt;If the current substring is not a palindrome, do not add it to the path and continue to traverse the rest of the string.&lt;/li&gt;
&lt;li&gt;If the current index is equal to the length of the string, add the current path to the result.&lt;/li&gt;
&lt;li&gt;After the traversal is completed, return the result.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Code:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Solution {
    public List&amp;lt;List&amp;lt;String&amp;gt;&amp;gt; partition(String s) {
        List&amp;lt;List&amp;lt;String&amp;gt;&amp;gt; res = new ArrayList&amp;lt;&amp;gt;();
        List&amp;lt;String&amp;gt; path = new ArrayList&amp;lt;&amp;gt;();
        dfs(s, 0, path, res);
        return res;

    }

    private void dfs(String s, int index, List&amp;lt;String&amp;gt; path, List&amp;lt;List&amp;lt;String&amp;gt;&amp;gt; res) {
        if (index == s.length()) {
            res.add(new ArrayList&amp;lt;&amp;gt;(path));
            return;
        }
        for (int i = index; i &amp;lt; s.length(); i++) {
            if (isPalindrome(s, index, i)) {
                path.add(s.substring(index, i + 1));
                dfs(s, i + 1, path, res);
                path.remove(path.size() - 1);
            }
        }
    }

    private boolean isPalindrome(String s, int left, int right) {
        while (left &amp;lt; right) {
            if (s.charAt(left) != s.charAt(right)) {
                return false;
            }
            left++;
            right--;
        }
        return true;
    }

}

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Time Complexity:&lt;/strong&gt;&lt;br&gt;
O(n*2^n)O(n∗2n) The time complexity of the backtracking algorithm is exponential, because in the worst case, we need to add n-1 separators into a string of length n, which results in 2^n2&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Space Complexity:&lt;/strong&gt;&lt;br&gt;
O(n)O(n) The space complexity of the backtracking algorithm is linear, because in the worst case, the depth of the recursion tree can reach nn.&lt;/p&gt;

</description>
      <category>welcome</category>
      <category>community</category>
      <category>machinelearning</category>
      <category>datascience</category>
    </item>
    <item>
      <title>59. Spiral Matrix II</title>
      <dc:creator>Harsh Rajpal</dc:creator>
      <pubDate>Thu, 19 Jan 2023 05:14:04 +0000</pubDate>
      <link>https://dev.to/harshrajpal/59-spiral-matrix-ii-492</link>
      <guid>https://dev.to/harshrajpal/59-spiral-matrix-ii-492</guid>
      <description>&lt;p&gt;&lt;strong&gt;Problem Statement:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Given a positive integer n, generate an n x n matrix filled with elements from 1 to n2 in spiral order.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Input:&lt;/strong&gt; n = 3&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; [[1,2,3],[8,9,4],[7,6,5]]&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Input:&lt;/strong&gt; n = 1&lt;br&gt;
*&lt;em&gt;Output: *&lt;/em&gt;[[1]]&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;1 &amp;lt;= n &amp;lt;= 20&lt;/li&gt;
&lt;/ul&gt;

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

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

&lt;ol&gt;
&lt;li&gt;Create a matrix of size n x n.&lt;/li&gt;
&lt;li&gt;Initialize left = 0, right = n - 1, top = 0, bottom = n - 1, num = 1&lt;/li&gt;
&lt;li&gt;Iterate while left &amp;lt;= right and top &amp;lt;= bottom.
a) Iterate from left to right and fill the top row with num.
b) Increment top.
c) Iterate from top to bottom and fill the right column with num.
d) Decrement right.
e) Iterate from right to left and fill the bottom row with num.
f) Decrement bottom.
g) Iterate from bottom to top and fill the left column with num.
h) Increment left.&lt;/li&gt;
&lt;li&gt;Return matrix.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Code:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Solution {
    public int[][] generateMatrix(int n){
        int[][] matrix = new int[n][n];
        int left = 0;
        int right = n - 1;
        int top = 0;
        int bottom = n - 1;
        int num = 1;
        while(left &amp;lt;= right &amp;amp;&amp;amp; top &amp;lt;= bottom){
            for(int i = left; i &amp;lt;= right; i++){
                matrix[top][i] = num++;
            }
            top++;
            for(int i = top; i &amp;lt;= bottom; i++){
                matrix[i][right] = num++;
            }
            right--;
            for(int i = right; i &amp;gt;= left; i--){
                matrix[bottom][i] = num++;
            }
            bottom--;
            for(int i = bottom; i &amp;gt;= top; i--){
                matrix[i][left] = num++;
            }
            left++;
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Time Complexity:&lt;/strong&gt;&lt;br&gt;
O(N^2)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Space Complexity:&lt;/strong&gt;&lt;br&gt;
O(N^2)&lt;/p&gt;

</description>
      <category>psychology</category>
      <category>science</category>
    </item>
    <item>
      <title>974. Subarray Sums Divisible by K</title>
      <dc:creator>Harsh Rajpal</dc:creator>
      <pubDate>Thu, 19 Jan 2023 04:16:40 +0000</pubDate>
      <link>https://dev.to/harshrajpal/974-subarray-sums-divisible-by-k-4m9p</link>
      <guid>https://dev.to/harshrajpal/974-subarray-sums-divisible-by-k-4m9p</guid>
      <description>&lt;p&gt;&lt;strong&gt;Problem Statement:&lt;/strong&gt;&lt;br&gt;
Given an integer array nums and an integer k, return the number of non-empty subarrays that have a sum divisible by k.&lt;/p&gt;

&lt;p&gt;A subarray is a contiguous part of an array.&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; nums = [4,5,0,-2,-3,1], k = 5&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 7&lt;br&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; There are 7 subarrays with a sum divisible by k = 5:&lt;br&gt;
[4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3]&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; nums = [5], k = 9&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 0&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;1 &amp;lt;= nums.length &amp;lt;= 3 * 104&lt;/li&gt;
&lt;li&gt;-104 &amp;lt;= nums[i] &amp;lt;= 104&lt;/li&gt;
&lt;li&gt;2 &amp;lt;= k &amp;lt;= 104&lt;/li&gt;
&lt;/ul&gt;

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

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

&lt;ol&gt;
&lt;li&gt;Create a count array of size k and initialize all elements as 0.&lt;/li&gt;
&lt;li&gt;Initialize sum of elements as 0 and count of subarrays as 0.&lt;/li&gt;
&lt;li&gt;Iterate through the array and for every element arr[i], do following.
a) Increment sum by arr[i].
b) If k is non-zero, then update sum as sum = sum % k.
c) Increment count of current sum.
d) Add count[sub_sum] to the result.
e) Increment count[sub_sum] by 1.&lt;/li&gt;
&lt;li&gt;Return result.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Code:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Solution {
    public int subarrayDivByK(int[] nums, int k){
        int[] count = new int[k];
        count[0] = 1;
        int sum = 0;
        int ans = 0;
        for(int i = 0; i &amp;lt; nums.length; i++){
            sum += nums[i];
            int mod = (sum % k + k) % k;
            ans += count[mod];
            count[mod]++;
        }
        return ans;
    }

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Time Complexity:&lt;/strong&gt;&lt;br&gt;
O(N)&lt;br&gt;
&lt;strong&gt;Space Complexity:&lt;/strong&gt;&lt;br&gt;
O(K)&lt;/p&gt;

</description>
      <category>java</category>
      <category>algorithms</category>
      <category>100daysofcode</category>
      <category>leetcode</category>
    </item>
    <item>
      <title>234. Palindrome Linked List</title>
      <dc:creator>Harsh Rajpal</dc:creator>
      <pubDate>Tue, 17 Jan 2023 10:29:16 +0000</pubDate>
      <link>https://dev.to/harshrajpal/234-palindrome-linked-list-4d5h</link>
      <guid>https://dev.to/harshrajpal/234-palindrome-linked-list-4d5h</guid>
      <description>&lt;p&gt;&lt;strong&gt;Problem Statement:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Given the head of a singly linked list, return true if it is a &lt;br&gt;
palindrome or false otherwise.&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; head = [1,2,2,1]&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; true&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; head = [1,2]&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; false&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;The number of nodes in the list is in the range [1, 105].&lt;/li&gt;
&lt;li&gt;0 &amp;lt;= Node.val &amp;lt;= 9&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;&lt;strong&gt;Code:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Solution {
    public boolean isPalindrome(ListNode head) {
        ListNode slow = head, fast = head, prev, temp;
        while (fast != null &amp;amp;&amp;amp; fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }
        prev = slow;
        slow = slow.next;
        prev.next = null;
        while (slow != null) {
            temp = slow.next;
            slow.next = prev;
            prev = slow;
            slow = temp;
        }
        fast = head;
        slow = prev;
        while (slow != null) {
            if (fast.val != slow.val)
                return false;
            fast = fast.next;
            slow = slow.next;
        }
        return true;
    }

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

&lt;/div&gt;



</description>
      <category>java</category>
      <category>algorithms</category>
      <category>leetcode</category>
      <category>100daysofcode</category>
    </item>
    <item>
      <title>926. Flip String to Monotone Increasing</title>
      <dc:creator>Harsh Rajpal</dc:creator>
      <pubDate>Tue, 17 Jan 2023 04:09:08 +0000</pubDate>
      <link>https://dev.to/harshrajpal/926-flip-string-to-monotone-increasing-31ok</link>
      <guid>https://dev.to/harshrajpal/926-flip-string-to-monotone-increasing-31ok</guid>
      <description>&lt;p&gt;&lt;strong&gt;Problem Statement:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A binary string is monotone increasing if it consists of some number of 0's (possibly none), followed by some number of 1's (also possibly none).&lt;/p&gt;

&lt;p&gt;You are given a binary string s. You can flip s[i] changing it from 0 to 1 or from 1 to 0.&lt;/p&gt;

&lt;p&gt;Return the minimum number of flips to make s monotone increasing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Input:&lt;/strong&gt; s = "00110"&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 1&lt;br&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; We flip the last digit to get 00111.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Input:&lt;/strong&gt; s = "010110"&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 2&lt;br&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; We flip to get 011111, or alternatively 000111.&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; s = "00011000"&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 2&lt;br&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; We flip to get 00000000.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;1 &amp;lt;= s.length &amp;lt;= 105&lt;/li&gt;
&lt;li&gt;s[i] is either '0' or '1'.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;&lt;strong&gt;Code:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
    public int minFlipsMonoIncr(String s) {
        int n = s.length();
        int flipCount = 0;
        int oneCount = 0;
        for (int i = 0; i &amp;lt; n; i++) {
            if (s.charAt(i) == '0') {
                if (oneCount == 0)
                    continue;
                flipCount++;
            } else
                oneCount++;
            if (oneCount &amp;lt; flipCount)
                flipCount = oneCount;
        }
        return flipCount;

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Time Complexity:&lt;/strong&gt;&lt;br&gt;
O(N)&lt;br&gt;
&lt;strong&gt;Space Complexity:&lt;/strong&gt;&lt;br&gt;
O(1)&lt;/p&gt;

</description>
      <category>programming</category>
      <category>productivity</category>
      <category>discuss</category>
    </item>
  </channel>
</rss>
