@@ -0,0 +1,2297 @@
+package com.sunny.grokkingalgorithms.ctci.c3;%0A%0Aimport java.util.Stack;%0A%0A/**%0A * Created by sundas on 3/17/2018.%0A */%0Apublic class SetOfStacks %7B%0A%0A   /*%0A   In real world if stacks get too high, it might topple over. Implement a set of stacks such that when a threshold is%0A   reached for a certain stack it spills over to the next one and so on.Push and pop should behave as if there was%0A   only one stack.%0A    */%0A%0A  private Stack%3CInteger%3E%5B%5D stacks;%0A%0A  private int threshHold;%0A%0A  private int currentStackIndex;%0A%0A  /**%0A   *%0A   * @param threshHold%0A   * @param stackSetSize%0A   */%0A  public SetOfStacks(int threshHold,int stackSetSize)%7B%0A    this.threshHold = threshHold;%0A    stacks = new Stack%5BstackSetSize%5D;%0A  %7D%0A%0A%0A  /**%0A   *%0A   * @param data%0A   */%0A  public void push(int data)%7B%0A    Stack currentStack = stacks%5BcurrentStackIndex%5D;%0A    //If first time initialize first stack%0A    if(currentStack == null)%7B%0A      stacks%5BcurrentStackIndex%5D = new Stack%3C%3E();%0A      stacks%5BcurrentStackIndex%5D.push(data);%0A    %7D%0A    //Capacity reached spill over to next%0A    else if(currentStack.size() %3E= threshHold)%7B%0A      currentStackIndex++;%0A      if(currentStackIndex %3E= stacks.length)%7B%0A        throw new UnsupportedOperationException(%22Maximum capacity reached%22);%0A      %7D%0A      stacks%5BcurrentStackIndex%5D = new Stack%3C%3E();%0A      stacks%5BcurrentStackIndex%5D.push(data);%0A    %7D%0A    else%7B%0A      //Normal case%0A      stacks%5BcurrentStackIndex%5D.push(data);%0A    %7D%0A  %7D%0A%0A%0A  /**%0A   *%0A   * @return%0A   */%0A  public int pop()%7B%0A    /*%0A    Try to see if current stack has any elements%0A     */%0A    if(!stacks%5BcurrentStackIndex%5D.isEmpty())%7B%0A      return stacks%5BcurrentStackIndex%5D.pop();%0A    %7D%0A    /*%0A    Maybe it is in the previous stack%0A     */%0A    else%7B%0A      currentStackIndex--;%0A      if(currentStackIndex %3E= 0)%7B%0A        return stacks%5BcurrentStackIndex%5D.pop();%0A      %7D%0A      else%7B%0A        throw new UnsupportedOperationException(%22No Data Found%22);%0A      %7D%0A    %7D%0A  %7D%0A%0A  /**%0A   *%0A   * @param args%0A   */%0A  public static void main(String%5B%5D args) %7B%0A    int%5B%5D input = new int%5B%5D%7B1,2,3,4,5,6,0,-1,-200,100,-100,10%7D;%0A    SetOfStacks setOfStacks = new SetOfStacks(4,3);%0A    for(int i = 0 ; i %3C input.length ; i++)%7B%0A      setOfStacks.push(input%5Bi%5D);%0A    %7D%0A%0A    for(int i = 0 ; i %3C input.length ; i++)%7B%0A      System.out.println(setOfStacks.pop());%0A    %7D%0A  %7D%0A%0A%7D%0A

Implement set of stacks, spillover logic if threshold is reached.