<?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: Vikram Singh Gurjar</title>
    <description>The latest articles on DEV Community by Vikram Singh Gurjar (@vpsinghg).</description>
    <link>https://dev.to/vpsinghg</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%2F846933%2F82cd12bc-3af1-451c-bf5e-08e4ec0ce05b.png</url>
      <title>DEV Community: Vikram Singh Gurjar</title>
      <link>https://dev.to/vpsinghg</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vpsinghg"/>
    <language>en</language>
    <item>
      <title>Min Stack - InterviewBit Problem</title>
      <dc:creator>Vikram Singh Gurjar</dc:creator>
      <pubDate>Sun, 17 Apr 2022 12:48:41 +0000</pubDate>
      <link>https://dev.to/vpsinghg/min-stack-interviewbit-problem-1dg2</link>
      <guid>https://dev.to/vpsinghg/min-stack-interviewbit-problem-1dg2</guid>
      <description>&lt;p&gt;Hi Devs, Happy Coding&lt;/p&gt;

&lt;p&gt;Today, I will be solving a problem titled &lt;a href="https://www.interviewbit.com/problems/min-stack/"&gt;Min Stack&lt;/a&gt; which is based on Stack Data Structure.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Difficulty Level&lt;/strong&gt; - Medium&lt;br&gt;
&lt;strong&gt;Asked In:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;YAHOO&lt;/li&gt;
&lt;li&gt;AMAZON&lt;/li&gt;
&lt;li&gt;ADOBE&lt;/li&gt;
&lt;li&gt;MICROSOFT&lt;/li&gt;
&lt;li&gt;GRAB&lt;/li&gt;
&lt;li&gt;TWITTER&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  &lt;strong&gt;Understanding The Problem&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Problem Description&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;push(x) – Push element &lt;code&gt;x&lt;/code&gt; onto the stack.&lt;/li&gt;
&lt;li&gt;pop() – Removes the element on top of the stack.&lt;/li&gt;
&lt;li&gt;top() – Get the top element.&lt;/li&gt;
&lt;li&gt;getMin() – Retrieve the minimum element in the stack.&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Problem Note&lt;/strong&gt;&lt;br&gt;
All the operations above mentioned have to be constant time (&lt;code&gt;O(1)&lt;/code&gt;) operations.&lt;/p&gt;

&lt;p&gt;As we read the problem description we are not clear about a few cases. So we should ask the Interviewer about those cases.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Here we can ask the following questions&lt;/strong&gt;&lt;br&gt;
Q: What should getMin() do on an empty stack? &lt;br&gt;
A: In this case, return -1.&lt;/p&gt;

&lt;p&gt;Q: What should pop do on an empty stack? &lt;br&gt;
A: In this case, nothing. &lt;/p&gt;

&lt;p&gt;Q: What should top() do on an empty stack?&lt;br&gt;
A: In this case, return -1&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&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;Consider the following stack that we want
4  --&amp;gt; TOP
2
5
3

When getMin() is called it should return 2, 
which is the minimum element in the current stack. 

If we do pop two times on stack, the stack becomes
5  --&amp;gt; TOP
3
When getMin() is called, it should return 3
which is the minimum in the current stack.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Solutions&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Brute Force Solution&lt;/strong&gt; - We can use two stack one for storing out main stack &lt;code&gt;main_stack&lt;/code&gt; and other auxiliary stack for storing &lt;code&gt;min_value&lt;/code&gt;. Our push and pop operations should be in such a way that minimum value in stack should be always at the top of &lt;code&gt;min_stack&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;We will try to optimise the space complexity using single stack.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Brute Force Solution&lt;/strong&gt; - &lt;br&gt;
We have two stack &lt;code&gt;main_stack&lt;/code&gt; and &lt;code&gt;min_stack&lt;/code&gt; as discussed above. Our push, pop, getMin and top operations will be following .&lt;br&gt;
&lt;strong&gt;push(x)&lt;/strong&gt; - we will directly push the &lt;code&gt;x&lt;/code&gt; in &lt;code&gt;main_stack&lt;/code&gt; &lt;code&gt;main_stack.push(x)&lt;/code&gt;. our new minimum_value will be &lt;code&gt;new_min = min(x, min_stack.top())&lt;/code&gt; so now push the &lt;code&gt;new_min&lt;/code&gt; in &lt;code&gt;min_stack&lt;/code&gt; &lt;code&gt;min_stack.push(new_min)&lt;/code&gt;. Both push are &lt;strong&gt;C++ standard template library methods in stack container&lt;/strong&gt; and we know both have &lt;code&gt;O(1)&lt;/code&gt; time complexity. So overall push will be also of &lt;code&gt;O(1)&lt;/code&gt; time complexity.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pop()&lt;/strong&gt; - if stack is empty then do nothing. else pop the top element from &lt;code&gt;main_stack&lt;/code&gt; as well as &lt;code&gt;min_stack&lt;/code&gt;. Both pop methods are from &lt;strong&gt;c++ stl&lt;/strong&gt; and both have &lt;code&gt;O(1)&lt;/code&gt; time complexity so overall complexity will be of &lt;code&gt;O(1)&lt;/code&gt; as well.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;top()&lt;/strong&gt; - if &lt;code&gt;main_stack&lt;/code&gt; is empty then return -1 else return &lt;code&gt;main_stack.top()&lt;/code&gt;. top method also has time complexity of &lt;code&gt;O(1)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;getMin()&lt;/strong&gt;- if &lt;code&gt;min_stack&lt;/code&gt; is empty then return -1 else return &lt;code&gt;min_stack.top()&lt;/code&gt;. This top will also have time complexity of &lt;code&gt;O(1)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Explanation with Our Example&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;When we push 3, both stacks change to the following.
Main Stack             Min Stack
3 -&amp;gt; Top               3-&amp;gt;Top

When 5 is pushed, both stacks change to the following.
Main Stack             Min Stack
4 -&amp;gt; Top               3-&amp;gt;Top
3                          3

if we apply the top method then it will return 4 which is the correct solution.

if we apply the getMin method then the top of the Min stack will be returned which is 3.

if we apply the pop method then we will pop 4 from the main stack and two update min stack we will also pop 3 so new stacks will be
Main Stack             Min Stack
3 -&amp;gt; Top               3-&amp;gt;Top
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Brute Force Solution Complexity Analysis&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Time Complexity- we have &lt;code&gt;O(1)&lt;/code&gt; time complexity for all push, pop, top and getMin methods.&lt;/li&gt;
&lt;li&gt;Space Complexity - we are using the extra auxiliary stack for storing minimum value. Our space complexity is &lt;code&gt;O(n)&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;In this approach, we will be solving the problem in &lt;code&gt;0(1)&lt;/code&gt; space complexity. Instead of storing min values in the stack, we will try to use a single value variable Min_value which will store the current minimum value in the stack. &lt;br&gt;
In this approach critical part will be when we pop a value and that value is itself current &lt;code&gt;Min_value&lt;/code&gt; then How to get the next &lt;code&gt;Min_value&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How we are going handle this ?&lt;/strong&gt;&lt;br&gt;
To handle this, we push &lt;code&gt;2*x – Min_value&lt;/code&gt; into the stack instead of &lt;code&gt;x&lt;/code&gt; so that the previous minimum element can be retrieved using the current &lt;code&gt;Min_value&lt;/code&gt; and its value stored in the stack.&lt;br&gt;
So Our operations push, pop,getMin, and top are implemented as follows.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Push(x)&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If stack is empty, insert &lt;code&gt;x&lt;/code&gt; into the stack and make Min_value equal to &lt;code&gt;x&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;if &lt;code&gt;x&lt;/code&gt; is less than Min_value then insert &lt;code&gt;(2*x- Min_value)&lt;/code&gt; into stack and update &lt;code&gt;Min_value&lt;/code&gt; equal to &lt;code&gt;x&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;if &lt;code&gt;x&amp;gt;=Min_value&lt;/code&gt; then insert &lt;code&gt;x&lt;/code&gt; into stack.
Example: let's assume we have minimum value in stack till now as 3 and now want to insert 1 then we will insert &lt;code&gt;(2*1 -3)&lt;/code&gt; in the stack and make &lt;code&gt;Min_value = 1&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Pop ()&lt;/strong&gt; &lt;br&gt;
pop the top element and Let the popped element be y then our critical case might occur. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;if &lt;code&gt;y&amp;gt;=Min_value&lt;/code&gt; then our Min_value will remain Min_Value. Because the popped element is not less than Min_Value.&lt;/li&gt;
&lt;li&gt;But if &lt;code&gt;y&amp;lt; Min_value&lt;/code&gt; then it is the case when we are removing the current minimum element of the stack. So we need to find out the next Min_value. and &lt;code&gt;Min_value = 2*y - Min_value&lt;/code&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Can anyone guess why we used &lt;code&gt;Min_value  =2*y -Min_value&lt;/code&gt;? Write in Comment Section (Writer’s 1st Question to you)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Top()&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;if stack is empty return -1. &lt;/li&gt;
&lt;li&gt;Let &lt;code&gt;z = stack.top()&lt;/code&gt;, If &lt;code&gt;z&amp;gt;= Min_value&lt;/code&gt; then return z. It will be original top.&lt;/li&gt;
&lt;li&gt;If &lt;code&gt;z&amp;lt;Min_value&lt;/code&gt; then return Min_value . &lt;code&gt;Anyone Want to guess ? Let me know in comment Section&lt;/code&gt;(Writer’s 2nd Question).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;getMin&lt;/strong&gt;- if the stack is empty then return -1 else It is simply the Min_value.&lt;/p&gt;
&lt;h2&gt;
  
  
  Complete Code
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;stack&amp;lt;int&amp;gt; s;
int mini;
MinStack::MinStack() {
    s = stack&amp;lt;int&amp;gt;();
}

void MinStack::push(int x) {
    if(s.empty()){
        mini = x;
    }
    if(x&amp;gt;=mini){
        s.push(x);
    }
    else{
        s.push(2*x-mini);
        mini = x;
    }
}

void MinStack::pop() {
    if(s.empty()){
        return;
    }
    if(s.top()&amp;lt;mini){
        mini = 2*mini - s.top();
    }
    s.pop();
}

int MinStack::top() {
    if(s.empty()) {
        return -1;
    }


    if(s.top() &amp;gt;=mini){
        return s.top();
    }
    else{
        return mini;
    }
}

int MinStack::getMin() {
    if(s.empty()){
        return -1;
    }
    return mini;
}

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

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;Answer to Questions that I asked and  Why our logic works.&lt;/strong&gt;&lt;br&gt;
When the element to be inserted is less than Min_value, we inserted &lt;code&gt;2x – Min_value&lt;/code&gt;. The important thing to note here is that &lt;code&gt;2x – Min_Value&lt;/code&gt; will always be less than x(which will become the next Min_value) Because&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;How 2*x - Min_value is less than x in push()? 
x &amp;lt; Min_value which means x - Min_value &amp;lt; 0

// Adding x on both sides
x - Min_value + x &amp;lt; 0 + x 

2*x - Min_value &amp;lt; x 

So we can say that 2*x - Min_value &amp;lt; new Min_value 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So when we pop out elements and we get a top value less than Min_value then It is clear that We are popping out Min_value. So we need to update our Min_value. How it is calculated is explained below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;How previous minimum element, prev_Min_Value is 2*minEle - y
in pop() when y the popped element?
because when we pushed y =(2*x-prev_Min_Value) here prev_Min_Value is the minimum element before we inserted y. and here x is an original value that we received to insert.
and after insert, we set Min_value = x.
So 
our equation will be y = 2*Min_Value - prev_Min_Value 
which leads us to get prev_Min_Value as 2*Min_value - y (y is the value we just popped out)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hope you find it Relevant. If you face any issues while understanding and have any feedback then the comment box is all yours. Cheers&lt;/p&gt;

</description>
      <category>dsa</category>
      <category>stack</category>
      <category>interviewbit</category>
      <category>100daysofcode</category>
    </item>
    <item>
      <title>Diffk Solution - Interviewbit</title>
      <dc:creator>Vikram Singh Gurjar</dc:creator>
      <pubDate>Sat, 16 Apr 2022 12:28:59 +0000</pubDate>
      <link>https://dev.to/vpsinghg/diffk-interviewbit-21jj</link>
      <guid>https://dev.to/vpsinghg/diffk-interviewbit-21jj</guid>
      <description>&lt;p&gt;Hi Devs, Happy Coding &lt;br&gt;
I am planning to revise Data Structure and Algorithms Concepts so I will be sharing my approach as well as a full solution for problems I solve. This is my first attempt at writing a dev blog, yeah I am a bit nervous. Hope you like it and Your valuable feedback and contributions are always welcome.&lt;/p&gt;

&lt;p&gt;Today I am going to solve a coding problem &lt;a href="https://www.interviewbit.com/problems/diffk/"&gt;Diffk&lt;/a&gt; which is based on the Two Pointer concept.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Asked In - Facebook Interview&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Problem Statement - Given an array ‘A’ of sorted integers and another non negative integer k, find if there exists 2 indices i and j such that A[i] - A[j] = k, i != j.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Time and Space Complexity Constraint&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
    1. Try doing this in less than linear space complexity.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&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;Input :
  A : [1 3 5] 
  k : 4
Output : YES
Explanation : as 5 - 1 = 4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Brute Force Approach -&lt;/strong&gt; we use two loop for two indices i,j  and iterate if we find any A[j]-A[i] = k and i!=j then return 1 else 0.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int diffPossible(vector&amp;lt;int&amp;gt; &amp;amp;A, int k) {
  int N= A.size();
  for (int i = 0; i &amp;lt; N; i++) {
    for (int j = i + 1; j &amp;lt; N; j++) {
      if (A[j] - A[i] &amp;gt; k) break; // No need check forward because Array is sorted so the difference is going to increase even further because A[I] is going to increase or remain same.
      if (A[j] - A[i] == k) return 1; 
    }
  }
  return 0;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Time Complexity of this approach is O(N*N). &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Observation&lt;/strong&gt;&lt;br&gt;
Let's check the above approach when we have &lt;code&gt;i = I&lt;/code&gt; and find &lt;code&gt;j=J&lt;/code&gt; ,&lt;code&gt;Diff_1 =(A[J-1]-A[I])&lt;/code&gt; such that &lt;code&gt;Diff_1 &amp;lt;k&lt;/code&gt; and &lt;code&gt;A[J]-A[I] &amp;gt;k&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;This means that for &lt;code&gt;i =I&lt;/code&gt; and &lt;code&gt;j&amp;gt; J&lt;/code&gt; we are not going to find our solution because &lt;code&gt;A[j]&lt;/code&gt; will be increasing(Array is sorted) so will &lt;code&gt;A[j]-A[I]&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;when &lt;code&gt;i&lt;/code&gt; increase then &lt;code&gt;A[i]&lt;/code&gt; will increase too then &lt;code&gt;A[J]- A[i]&lt;/code&gt; will decrease so till &lt;code&gt;j =J-1&lt;/code&gt; our &lt;code&gt;A[j] -A[i]&lt;/code&gt; will be less than &lt;code&gt;Diff_1&lt;/code&gt; which is also less than &lt;code&gt;k&lt;/code&gt;( Observation 1 ) So we will not find our solution till &lt;code&gt;j =J-1&lt;/code&gt;. So Efficient Approach will be when we don’t start &lt;code&gt;j&lt;/code&gt; every time with &lt;code&gt;I+1&lt;/code&gt; but start with j=J.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;New Approach Code (Time Complexity:  O(n))&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;int diffPossible(vector&amp;lt;int&amp;gt; &amp;amp;A, int k) {
    int N = A.size();
    int j = 0; 
    for (int i = 0; i &amp;lt; N; i++) {
        j = max(j, i+1);
        while (j &amp;lt; N &amp;amp;&amp;amp; (A[j] - A[i] &amp;lt; B)) {
            j += 1;
        }
        if (A[j] - A[i] == B) return 1;
    }
    return 0;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://github.com/vpsinghg/DSA/blob/master/InterviewBit/Two-Pointer/Diffk.cpp"&gt;Github Solution Link&lt;/a&gt;&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>dsa</category>
      <category>twopointer</category>
      <category>interviewbit</category>
    </item>
  </channel>
</rss>
