0

I have implemented code for The Stock Span Problem. The problem is that the variable count is not being permanently changed and it stays zero. It changes only during the recursion. I have also made count static. But the same problem occurs. Below is the code.

public static void test(Stack s,int previous,int count)
{
    if (!s.isEmpty())
    {
        int next = (int)s.pop();
        if (previous > next)
        {
            count++;
                // System.out.print(previous+"  "+next+"===>"+count); // count is being changed here
        }
        System.out.println();
        test( s,previous,count);
        s.push(next);
    }
}
public static void pan(Stack s, int [] array)
{
    int count=0;
    int i=array.length-1;
    while (s.size()>1)
    {
        int previous = (int)s.pop();
        test(s,previous,count);
        array [i] = count+1;
        i--;
        count=0;
    }
    array [i] = 1;
}
public static void main (String [] args)
{
        Stack s = new Stack();
        s.push(10);s.push(20);s.push(5);s.push(5);s.push(10);
        int [] array  = new int [s.size()];
        pan(s,array);
        System.out.println(Arrays.toString(array));
}

0 Answers0