<?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: Tisha Agarwal</title>
    <description>The latest articles on DEV Community by Tisha Agarwal (@tishaag098).</description>
    <link>https://dev.to/tishaag098</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%2F806320%2F62a691d7-5ac9-48f1-aa9a-72dcfdf94dcf.jpeg</url>
      <title>DEV Community: Tisha Agarwal</title>
      <link>https://dev.to/tishaag098</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tishaag098"/>
    <language>en</language>
    <item>
      <title>LeetCode: Majority Element (Boyer-Moore Majority Voting Algorithm)</title>
      <dc:creator>Tisha Agarwal</dc:creator>
      <pubDate>Mon, 21 Feb 2022 15:20:39 +0000</pubDate>
      <link>https://dev.to/tishaag098/leetcode-majority-element-boyer-moore-majority-voting-algorithm-42io</link>
      <guid>https://dev.to/tishaag098/leetcode-majority-element-boyer-moore-majority-voting-algorithm-42io</guid>
      <description>&lt;p&gt;Given an array nums of size n, return the majority element.&lt;/p&gt;

&lt;p&gt;The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array.&lt;/p&gt;

&lt;p&gt;Example 1:&lt;br&gt;
Input: nums = [3,2,3]&lt;br&gt;
Output: 3&lt;/p&gt;

&lt;p&gt;Example 2:&lt;br&gt;
Input: nums = [2,2,1,1,1,2,2]&lt;br&gt;
Output: 2&lt;/p&gt;

&lt;p&gt;This is a &lt;strong&gt;EASY LEVEL&lt;/strong&gt; question but it is very important.&lt;br&gt;
It can easily be solved using two or three technics. But the main algorithm that we'll learn while solving it is &lt;strong&gt;Boyer-Moore Majority Voting Algorithm&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To solve the question click here:(&lt;a href="https://leetcode.com/problems/majority-element/"&gt;https://leetcode.com/problems/majority-element/&lt;/a&gt;)&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The &lt;u&gt;Boyer-Moore voting algorithm&lt;/u&gt; is one of the popular optimal algorithms which is used to find the majority element among the given elements that have more than N/ 2 occurrences. This works perfectly fine for finding the majority element which takes 2 traversals over the given elements, which works in O(N) time complexity and O(1) space complexity.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Approaches-&lt;/strong&gt;&lt;br&gt;
(1) &lt;strong&gt;Nested for-loop :&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;Time Complexity: O(N^2)&lt;/em&gt;&lt;br&gt;
&lt;em&gt;Space Complexity: O(1)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;JAVA 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 static int majorityElement(int[] nums) {

        int n=nums.length;
        int max=0,ele=nums[0];
        for(int i=0;i&amp;lt;nums.length;i++)
        {
            int count=0;
            for(int j=i;j&amp;lt;nums.length;j++)
            {
                if(nums[i]==nums[j])
                    count++;
            }
            if(count&amp;gt;max)
            {
                max=count;
                ele=nums[i];
            }
        }
        if(max&amp;gt;=(int)(n/2))
            return ele;

        return -1;
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(2) &lt;strong&gt;Sorting:&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;Time Complexity: O(NlogN)&lt;/em&gt;&lt;br&gt;
&lt;em&gt;Space Complexity: O(1)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;JAVA 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 static int majorityElement_m2(int nums[])
    {
            Arrays.sort(nums);
            return nums[nums.length/2];
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;&lt;strong&gt;JAVA 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 static int majorityElement_m3(int[] nums) {

        HashMap&amp;lt;Integer,Integer&amp;gt; hm=new HashMap&amp;lt;&amp;gt;();

        for(int i=0;i&amp;lt;nums.length;i++)
        {
            if(hm.containsKey(nums[i]))
                hm.put(nums[i] , hm.get(nums[i])+1);
            else
                hm.put(nums[i],1);
        }
        int answer =0;
     for (Map.Entry&amp;lt;Integer, Integer&amp;gt; entry : hm.entrySet())
     {
         if(entry.getValue()&amp;gt;nums.length/2)
         {
             answer = entry.getKey();
         }
     }
    return answer;
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(4)&lt;strong&gt;Boyer-Moore Majority Voting Algorithm:&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;Time Complexity: O(N)&lt;/em&gt;&lt;br&gt;
&lt;em&gt;Space Complexity: O(1)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;JAVA 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 static int majorityElement_m4(int[] nums) {
        int ans=nums[0];
        int count=1;
        for(int i=1;i&amp;lt;nums.length;i++)
        {
                if(nums[i]==ans)
                    count++;
                else
                    count--;

        if(count==0)
        {
             ans=nums[i];
            count=1; 
        }

        }
        return ans;

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

&lt;/div&gt;



</description>
      <category>java</category>
      <category>algorithms</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>LeetCode: Count and Say</title>
      <dc:creator>Tisha Agarwal</dc:creator>
      <pubDate>Sun, 20 Feb 2022 09:12:30 +0000</pubDate>
      <link>https://dev.to/tishaag098/leetcode-count-and-say-52ag</link>
      <guid>https://dev.to/tishaag098/leetcode-count-and-say-52ag</guid>
      <description>&lt;p&gt;&lt;a href="https://leetcode.com/problems/count-and-say/"&gt;Question link&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It's a &lt;strong&gt;MEDIUM LEVEL&lt;/strong&gt; question. At first I found is tough but after going through the question thrice, I understood the concept.&lt;/p&gt;

&lt;p&gt;So, before moving on to my question, I just wanna tell that this question can be done using 2 methods-&lt;br&gt;
(1) &lt;em&gt;Recursion&lt;/em&gt;  (Optimize method)&lt;br&gt;
(2) &lt;em&gt;For-loop&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;For solving this question, I haven't used recursion. Instead I used &lt;code&gt;for-loop&lt;/code&gt; method as it helped me in understanding the concept more clearly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;JAVA 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;import java.util.*;
public class Count_and_Say {
    public static void main(String[] args) throws Exception
    {
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter the value of n: ");
        int n=sc.nextInt();
        String s=countAndSay(n);
        System.out.println(s);
    }
    public static String countAndSay(int n) {
        String str="1";

        for(int i=2;i&amp;lt;=n;i++)
        {
            StringBuilder temp=new StringBuilder();
            char prev=str.charAt(0);
            int counter=1;
            for(int j=1;j&amp;lt;str.length();j++)
            {
                char ch=str.charAt(j);
                if(ch!=prev)
                {
                   temp.append(counter).append(prev);
                    prev=ch;
                    counter=1;
                }
                else
                {
                    counter++;
                }
            }
                temp.append(counter).append(prev);
                str=temp.toString();
            }

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

&lt;/div&gt;



</description>
      <category>java</category>
      <category>string</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Why String is Immutable in JAVA? What exactly does it mean?</title>
      <dc:creator>Tisha Agarwal</dc:creator>
      <pubDate>Mon, 14 Feb 2022 20:32:14 +0000</pubDate>
      <link>https://dev.to/tishaag098/why-string-is-immutable-in-java-what-exactly-does-it-mean-59ki</link>
      <guid>https://dev.to/tishaag098/why-string-is-immutable-in-java-what-exactly-does-it-mean-59ki</guid>
      <description>&lt;p&gt;So recently I came across the question, why are Strings Immutable in java?&lt;br&gt;
Before discussing on this, I would first like to tell &lt;strong&gt;What are Immutable strings?&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Immutable simply means unmodifiable or unchangeable. Once String object is created its data or state can't be changed but a new String object is created.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Before going through this article, I recommend to first know about &lt;a href="https://dev.to/tishaag098/string-constant-pool-storage-of-strings-1784"&gt;String Constant Pool&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This is how String works:&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;String str = new String("Tisha");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This, as usual, creates a string containing "Tisha" and assigns it a reference str.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  str = str.concat(" Agarwal");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F338m4oskhv5z49l5gklo.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F338m4oskhv5z49l5gklo.jpeg" alt="immutable"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This appends a string " Agarwal" to &lt;code&gt;str&lt;/code&gt;. But wait, how is this possible, since &lt;strong&gt;String objects are immutable&lt;/strong&gt;? Well to your surprise, it is.&lt;br&gt;
When the above statement is executed, the VM takes the value of String &lt;code&gt;str&lt;/code&gt;, i.e. "Tisha" and appends " Agarwal", giving us the value &lt;code&gt;"Tisha Agarwal"&lt;/code&gt;. Now, since Strings are immutable, the VM can’t assign this value to &lt;code&gt;str&lt;/code&gt;, so it creates a &lt;strong&gt;new String object&lt;/strong&gt;, gives it a value &lt;code&gt;"Tisha Agarwal"&lt;/code&gt;, and gives it a reference str.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Why are strings Immutable?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Let's understand this with the help of an example:&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;String chocolate1="KitKat";&lt;br&gt;
 String chocolate2="KitKat";&lt;br&gt;
 String chocolate3="KitKat"; &lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fepy8d45r59yokly3zcfb.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fepy8d45r59yokly3zcfb.jpeg" alt="image2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So what's happening?&lt;br&gt;
In this a object of &lt;code&gt;KitKat&lt;/code&gt; is created inside String Constant pool and all three variables &lt;code&gt;chocolate1&lt;/code&gt; , &lt;code&gt;chocolate2&lt;/code&gt; , &lt;code&gt;chocolate3&lt;/code&gt; are pointing to it. &lt;/p&gt;

&lt;p&gt;Now if we change cholocate3 to MilkyBar&lt;br&gt;
&lt;code&gt;chocolate3=MilkyBar;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Suppose if the string were mutable then without creating a new object the value of &lt;code&gt;chocolate3&lt;/code&gt; would have changed from &lt;code&gt;KitKat&lt;/code&gt; to &lt;code&gt;MilkyBar&lt;/code&gt;.But don't you think this would also change the value of &lt;code&gt;chocolate1&lt;/code&gt; and &lt;code&gt;chocolate2&lt;/code&gt;from &lt;code&gt;KitKat&lt;/code&gt; to &lt;code&gt;MilkyBar&lt;/code&gt; as shown in the above diagram, these 3 were pointing to the same object. &lt;/p&gt;

&lt;p&gt;So, that's why Strings are Immutable,&lt;br&gt;
from the above example, now the chocolate3 will create a different object and will point to it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0nawikstltzbws2tv2fl.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0nawikstltzbws2tv2fl.jpeg" alt="Image3"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Point to Remember&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Strings are Immutable in Java because String objects are cached in String pool. Since cached String literals are shared between multiple persons there is always a risk, where one person's action would affect all other persons.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>java</category>
      <category>beginners</category>
      <category>discuss</category>
      <category>programming</category>
    </item>
    <item>
      <title>String Constant Pool (Storage of Strings)</title>
      <dc:creator>Tisha Agarwal</dc:creator>
      <pubDate>Mon, 14 Feb 2022 13:51:23 +0000</pubDate>
      <link>https://dev.to/tishaag098/string-constant-pool-storage-of-strings-1784</link>
      <guid>https://dev.to/tishaag098/string-constant-pool-storage-of-strings-1784</guid>
      <description>&lt;p&gt;A &lt;strong&gt;string constant pool&lt;/strong&gt; is a separate place in the heap memory where the values of all the strings which are defined in the program are stored.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Two ways to create string object:&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;1) &lt;code&gt;String s1=new String("Tisha");&lt;/code&gt;&lt;br&gt;
If we create a string like this, then it creates &lt;strong&gt;two objects&lt;/strong&gt; in the memory. One is in Heap Area and the other is in string constant pool.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwj3tokvyz5qvigqhafmz.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwj3tokvyz5qvigqhafmz.jpeg" alt="2 objects"&gt;&lt;/a&gt;&lt;br&gt;
2) &lt;code&gt;String s2="Anam";&lt;/code&gt;&lt;br&gt;
In this only &lt;strong&gt;one object&lt;/strong&gt; is created i.e. inside string constant pool.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffmp5wjxh2qojqumuz4pf.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffmp5wjxh2qojqumuz4pf.jpeg" alt="1 object"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That is why it is recommended to make string objects like this: &lt;code&gt;String s2="Anam";&lt;/code&gt;, as only one object this created.&lt;br&gt;
&lt;em&gt;More the number of objects, more the project will become heavy.&lt;br&gt;
If the project becomes heavy then it will slow down.&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Interview question:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;u&gt;&lt;em&gt;Can Garbage Collection occur in string constant pool?&lt;/em&gt;&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;&lt;u&gt;&lt;em&gt;String objects which are in the string pool will not be garbage collected because a reference variable internally is maintained by JVM for each string literal object. Other String objects will be garbage collected if you don't have reference to it in your program execution.&lt;/em&gt;&lt;/u&gt;&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Special Cases&lt;/strong&gt;&lt;br&gt;
Suppose if we create two strings &lt;code&gt;str1&lt;/code&gt; and &lt;code&gt;str2&lt;/code&gt;&lt;br&gt;
&lt;code&gt;String str1=new String("Alex");&lt;/code&gt;---------------&amp;gt;2 objects are created&lt;br&gt;
&lt;code&gt;String str2=new String ("John");-&lt;/code&gt;-------------&amp;gt;2 objects are created&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foarfjzaequvhrs13tppa.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foarfjzaequvhrs13tppa.jpeg" alt="Special Case"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now if we create one more string with the same string literal as &lt;code&gt;str1&lt;/code&gt; so in that case only one object will be created in heap area.&lt;br&gt;
String constant pool(SCP) will check if that literal is inside it or not. If the literal &lt;code&gt;"Alex"&lt;/code&gt; is present inside SCP then it will not create another literal with the same name. Hence only one object is created.&lt;br&gt;
&lt;code&gt;String str3=new String("Alex");&lt;/code&gt;-----------------&amp;gt;1 object created&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5yklrg31eeny1lta6wvb.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5yklrg31eeny1lta6wvb.jpeg" alt="SpecialCase2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now if we create string,&lt;br&gt;
&lt;code&gt;String str4="Simran";&lt;/code&gt;--------------&amp;gt;1 object created inside SCP&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw9xmttdreprjxiocg1b9.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw9xmttdreprjxiocg1b9.jpeg" alt="Simran"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To understand it better, let's create more strings and see where their objects are being created- in &lt;em&gt;heap area&lt;/em&gt; or in &lt;em&gt;string constant memory&lt;/em&gt;?&lt;/p&gt;

&lt;p&gt;&lt;code&gt;String str5="Alex"&lt;/code&gt;;-----------&amp;gt;No objects are created&lt;br&gt;
&lt;code&gt;str5&lt;/code&gt; will not create any objects because SCP will first check that whether &lt;code&gt;"Alex"&lt;/code&gt; is already present inside it or not.&lt;br&gt;
Since in this case &lt;code&gt;"Alex"&lt;/code&gt; is already in SCP so no objects will be created. And str5 will start pointing to the previous "Alex" that existed inside String constant pool(SCP)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fspm992tr8o91qsm1b5jt.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fspm992tr8o91qsm1b5jt.jpeg" alt="str5"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;str6="Alex";&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
In this case &lt;code&gt;str6&lt;/code&gt; and &lt;code&gt;str5&lt;/code&gt; both will point to &lt;code&gt;"Alex"&lt;/code&gt; inside SCP.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8qf49v4gu5ttrjwf26xy.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8qf49v4gu5ttrjwf26xy.jpeg" alt="str6"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Points to remember:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;If &lt;code&gt;new&lt;/code&gt; keyword is used to create string then object is not only created in String Constant Pool but also in Heap Area.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If we create strings like &lt;code&gt;String S="WONDERFUL"&lt;/code&gt; , then SCP checks whether the string literal is already present in SCP or not.&lt;br&gt;
If it is not present the only 1 object is created inside String Constant Pool(SCP)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If same literal object is present inside string constant pool (SCP)&lt;br&gt;
then instead of creating new object with the same literal, it points to the existing object.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>java</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
    <item>
      <title>Maximum Subarray Sum</title>
      <dc:creator>Tisha Agarwal</dc:creator>
      <pubDate>Thu, 10 Feb 2022 19:33:13 +0000</pubDate>
      <link>https://dev.to/tishaag098/maximum-subarray-sum-1o59</link>
      <guid>https://dev.to/tishaag098/maximum-subarray-sum-1o59</guid>
      <description>&lt;p&gt;Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.&lt;br&gt;
A subarray is a contiguous part of an array.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;Example 1:&lt;/u&gt;&lt;br&gt;
Input: nums = [-2,1,-3,4,-1,2,1,-5,4]&lt;br&gt;
Output: 6&lt;br&gt;
Explanation: [4,-1,2,1] has the largest sum = 6.&lt;/p&gt;

&lt;p&gt;To solve this question click here:(&lt;a href="https://leetcode.com/problems/maximum-subarray/"&gt;https://leetcode.com/problems/maximum-subarray/&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;Though it is a &lt;strong&gt;EASY LEVEL&lt;/strong&gt; question but is really very important.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Brute force Approach:&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;Time Complexity: O(n^3)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;C++ 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;#include&amp;lt;iostream&amp;gt;
#include&amp;lt;climits&amp;gt;
using namespace std;
int main()
{
    int n;
    cin&amp;gt;&amp;gt;n;
    int arr[n];
    for(int i=0;i&amp;lt;n;i++)
      cin&amp;gt;&amp;gt;arr[i];
    int maxSum=INT_MIN;
    for(int i=0;i&amp;lt;n;i++)
    {
        for(int j=i;j&amp;lt;n;j++)
        {
            int sum=0;
            for(int k=i;k&amp;lt;=j;k++)
                sum+=arr[k];
            maxSum=max(sum,maxSum);
        }
    }
    cout&amp;lt;&amp;lt;maxSum&amp;lt;&amp;lt;endl;
    return 0;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This question can be solved in an optimized way by using &lt;strong&gt;Kadane Algorithm.&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;The simple idea of Kadane’s algorithm is to look for all positive contiguous segments of the array. And keep track of maximum sum contiguous segment among all positive segments&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;C++ 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;#include&amp;lt;iostream&amp;gt;
#include &amp;lt;climits&amp;gt;
using namespace std;
int main()
{
    int n;
    cin&amp;gt;&amp;gt;n;
    int arr[n];
    for(int i=0;i&amp;lt;n;i++)
    cin&amp;gt;&amp;gt;arr[i];

   int currSum=0;
   int maxSum=INT_MIN;
   for(int i=0;i&amp;lt;n;i++)
   {
       currSum+=arr[i];
       if(currSum&amp;lt;0)
           currSum=0;
       maxSum=max(maxSum,currSum);
   }
   cout&amp;lt;&amp;lt;maxSum&amp;lt;&amp;lt;endl;
    return 0;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>beginners</category>
      <category>programming</category>
      <category>java</category>
      <category>dsa</category>
    </item>
    <item>
      <title>Subarray with 0 sum</title>
      <dc:creator>Tisha Agarwal</dc:creator>
      <pubDate>Thu, 10 Feb 2022 15:18:01 +0000</pubDate>
      <link>https://dev.to/tishaag098/subarray-with-0-sum-46gj</link>
      <guid>https://dev.to/tishaag098/subarray-with-0-sum-46gj</guid>
      <description>&lt;p&gt;Given an array of positive and negative numbers. Find if there is a subarray (of size at-least one) with 0 sum.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;Example 1:&lt;/u&gt;&lt;br&gt;
Input:&lt;br&gt;
5&lt;br&gt;
4 2 -3 1 6&lt;br&gt;
Output: &lt;br&gt;
Yes&lt;br&gt;
&lt;em&gt;Explanation: &lt;br&gt;
2, -3, 1 is the subarray &lt;br&gt;
with sum 0.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This is similar to the previous question &lt;a href="https://dev.to/tishaag098/subarray-sum-equals-k-4djk"&gt;Subarray sum equals k&lt;/a&gt; but this is &lt;strong&gt;EASY&lt;/strong&gt; compared to that.&lt;/p&gt;

&lt;p&gt;To solve this question click here:(&lt;a href="https://practice.geeksforgeeks.org/problems/subarray-with-0-sum-1587115621/1/"&gt;https://practice.geeksforgeeks.org/problems/subarray-with-0-sum-1587115621/1/&lt;/a&gt;)&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;JAVA 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 static boolean subArrayExists(int arr[],int n)
    {
        for(int i=0;i&amp;lt;n;i++)
        {
            int sum=0;
            for(int j=i;j&amp;lt;n;j++)
            {
                sum+=arr[j];
                if(sum==0)
                  return true;

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Approach 2:&lt;/strong&gt;&lt;br&gt;
This is similar to the previous subarray question that we solved.&lt;br&gt;
&lt;em&gt;Time Complexity: O(n)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;JAVA 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;  static boolean findsum(int arr[],int n)
    {
        //Your code here
        HashMap&amp;lt;Integer,Integer&amp;gt; hm=new HashMap&amp;lt;&amp;gt;();
        hm.put(0,1); // it is imp to put 0 initially in the hashmap
        int sum=0;
        for(int i=0;i&amp;lt;n;i++)
        {
            sum+=arr[i];
            if(hm.containsKey(sum))
                return true;

            hm.put(sum,hm.getOrDefault(sum,0)+1);
        }
        return false;
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Approach 3:&lt;/strong&gt;&lt;br&gt;
We have used HashSet in this as their is no need to use HashMap since we have no use of storing values in key, pair&lt;br&gt;
&lt;em&gt;Time Complexity: O(n)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;JAVA 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 static boolean subArrayExists(int arr[],int n)
    {
         //declare a set data structure
         HashSet&amp;lt;Integer&amp;gt; res=new HashSet&amp;lt;&amp;gt;();
         int sum=0;

         for(int i=0;i&amp;lt;n;i++)
         {
             res.add(sum);//in first step it will add 0
             sum+=arr[i];
             if(res.contains(sum))
                 return true;
         }
         return false;

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

&lt;/div&gt;



</description>
      <category>beginners</category>
      <category>programming</category>
      <category>java</category>
      <category>dsa</category>
    </item>
    <item>
      <title>Subarray Sum Equals K</title>
      <dc:creator>Tisha Agarwal</dc:creator>
      <pubDate>Thu, 10 Feb 2022 13:51:57 +0000</pubDate>
      <link>https://dev.to/tishaag098/subarray-sum-equals-k-4djk</link>
      <guid>https://dev.to/tishaag098/subarray-sum-equals-k-4djk</guid>
      <description>&lt;p&gt;Given an array of integers arr and an integer k, return the total number of continuous subarrays whose sum equals to k.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;Example 1:&lt;/u&gt;&lt;br&gt;
Input: arr = [1,1,1], k = 2&lt;br&gt;
Output: 2&lt;/p&gt;

&lt;p&gt;&lt;u&gt;Example 2:&lt;/u&gt;&lt;br&gt;
Input: arr = [1,2,3], k = 3&lt;br&gt;
Output: 2&lt;/p&gt;

&lt;p&gt;To solve this question click here:(&lt;a href="https://leetcode.com/problems/subarray-sum-equals-k/"&gt;https://leetcode.com/problems/subarray-sum-equals-k/&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;So this is a &lt;strong&gt;MEDIUM LEVEL&lt;/strong&gt; question. When I read this question then I knew that I have solved similar question in the past but still I couldn't solve this on my own. But after searching for a while I came across 3 approaches.&lt;br&gt;
&lt;em&gt;Brute Force----------------------------&amp;gt;Optimized&lt;br&gt;
   O(n^3)------------O(n^2)---------------O(n)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Approach 1:&lt;/strong&gt;&lt;br&gt;
So this is really very simple approach, in this we just need to run 3 nested loops and then calculate the sum of each subarray and check it with the target(k).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;JAVA 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 static int subaaray(int arr[],int n,int X)
    {
        int ans=0;
        for(int i=0;i&amp;lt;n;i++)
        {
            for(int j=i;j&amp;lt;n;j++)
            {
                int sum=0;
                for(int k=i;k&amp;lt;=j;k++)
                {
                    sum+=arr[i];
                }
                if(sum==X)
                   ans++;
            }
        }
        return ans;
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Approach 2:&lt;/strong&gt;&lt;br&gt;
In this we just removed the last for loop and calculated the sum inside j loop itself.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;JAVA 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 static int subaaray(int arr[],int n,int X)
    {
        int ans=0;
        for(int i=0;i&amp;lt;n;i++)
        {
            int sum=0;
            for(int j=i;j&amp;lt;n;j++)
            {
                sum+=arr[i];
                if(sum==X)
                   ans++;
            }
        }
        return ans;
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Approach 3:(Optimized)&lt;/strong&gt;&lt;br&gt;
In this we used HashMap to store the cumulative sum of the array.&lt;br&gt;
&lt;em&gt;Time Complexity: O(n)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;JAVA 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 static int subarray(int arr[],int n,int k)
    {
        HashMap&amp;lt;Integer,Integer&amp;gt; hm=new HashMap&amp;lt;&amp;gt;();
        hm.put(0,1); // it is imp to put 0 initially in the hashmap
        int ans=0,sum=0;
        for(int i=0;i&amp;lt;n;i++)
        {
            sum+=arr[i];
            if(hm.containsKey(sum-k))
                ans+=hm.get(sum-k);

            hm.put(sum,hm.getOrDefault(sum,0)+1);
        }
        return ans;

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

&lt;/div&gt;



</description>
      <category>beginners</category>
      <category>programming</category>
      <category>java</category>
      <category>dsa</category>
    </item>
    <item>
      <title>Longest Consecutive Sequence</title>
      <dc:creator>Tisha Agarwal</dc:creator>
      <pubDate>Sun, 06 Feb 2022 12:58:49 +0000</pubDate>
      <link>https://dev.to/tishaag098/longest-consecutive-sequence-51m6</link>
      <guid>https://dev.to/tishaag098/longest-consecutive-sequence-51m6</guid>
      <description>&lt;p&gt;Given an array of positive integers. Find the length of the longest sub-sequence such that elements in the subsequence are consecutive integers, the consecutive numbers can be in any order.&lt;/p&gt;

&lt;p&gt;Example 1:&lt;br&gt;
Input:&lt;br&gt;
N = 7&lt;br&gt;
&lt;em&gt;a[] = {2,6,1,9,4,5,3}&lt;/em&gt;&lt;br&gt;
Output:&lt;br&gt;
6&lt;br&gt;
Explanation:&lt;br&gt;
The consecutive numbers here&lt;br&gt;
are 1, 2, 3, 4, 5, 6. These 6 &lt;br&gt;
numbers form the longest consecutive&lt;br&gt;
subsequence.&lt;/p&gt;

&lt;p&gt;It is a MEDIUM LEVEL question, asked in &lt;strong&gt;Amazon&lt;/strong&gt;,&lt;strong&gt;Walmart&lt;/strong&gt;, &lt;strong&gt;Microsoft&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;To solve this question click here:(&lt;a href="https://practice.geeksforgeeks.org/problems/longest-consecutive-subsequence2449/1"&gt;https://practice.geeksforgeeks.org/problems/longest-consecutive-subsequence2449/1&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Approach 1:&lt;/strong&gt;&lt;br&gt;
It can be solved if we first sort the array and then find the longest subsequence. Write your code while keeping in mind that the elements can repeat.&lt;br&gt;
This approach is not that efficient as the &lt;em&gt;Time Complexity is O(nlog n+n)&lt;/em&gt; as to sorting algorithm will take time of nlogn.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Approach 2:&lt;/strong&gt;&lt;br&gt;
In second method I used &lt;strong&gt;HashSet&lt;/strong&gt; to:&lt;br&gt;
-remove duplicate elements from the array.&lt;br&gt;
-to find the starting element of the sequence.&lt;br&gt;
&lt;em&gt;Time Complexity: O(n)&lt;br&gt;
Space Complexity: O(n)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;JAVA 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;import java.util.*;
class LongestConsecutiveSeq{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the size of the array:");
        int n=sc.nextInt();
        int[] arr=new int[n];
        System.out.println("Enter the elements of the array:");
        for(int i=0;i&amp;lt;n;i++)
        {
            arr[i]=sc.nextInt();
        }
        System.out.println(findLongestConseqSubseq(arr,n));
    }
    static int findLongestConseqSubseq(int arr[], int n)
    {
        //creating hashSet to remove duplicacy
        HashSet&amp;lt;Integer&amp;gt; st=new HashSet&amp;lt;&amp;gt;();

        for(int i=0;i&amp;lt;n;i++)
            st.add(arr[i]);

        int ans=0,c=1;
        for(int i=0;i&amp;lt;n;i++)
        {
            c=1;
            if(!st.contains(arr[i]-1))
            {
                int val=arr[i]+1;
                while(st.contains(val))
                {
                    val++;
                    c++;
                }
                ans=Math.max(ans,c);
            }
        }
        return ans;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>java</category>
      <category>programming</category>
      <category>dsa</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Rearrange array in alternating positive &amp; negative items with O(1) extra space </title>
      <dc:creator>Tisha Agarwal</dc:creator>
      <pubDate>Sat, 05 Feb 2022 10:27:32 +0000</pubDate>
      <link>https://dev.to/tishaag098/rearrange-array-in-alternating-positive-negative-items-with-o1-extra-space-3d11</link>
      <guid>https://dev.to/tishaag098/rearrange-array-in-alternating-positive-negative-items-with-o1-extra-space-3d11</guid>
      <description>&lt;p&gt;Given an array of positive and negative numbers, arrange them in an alternate fashion such that every positive number is followed by negative and vice-versa maintaining the order of appearance. &lt;br&gt;
Number of positive and negative numbers need not be equal. If there are more positive numbers they appear at the end of the array. If there are more negative numbers, they too appear in the end of the array.&lt;/p&gt;

&lt;p&gt;Examples : &lt;/p&gt;

&lt;p&gt;Input:  arr[] = {1, 2, 3, -4, -1, 4}&lt;br&gt;
Output: arr[] = {-4, 1, -1, 2, 3, 4}&lt;/p&gt;

&lt;p&gt;Input:  arr[] = {-5, -2, 5, 2, 4, 7, 1, 8, 0, -8}&lt;br&gt;
output: arr[] = {-5, 5, -2, 2, -8, 4, 7, 1, 8, 0}&lt;/p&gt;

&lt;p&gt;Asked in &lt;strong&gt;Amazon&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;At even position:&lt;/strong&gt; negative numbers&lt;br&gt;
&lt;strong&gt;At odd position:&lt;/strong&gt; positive numbers&lt;/p&gt;

&lt;p&gt;&lt;code&gt;i&lt;/code&gt;--&amp;gt; &lt;em&gt;It will find the negative element in the array&lt;/em&gt;&lt;br&gt;
&lt;code&gt;j&lt;/code&gt;--&amp;gt; &lt;em&gt;It will find the positive element in the array&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;JAVA 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;import java.util.*;
public class RearrangeArrayInAlterPos {
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the size of the array:");
        int n=sc.nextInt();
        int[] arr=new int[n];
        System.out.println("Enter the elements of the array:");
        for(int i=0;i&amp;lt;n;i++)
        {
            arr[i]=sc.nextInt();
        }
        reArrange(arr, n);
    }
    //doing right rotate to shift the elements to the right
    //this is done to maintain the order of the elements
    public static void rotate(int arr[],int start,int end)
    {
        int temp=arr[end];
        for(int i=end-1;i&amp;gt;=start;i--)
        {
            arr[i+1]=arr[i];
        }
        arr[start]=temp;
    }
    public static void reArrange(int arr[],int n)
    {
        int i=0,j=0,k=0;
        while(i&amp;lt;n &amp;amp;&amp;amp; j&amp;lt;n &amp;amp;&amp;amp; k&amp;lt;n)
        {
            if(k%2==0)//if k is even
            {
                if(arr[k]&amp;gt;=0)
                {
                    i=k; j=k;
                    while(i&amp;lt;n &amp;amp;&amp;amp; arr[i]&amp;gt;=0)
                       i++;
                    if(i&amp;gt;=n) break;
                    else
                       rotate(arr,j,i);
                }
            }
            else{
                if(arr[k]&amp;lt;0)
                {
                    i=k;
                    j=k;
                    while(j&amp;lt;n &amp;amp;&amp;amp; arr[i]&amp;lt;0)
                       j++;
                    if(j&amp;gt;=n) break;
                    else
                    rotate(arr,i,j);
                }
            }
            k++;

        }
        for(int l=0;l&amp;lt;n;l++)
           System.out.print(arr[l]+" ");

    }

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

&lt;/div&gt;



</description>
      <category>programming</category>
      <category>beginners</category>
      <category>java</category>
      <category>dsa</category>
    </item>
    <item>
      <title>Merge Intervals</title>
      <dc:creator>Tisha Agarwal</dc:creator>
      <pubDate>Fri, 04 Feb 2022 21:08:28 +0000</pubDate>
      <link>https://dev.to/tishaag098/merge-intervals-22db</link>
      <guid>https://dev.to/tishaag098/merge-intervals-22db</guid>
      <description>&lt;p&gt;Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input.&lt;/p&gt;

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

&lt;p&gt;Input: &lt;em&gt;intervals = [[1,3],[2,6],[8,10],[15,18]]&lt;/em&gt;&lt;br&gt;
Output: [[1,6],[8,10],[15,18]]&lt;br&gt;
&lt;em&gt;Explanation: Since intervals [1,3] and [2,6] overlaps, merge them into [1,6].&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;To solve the question click here:(&lt;a href="https://leetcode.com/problems/merge-intervals/"&gt;https://leetcode.com/problems/merge-intervals/&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;The new thing that I learned in this question is about &lt;strong&gt;comparator interface&lt;/strong&gt; in java. Using a comparator, we can sort the elements based on data members. For instance, it may be on roll no, name, age, or anything else.&lt;/p&gt;

&lt;p&gt;In this question, I used comparator to sort the intervals according to the 1st interval.&lt;br&gt;
       &lt;code&gt;Arrays.sort(intervals,(o1,o2)-&amp;gt;{&lt;br&gt;
            return o1[0]-o2[0];&lt;br&gt;
        });&lt;/code&gt;&lt;br&gt;
&lt;em&gt;For eg:[2,4] [1,5] [7,19] [6,7] -------&amp;gt;[1,5] [2,4] [6,7] [7,19]&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Approach 1:&lt;/strong&gt;&lt;br&gt;
Using Stack method&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;First I have created a stack and pushed the first interval in the stack.([1,3] is pushed in the stack st)&lt;/li&gt;
&lt;li&gt;Then I iterated the loop from 2nd interval to the end of the array.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;JAVA 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 static void merge(int[][] intervals){

        Arrays.sort(intervals,(o1,o2)-&amp;gt;{
            return o1[0]-o2[0];
        });

        Stack&amp;lt;int[]&amp;gt; st=new Stack&amp;lt;&amp;gt;();
        st.push(intervals[0]);//[1,3] is pushed in the stack

        for(int i=1;i&amp;lt;intervals.length;i++)
        {
            int[] current=intervals[i];
            int[] last_inserted=st.peek();
             if(current[0]&amp;lt;=last_inserted[1])
             {
                 st.pop();
                 st.push(new int[]{last_inserted[0],Math.max(last_inserted[1],current[1])});
             }
             else{
                 st.push(current);
             }
        }
        // Stack&amp;lt;int[]&amp;gt; res=new Stack&amp;lt;&amp;gt;();
        // while(st.size()&amp;gt;0)
        // {
        //     res.push(st.pop());
        // }
        // while(res.size()&amp;gt;0)
        // {
        //     System.out.println(res.pop());
        // }
        int[][] res = new int[st.size()][2];
        st.toArray(res);
        System.out.println("----------");
        for(int i=0;i&amp;lt;res.length;i++)
        {
          System.out.print("["+res[i][0]+",");
          System.out.print(res[i][1]+"]"+" ");
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Approach 2:&lt;/strong&gt;&lt;br&gt;
Used ArrayList to solve the problem in &lt;strong&gt;Space Complexity: O(n)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;JAVA 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;import java.util.*;
class merge_intervals{

    public static void main(String[] args) throws Exception
    {
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter the number of elements: ");
        int n=sc.nextInt();
        int[][] arr=new int[n][2];

        for(int i=0;i&amp;lt;n;i++)
        {
            arr[i][0]=sc.nextInt();
            arr[i][1]=sc.nextInt();
        }
        merge(arr);
    }
public static void merge(int[][] intervals)
    {
        Arrays.sort(intervals,(o1,o2)-&amp;gt;{
            return o1[0]-o2[0];
        });
        ArrayList&amp;lt;int[]&amp;gt; ans=new ArrayList&amp;lt;&amp;gt;();

        int start=intervals[0][0];// start=1
        int last=intervals[0][1]; //end=3

        for(int i=1;i&amp;lt;intervals.length;i++)
        {
            if(intervals[i][0]&amp;lt;=last &amp;amp;&amp;amp; intervals[i][0]&amp;gt;=start)
            {
                last=Math.max(last,intervals[i][1]);
            }
            else
            {
                ans.add(new int[]{start,last});
                start=intervals[i][0];
                last=intervals[i][1];
            }
        }
        ans.add(new int[]{start,last});
        int[][] res=new int[ans.size()][2];
        ans.toArray(res);
        System.out.println("-----------------");
        for(int i=0;i&amp;lt;ans.size();i++)
        {
            System.out.print("["+res[i][0]+",");
            System.out.print(res[i][1]+"]"+" ");
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>java</category>
      <category>dsa</category>
      <category>array</category>
      <category>programming</category>
    </item>
    <item>
      <title>Factorial of a large Number</title>
      <dc:creator>Tisha Agarwal</dc:creator>
      <pubDate>Thu, 03 Feb 2022 20:08:28 +0000</pubDate>
      <link>https://dev.to/tishaag098/factorial-of-a-large-number-31fi</link>
      <guid>https://dev.to/tishaag098/factorial-of-a-large-number-31fi</guid>
      <description>&lt;p&gt;Given an integer N, find its factorial.&lt;/p&gt;

&lt;p&gt;Example 1:&lt;br&gt;
Input: N = 5&lt;br&gt;
Output: 120&lt;br&gt;
&lt;em&gt;Explanation : 5! = 1*2*3*4*5 = 120&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Example 2:&lt;br&gt;
Input: N = 10&lt;br&gt;
Output: 3628800&lt;br&gt;
&lt;em&gt;Explanation :&lt;br&gt;
10! = 1*2*3*4*5*6*7*8*9*10 = 3628800&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;To solve this question click here:(&lt;a href="https://practice.geeksforgeeks.org/problems/factorials-of-large-numbers2508/1#"&gt;https://practice.geeksforgeeks.org/problems/factorials-of-large-numbers2508/1#&lt;/a&gt;);&lt;/p&gt;

&lt;p&gt;Asked in companies like &lt;strong&gt;Adobe&lt;/strong&gt;, &lt;strong&gt;BrowserStack&lt;/strong&gt;, &lt;strong&gt;MakeMyTrip&lt;/strong&gt;, &lt;strong&gt;Microsoft&lt;/strong&gt;, &lt;strong&gt;Philips&lt;/strong&gt;, &lt;strong&gt;Samsung&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It is a &lt;strong&gt;MEDIUM LEVEL&lt;/strong&gt; question, you must have calculated factorial of a number. But this question is a bit different, in this we have to calculate factorial of a &lt;strong&gt;large number&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Factorial of large numbers cannot be stored in &lt;code&gt;int&lt;/code&gt; or &lt;code&gt;long&lt;/code&gt; or &lt;code&gt;long long&lt;/code&gt;. So in this case we use ArrayList or LinkedList to solve the question.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;JAVA 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;static ArrayList&amp;lt;Integer&amp;gt; factorial(int N){
        //code here
        ArrayList&amp;lt;Integer&amp;gt; res=new ArrayList&amp;lt;&amp;gt;();
        res.add(0,1); //adding 1 in the arraylist
        int size=1;
        int carry=0,val=2;

        while(val&amp;lt;=N)
        {
            for(int i=size-1;i&amp;gt;=0;i--)
            {
                int temp=res.get(i)*val + carry;
                //store the last digit at index and add remaining to carry
                res.set(i,temp%10);
                //update carry
                carry=temp/10;
            }
            while(carry!=0)
            {
                res.add(0,carry%10);
                carry=carry/10;
                size++;
            }
            val++;

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

&lt;/div&gt;



</description>
      <category>java</category>
      <category>programming</category>
      <category>dsa</category>
    </item>
    <item>
      <title>Common Element in 3 sorted array</title>
      <dc:creator>Tisha Agarwal</dc:creator>
      <pubDate>Tue, 01 Feb 2022 16:24:47 +0000</pubDate>
      <link>https://dev.to/tishaag098/common-element-in-3-sorted-array-2o0c</link>
      <guid>https://dev.to/tishaag098/common-element-in-3-sorted-array-2o0c</guid>
      <description>&lt;p&gt;Given three arrays sorted in increasing order. Find the elements that are common in all three arrays.&lt;br&gt;
Note: can you take care of the duplicates without using any additional Data Structure?&lt;/p&gt;

&lt;p&gt;Example 1:&lt;br&gt;
Input:&lt;br&gt;
&lt;em&gt;n1 = 6; A = {1, 5, 10, 20, 40, 80}&lt;br&gt;
n2 = 5; B = {6, 7, 20, 80, 100}&lt;br&gt;
n3 = 8; C = {3, 4, 15, 20, 30, 70, 80, 120}&lt;/em&gt;&lt;br&gt;
Output: 20 80&lt;br&gt;
_Explanation: 20 and 80 are the only&lt;br&gt;
common elements in A, B and C.&lt;br&gt;
_&lt;br&gt;
Example 2:&lt;br&gt;
Input:&lt;br&gt;
_n1=3; A={3, 3, 3}&lt;br&gt;
n2=3; B={3, 3, 3}&lt;br&gt;
n3=3; C={3, 3, 3}&lt;br&gt;
_Output: 3&lt;/p&gt;

&lt;p&gt;This question was asked in companies like MAQ Software, Microsoft ,VMWare&lt;/p&gt;

&lt;p&gt;To solve this question click here:(&lt;a href="https://practice.geeksforgeeks.org/problems/common-elements1132/1#"&gt;https://practice.geeksforgeeks.org/problems/common-elements1132/1#&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Approach 1:&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;Time Complexity: O(n1+n2+n3)&lt;br&gt;
Space Complexity: O(1)&lt;/em&gt;&lt;br&gt;
In this we have used three pointer i, j, k to iterate through the 3 sorted array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.util.*;

public class commonEle_in_3sorted {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter n1: ");
        int n1 = sc.nextInt();
        System.out.print("Enter n2: ");
        int n2 = sc.nextInt();
        System.out.print("Enter n3: ");
        int n3 = sc.nextInt();
        int[] A = new int[n1];
        int[] B = new int[n2];
        int[] C = new int[n3];
        System.out.println("Enter the elements of the array A: ");
        for (int i = 0; i &amp;lt; n1; i++) {
            A[i] = sc.nextInt();
        }
        System.out.println("Enter the elements of the array B: ");
        for (int i = 0; i &amp;lt; n2; i++) {
            B[i] = sc.nextInt();
        }
        System.out.println("Enter the elements of the array C: ");
        for (int i = 0; i &amp;lt; n3; i++) {
            C[i] = sc.nextInt();
        }
        M1commonElement(A, B, C, n1, n2, n3);
    }

    public static void M1commonElement(int A[], int B[], int C[], int n1, int n2, int n3) {
        int i = 0, j = 0, k = 0;
        ArrayList&amp;lt;Integer&amp;gt; al = new ArrayList&amp;lt;&amp;gt;();
        while (i &amp;lt; n1 &amp;amp;&amp;amp; j &amp;lt; n2 &amp;amp;&amp;amp; k &amp;lt; n3) {
            if (A[i] == B[j] &amp;amp;&amp;amp; A[i] == C[k]) {
                al.add(A[i]);// common element
                int ele = A[i];
                // to remove duplicacy
                while (i&amp;lt;n1 &amp;amp;&amp;amp; A[i]==ele)
                    i++;
                while (j&amp;lt;n2 &amp;amp;&amp;amp; B[j] == ele)
                    j++;
                while (k&amp;lt;n3 &amp;amp;&amp;amp; C[k] == ele)
                    k++;
            } 
            else if (A[i] &amp;lt; B[j])
                i++;
            else if (B[j] &amp;lt; C[k])
                j++;
            else
                k++;

        }
        for (int ele : al) {
            System.out.println(ele);
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Approach 2:&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;Time Complexity: O(n1+n2+n3)&lt;br&gt;
Space Complexity: O(n1+n2+n3)&lt;/em&gt;&lt;br&gt;
In this we have use HashMap to store the three sorted array. And then we have again iterated through the loop and checked if that element is present in the HashMap (&lt;code&gt;hm1&lt;/code&gt;,&lt;code&gt;hm2&lt;/code&gt;,&lt;code&gt;hm3&lt;/code&gt;) or not.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; public static void M2commonElement(int A[], int B[], int C[], int n1, int n2, int n3) {
        HashMap&amp;lt;Integer, Integer&amp;gt; hm1 = new HashMap&amp;lt;&amp;gt;();
        HashMap&amp;lt;Integer, Integer&amp;gt; hm2 = new HashMap&amp;lt;&amp;gt;();
        HashMap&amp;lt;Integer, Integer&amp;gt; hm3 = new HashMap&amp;lt;&amp;gt;();
        for (int i = 0; i &amp;lt; n1; i++)
            hm1.put(A[i], i);
        for (int i = 0; i &amp;lt; n2; i++)
            hm2.put(B[i], i);
        for (int i = 0; i &amp;lt; n3; i++)
            hm3.put(C[i], i);

        ArrayList&amp;lt;Integer&amp;gt; arr = new ArrayList&amp;lt;&amp;gt;();
        for (int i = 0; i &amp;lt; n1; i++) {
            if (hm1.containsKey(A[i]) &amp;amp;&amp;amp; hm2.containsKey(A[i]) &amp;amp;&amp;amp; hm3.containsKey(A[i])) {
                arr.add(A[i]);
                hm1.remove(A[i]);
            }
        }
        for (int ele : arr)
            System.out.print(arr);
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>java</category>
      <category>array</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
