<?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: Sai Kathiroli</title>
    <description>The latest articles on DEV Community by Sai Kathiroli (@saikathiroli).</description>
    <link>https://dev.to/saikathiroli</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%2F1222900%2Febcc3742-547e-4881-b547-b2d169f41928.jpeg</url>
      <title>DEV Community: Sai Kathiroli</title>
      <link>https://dev.to/saikathiroli</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/saikathiroli"/>
    <language>en</language>
    <item>
      <title>Solving the Permutations Problem using Backtracking in Java</title>
      <dc:creator>Sai Kathiroli</dc:creator>
      <pubDate>Tue, 05 Dec 2023 06:56:07 +0000</pubDate>
      <link>https://dev.to/saikathiroli/solving-the-permutations-problem-using-backtracking-in-java-1964</link>
      <guid>https://dev.to/saikathiroli/solving-the-permutations-problem-using-backtracking-in-java-1964</guid>
      <description>&lt;p&gt;Introduction:&lt;br&gt;
The "Permutations" problem on LeetCode challenges us to generate all possible permutations of a given set of distinct integers. In this article, we'll explore an effective solution to this problem using backtracking in Java.&lt;/p&gt;

&lt;p&gt;Understanding the Problem:&lt;br&gt;
The goal is to generate all possible permutations of the given array of integers. A permutation is an arrangement of all the elements in a set. The key here is that all permutations should be unique.&lt;/p&gt;

&lt;p&gt;Approach:&lt;br&gt;
We will use a backtracking approach to explore all possible combinations and construct permutations. Backtracking is a systematic way of exploring all possible solutions to a problem and undoing a choice if it doesn't lead to a valid solution.&lt;/p&gt;

&lt;p&gt;Code Walkthrough:&lt;br&gt;
Let's dive into the provided Java code and understand how it works.&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 {
    List&amp;lt;List&amp;lt;Integer&amp;gt;&amp;gt; ass = new ArrayList&amp;lt;&amp;gt;();

    public List&amp;lt;List&amp;lt;Integer&amp;gt;&amp;gt; permute(int[] nums) {
        List&amp;lt;Integer&amp;gt; one = new ArrayList&amp;lt;&amp;gt;(nums.length);
        for(int x:nums) {
            one.add(Integer.MIN_VALUE);
        }
        permu(nums, 0, nums.length, one);
        return ass;
    }

    public void permu(int[] nums, int a, int e, List&amp;lt;Integer&amp;gt; was) {
        if(a == e) {
            ass.add(was);
            return;
        }
        for(int i = 0; i &amp;lt; nums.length; i++) {
            if(was.get(i) == Integer.MIN_VALUE) {
                List&amp;lt;Integer&amp;gt; monk = new ArrayList&amp;lt;&amp;gt;(was);
                monk.set(i, nums[a]);
                permu(nums, a + 1, e, monk);
            }
        }
    }
}

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

&lt;/div&gt;



&lt;p&gt;List Initialization: The ass list is used to store the final result, i.e., all permutations.&lt;/p&gt;

&lt;p&gt;permute() Method: This method initializes a list one with a placeholder value (Integer.MIN_VALUE) and then calls the permu() method to start the permutation generation process.&lt;/p&gt;

&lt;p&gt;permu() Method: This is the recursive backtracking method. It checks if the current permutation is complete (a == e) and adds it to the result list. Otherwise, it iterates through the elements of the input array, makes a choice, and recurses.&lt;/p&gt;

&lt;p&gt;Improvements and Explanation:&lt;br&gt;
While the provided code successfully generates permutations, there is an issue with the way lists are handled. In Java, when you add a list to another list (ass.add(was)), you are essentially adding a reference to the same list. This can lead to unintended behavior, as modifications to one list will affect others.&lt;/p&gt;

&lt;p&gt;To fix this, we should create a deep copy of the list before adding it to the result. Replace the line ass.add(was) with ass.add(new ArrayList&amp;lt;&amp;gt;(was)) to ensure that each permutation is stored independently.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ass.add(new ArrayList&amp;lt;&amp;gt;(was));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Conclusion:&lt;br&gt;
In this article, we explored a backtracking approach to solving the "Permutations" problem on LeetCode using Java. We analyzed the provided code, explained the backtracking strategy, and suggested an improvement to handle list references correctly. Understanding and implementing backtracking is a valuable skill for solving a wide range of combinatorial problems.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>java</category>
      <category>competativeprogramming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Navigating the Coding Challenge Seas: Unraveling the Search in Rotated Sorted Array Puzzle</title>
      <dc:creator>Sai Kathiroli</dc:creator>
      <pubDate>Thu, 30 Nov 2023 15:43:04 +0000</pubDate>
      <link>https://dev.to/saikathiroli/navigating-the-coding-challenge-seas-unraveling-the-search-in-rotated-sorted-array-puzzle-3dj6</link>
      <guid>https://dev.to/saikathiroli/navigating-the-coding-challenge-seas-unraveling-the-search-in-rotated-sorted-array-puzzle-3dj6</guid>
      <description>&lt;p&gt;Hey Dev.to community! 👋 Today, I'm thrilled to share a recent triumph in the coding realm – solving the LeetCode problem "Search in Rotated Sorted Array".&lt;/p&gt;

&lt;p&gt;The Challenge:&lt;br&gt;
The task at hand involved searching for a target element in a rotated, sorted array. Sounds tricky, right? But fear not, as I dove into the problem armed with a Java implementation and a strategic algorithm.&lt;/p&gt;

&lt;p&gt;The Algorithm:&lt;br&gt;
Let's break down the thought process behind the solution:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Java code snippet
public class D1 {
    public static void main(String[] args) {
        // Given rotated sorted array
        int[] nums = {4,5,6,7,0,1,2};
        int target = 0;
        int low=0;
        int high=nums.length-1;
        int result=-1;

        while(low&amp;lt;=high) {
            // Binary search implementation
            int mid = high+(low-high)/2;
            if(nums[mid] == target) {
                result = mid;
                break;
            }

            // Check if the left part is sorted
            if(nums[low] &amp;lt;= nums[mid]) {
                // Check if there is a possibility that the target exists here
                if(nums[low] &amp;lt;= target &amp;amp;&amp;amp; target &amp;lt;= nums[mid]) {
                    high = mid - 1;
                } else {
                    low = mid + 1;
                }
            } else {
                // Check if there is a possibility that the target exists here
                if(target &amp;lt;= nums[high] &amp;amp;&amp;amp; nums[mid] &amp;lt;= target) {
                    low = mid + 1;
                } else {
                    high = mid - 1;
                }
            }
        }

        System.out.println(result);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The algorithm cleverly narrows down the search space by identifying sorted portions of the rotated array and zeroing in on the potential location of the target.&lt;/p&gt;

&lt;p&gt;Resources and Learning:&lt;br&gt;
To solidify my understanding and approach, I delved into other valuable resources:&lt;br&gt;
&lt;a href="https://www.youtube.com/watch?v=U8XENwh8Oy8"&gt;https://www.youtube.com/watch?v=U8XENwh8Oy8&lt;/a&gt;&lt;br&gt;
&lt;a href="https://takeuforward.org/data-structure/search-element-in-a-rotated-sorted-array/"&gt;https://takeuforward.org/data-structure/search-element-in-a-rotated-sorted-array/&lt;/a&gt;&lt;br&gt;
These resources provided additional insights and perspectives, contributing to a holistic grasp of the problem.&lt;/p&gt;

&lt;p&gt;As I share this accomplishment, I'm eager to connect with the Dev.to community. Your feedback, thoughts, and alternative approaches are not just welcomed but encouraged. Let's learn and grow together!&lt;/p&gt;

&lt;p&gt;This coding journey is a testament to my commitment to professionalism, continuous learning, and problem-solving skills. The ability to navigate complex challenges is not just a technical feat but a demonstration of adaptability and perseverance.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>java</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
