<?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: shashi</title>
    <description>The latest articles on DEV Community by shashi (@mshashikanth1).</description>
    <link>https://dev.to/mshashikanth1</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%2F1198899%2F3f96bf08-bca0-481b-8214-e9c3ddb7a5db.jpeg</url>
      <title>DEV Community: shashi</title>
      <link>https://dev.to/mshashikanth1</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mshashikanth1"/>
    <language>en</language>
    <item>
      <title>Find missing in second array</title>
      <dc:creator>shashi</dc:creator>
      <pubDate>Fri, 19 Apr 2024 06:45:19 +0000</pubDate>
      <link>https://dev.to/mshashikanth1/find-missing-in-second-array-5cbj</link>
      <guid>https://dev.to/mshashikanth1/find-missing-in-second-array-5cbj</guid>
      <description>&lt;p&gt;Given two arrays a of size n and b of size m, the task is to find numbers which are present in the first array, but not present in the second array.&lt;/p&gt;

&lt;blockquote&gt;
&lt;/blockquote&gt;

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

&lt;blockquote&gt;
&lt;p&gt;Input: &lt;br&gt;
n = 6, m = 5&lt;br&gt;
a[] = {1, 2, 3, 4, 5, 10}&lt;br&gt;
b[] = {2, 3, 1, 0, 5}&lt;br&gt;
Output: &lt;br&gt;
4 10&lt;br&gt;
Explanation: &lt;br&gt;
4 and 10 are present in first array, but not in second array.&lt;br&gt;
Example 2:&lt;br&gt;
Input: &lt;br&gt;
n = 5, m = 5&lt;br&gt;
a[] = {4, 3, 5, 9, 11}&lt;br&gt;
b[] = {4, 9, 3, 11, 10}&lt;br&gt;
Output: &lt;br&gt;
5&lt;br&gt;&lt;br&gt;
Explanation: &lt;br&gt;
Second array does not contain element 5.&lt;br&gt;
Your Task:&lt;br&gt;
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function findMissing() that takes array a, array b, integer n, and integer m as parameters and returns an array that contains the missing elements and the order of the elements should be the same as they are in array a.&lt;br&gt;
Expected Time Complexity: O(n+m).&lt;br&gt;
Expected Auxiliary Space: O(m).&lt;br&gt;
Constraints:&lt;br&gt;
1 ≤ n, m ≤ 105&lt;br&gt;
-109 ≤ A[i], B[i] ≤ 109**&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

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

    //Time :O(n +m) Space :O(m)
    ArrayList&amp;lt;Integer&amp;gt; findMissing(int a[], int b[], int n, int m)
    {
        Set&amp;lt;Integer&amp;gt; hSet=new HashSet&amp;lt;&amp;gt;();
        for(int i: b) hSet.add(i);

        ArrayList&amp;lt;Integer&amp;gt; ans=new ArrayList&amp;lt;&amp;gt;();
        for(int i: a) if(!hSet.contains(i)) ans.add(i);

        return ans;
    }
}

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

&lt;/div&gt;



</description>
      <category>hashtable</category>
      <category>hashmap</category>
      <category>hashset</category>
      <category>arrays</category>
    </item>
    <item>
      <title>Count Pairs in an Array</title>
      <dc:creator>shashi</dc:creator>
      <pubDate>Wed, 17 Apr 2024 08:51:21 +0000</pubDate>
      <link>https://dev.to/mshashikanth1/count-pairs-in-an-array-30dc</link>
      <guid>https://dev.to/mshashikanth1/count-pairs-in-an-array-30dc</guid>
      <description>&lt;h2&gt;
  
  
  Count Pairs in an Array
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Hard&lt;/strong&gt;Accuracy: &lt;strong&gt;48.25%&lt;/strong&gt;Submissions: &lt;strong&gt;24K+&lt;/strong&gt;Points: &lt;strong&gt;8&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Given an array &lt;strong&gt;arr&lt;/strong&gt; of &lt;strong&gt;n&lt;/strong&gt; integers, count all pairs (&lt;strong&gt;arr[i]&lt;/strong&gt;, &lt;strong&gt;arr[j]&lt;/strong&gt;) in it such that &lt;strong&gt;i*arr[i]&lt;/strong&gt; &amp;gt; &lt;strong&gt;j*arr[j] **and **0 ≤ i &amp;lt; j &amp;lt; n&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: 0-based Indexing is followed.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input :
n = 4
arr[] = {8, 4, 2, 1}
Output :
2
Explanation:
If we see the array after operations
[0*8, 1*4, 2*2, 3*1] =&amp;gt; [0, 4, 4, 3]
Pairs which hold the condition i*arr[i] &amp;gt; j*arr[j] are (4,1) and (2,1), so in total 2 pairs are available.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input :
n = 7
arr[] = {5, 0, 10, 2, 4, 1, 6}
Output:
5
Explanation :
Pairs which hold the condition i*arr[i] &amp;gt; j*arr[j] are (10,2), (10,4), (10,1), (2,1) and (4,1), so in total 5 pairs are there.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Your Task: **&lt;br&gt;
You don’t need to read input or print anything. Your task is to complete the function **countPairs()&lt;/strong&gt; which takes the array &lt;strong&gt;arr[]&lt;/strong&gt; and its size **n **as inputs and returns the required result.&lt;/p&gt;

&lt;p&gt;**Expected Time Complexity: **O(n*log(n))&lt;br&gt;
**Expected Auxiliary Space: **O(n*log(n))&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Constraints:&lt;/strong&gt;&lt;br&gt;
1 &lt;strong&gt;≤&lt;/strong&gt; n *&lt;em&gt;≤ **104&lt;br&gt;
0 ≤ arr[i] *&lt;/em&gt;≤ **104&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Topic Tags&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Report An Issue&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//User function Template for Java


//define a pair class 


class Solution {  

    //Time :O(n^2) space :O(1)
    static int countPairs1(int arr[], int n) 
    {
         // Your code goes here
         int c=0;
         for(int i=0;i&amp;lt;n;i++){
             for(int j=i+1; j&amp;lt;n ; j++){
                 if(i*arr[i] &amp;gt; j*arr[j]) c++;
             }
         }
         return c;
    }

    /*Time : O(n*(log(n)) Space : O(n*log(n))*/
    static int countPairs(int arr[], int n) 
    {
        //find the product of index and value store it in same array
        for(int i=0;i&amp;lt;n;i++)arr[i]*=i;

        return mergeSort(arr,0, n-1);
    }
    static int mergeSort(int[]arr, int l, int r){
        int count = 0;
        if(l&amp;lt;r){
            int mid = l+(r-l)/2;
            count += mergeSort(arr,l,mid);
            count += mergeSort(arr, mid+1,r);

            count += merge(arr,l,mid,r);
        }
        return count;
    }

    static int merge(int[]arr, int l, int mid, int r){
        int count = 0;

        int j = mid+1;

        /*  finding pair */
        for(int i=l; i&amp;lt;=mid; i++){

         while(j&amp;lt;=r &amp;amp;&amp;amp; arr[i]&amp;gt;arr[j] ) j++;
          count+= (j-(mid+1));

        }


      /*calculate the size of left sub array &amp;amp; right sub array*/
        int n = mid-l+1;
        int m = r-mid;


    /*declare the left sub array and righ sub array*/
        int[]left = new int[n];
        int[]right = new int[m];


    /*copy the data from input array to left , right sub array*/
        for(int i=0;i&amp;lt;n;i++){
            left[i] = arr[l+i];
        }
        for(int i=0;i&amp;lt;m;i++){
            right[i] = arr[mid+i+1];
        }



        /*declare the iterator indx variables for input array, left, right sub array */
        int i=0;
        j = 0;
        int k = l;

        /*iterate untill out of  bounce */
        while(i&amp;lt;n &amp;amp;&amp;amp; j&amp;lt;m){

            /*copyt the min element from right, left subarray to input array*/
            if(left[i]&amp;lt;=right[j]){
                arr[k++] = left[i++];
            }else{
                arr[k++] = right[j++];
            }
        }

        /*copyt the elements remaining from both the arrays*/
        while(i&amp;lt;n)arr[k++] = left[i++];
        while(j&amp;lt;m)arr[k++] = right[j++];

        return count;
    }

}

/*

Count Pairs in an Array
HardAccuracy: 48.25%Submissions: 24K+Points: 8
Given an array arr of n integers, count all pairs (arr[i], arr[j]) in it such that i*arr[i] &amp;gt; j*arr[j] and 0 ≤ i &amp;lt; j &amp;lt; n.

Note: 0-based Indexing is followed.

Example 1:

Input :
n = 4
arr[] = {8, 4, 2, 1}
Output :
2
Explanation:
If we see the array after operations
[0*8, 1*4, 2*2, 3*1] =&amp;gt; [0, 4, 4, 3]
Pairs which hold the condition i*arr[i] &amp;gt; j*arr[j] are (4,1) and (2,1), so in total 2 pairs are available.
Example 2:

Input :
n = 7
arr[] = {5, 0, 10, 2, 4, 1, 6}
Output:
5
Explanation :
Pairs which hold the condition i*arr[i] &amp;gt; j*arr[j] are (10,2), (10,4), (10,1), (2,1) and (4,1), so in total 5 pairs are there.
Your Task:  
You don't need to read input or print anything. Your task is to complete the function countPairs() which takes the array arr[] and its size n as inputs and returns the required result.

Expected Time Complexity: O(n*log(n))
Expected Auxiliary Space: O(n*log(n))

Constraints:
1 ≤ n ≤ 104
0 ≤ arr[i] ≤ 104


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

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ujAWDLLX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/7196/1%2ASg5FkwrhXQ7DBnl_PMgWqQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ujAWDLLX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/7196/1%2ASg5FkwrhXQ7DBnl_PMgWqQ.png" width="800" height="436"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>[623. Add One Row to Tree](https://leetcode.com/problems/add-one-row-to-tree/)</title>
      <dc:creator>shashi</dc:creator>
      <pubDate>Tue, 16 Apr 2024 11:47:18 +0000</pubDate>
      <link>https://dev.to/mshashikanth1/623-add-one-row-to-treehttpsleetcodecomproblemsadd-one-row-to-tree-27og</link>
      <guid>https://dev.to/mshashikanth1/623-add-one-row-to-treehttpsleetcodecomproblemsadd-one-row-to-tree-27og</guid>
      <description>&lt;h2&gt;
  
  
  &lt;a href="https://leetcode.com/problems/add-one-row-to-tree/" rel="noopener noreferrer"&gt;623. Add One Row to Tree&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Solved&lt;/p&gt;

&lt;p&gt;Medium&lt;/p&gt;

&lt;p&gt;Topics&lt;/p&gt;

&lt;p&gt;Companies&lt;/p&gt;

&lt;p&gt;Given the root of a binary tree and two integers val and depth, add a row of nodes with value val at the given depth depth.&lt;/p&gt;

&lt;p&gt;Note that the root node is at depth 1.&lt;/p&gt;

&lt;p&gt;The adding rule is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Given the integer depth, for each not null tree node cur at the depth depth - 1, create two tree nodes with value val as cur's left subtree root and right subtree root.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;cur's original left subtree should be the left subtree of the new left subtree root.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;cur's original right subtree should be the right subtree of the new right subtree root.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If depth == 1 that means there is no depth depth - 1 at all, then create a tree node with value val as the new root of the whole original tree, and the original tree is the new root's left subtree.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ClJiT1D---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2000/0%2A0naObfvtAR4bd6TO.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ClJiT1D---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2000/0%2A0naObfvtAR4bd6TO.jpg" width="800" height="370"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: root = [4,2,6,3,1,5], val = 1, depth = 2
Output: [4,1,1,2,null,null,6,3,1,5]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

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

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Cc56fQ6c--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2000/0%2ATxqv-UGBudgY2feh.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Cc56fQ6c--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2000/0%2ATxqv-UGBudgY2feh.jpg" width="762" height="422"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: root = [4,2,null,3,1], val = 1, depth = 3
Output: [4,2,null,1,1,3,null,null,1]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The number of nodes in the tree is in the range [1, 104].&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The depth of the tree is in the range [1, 104].&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;-100 &amp;lt;= Node.val &amp;lt;= 100&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;-105 &amp;lt;= val &amp;lt;= 105&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;1 &amp;lt;= depth &amp;lt;= the depth of tree + 1&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Seen this question in a real interview before?&lt;/p&gt;

&lt;p&gt;1/5&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public TreeNode addOneRow(TreeNode root, int val, int depth) {


       return  dfs(root,val,depth,1,false);

    }
    public TreeNode dfs(TreeNode root, int val, int depth, int curr,boolean isLeft){


        if(root==null) {
            if(depth==1) root=new TreeNode(val);
            return root;
        }

        if(depth==1 &amp;amp;&amp;amp; root!=null) {
                TreeNode newNode=new TreeNode(val);
                newNode.left=root;
                root=newNode;
                return newNode;
        }

        if(curr+1==depth){
                TreeNode newNode=new TreeNode(val);
                newNode.left=root.left;
                root.left=newNode;

                TreeNode newNode1=new TreeNode(val);
                newNode1.right=root.right;
                root.right=newNode1;

                return root;
            }

        dfs(root.left,val,depth,curr+1,true);
        dfs(root.right,val,depth,curr+1,false);
        return root;
    } 
}
/**

623. Add One Row to Tree
Solved
Medium
Topics
Companies
Given the root of a binary tree and two integers val and depth, add a row of nodes with value val at the given depth depth.

Note that the root node is at depth 1.

The adding rule is:

Given the integer depth, for each not null tree node cur at the depth depth - 1, create two tree nodes with value val as cur's left subtree root and right subtree root.
cur's original left subtree should be the left subtree of the new left subtree root.
cur's original right subtree should be the right subtree of the new right subtree root.
If depth == 1 that means there is no depth depth - 1 at all, then create a tree node with value val as the new root of the whole original tree, and the original tree is the new root's left subtree.

Example 1:


Input: root = [4,2,6,3,1,5], val = 1, depth = 2
Output: [4,1,1,2,null,null,6,3,1,5]
Example 2:


Input: root = [4,2,null,3,1], val = 1, depth = 3
Output: [4,2,null,1,1,3,null,null,1]

Constraints:

The number of nodes in the tree is in the range [1, 104].
The depth of the tree is in the range [1, 104].
-100 &amp;lt;= Node.val &amp;lt;= 100
-105 &amp;lt;= val &amp;lt;= 105
1 &amp;lt;= depth &amp;lt;= the depth of tree + 1
 */
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0grL4eal--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/7172/1%2A0L4xJIccul0iaLFg1jrMrw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0grL4eal--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/7172/1%2A0L4xJIccul0iaLFg1jrMrw.png" width="800" height="426"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
