-1

I have been doing a question recently and I used a similar approach to the editorial. Link to the question is below : https://www.spoj.com/problems/STPAR/

Editorial Code (Though it is from a third party site, I have tested it and it gives AC)-

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
using namespace std;

int a[1111], n;

int main() {
  while (scanf("%d", &n) && n != 0) {
    for (int i = 0; i < n; i++) scanf("%d", &a[i]);
    int k = 0, i = 0;
    stack<int> st;
    while (i < n) {
      while (st.size() && st.top() == k + 1) k++, st.pop();
      if (a[i] != k + 1) st.push(a[i]);
      else k++;
      i++;
    }
    while (st.size() && st.top() == k + 1) k++, st.pop();
    puts(k == n ? "yes" : "no");
  }
}

My code -

#include<bits/stdc++.h>

using namespace std;

int main()
{
    int t;
    while(t)
    {
        int n;
        cin>>n;

        if(n==0)
        {
            break;
        }

        int arr[n];
        for(int i=0; i<n; i++)
        {
            cin>>arr[i];
        }

        int p=1;
        stack<int> s;
        int flag=0;
        for(int i=0; i<n; i++)
        {
            while(!s.empty() && s.top()==p)
            {
                s.pop();
                p++;
            }
            if(p==arr[i])
            {
                p++;
            }else if(!s.empty() && s.top()<arr[i])
            {
                flag=1;
                break;
            }else{
                s.push(arr[i]);
            }

        }

        if(flag==0)
        {
            cout<<"yes"<<endl;
        }else{
        cout<<"no"<<endl;}
    }
}

I just cannot understand the difference between the two codes as they use the exactly similar approach and the code is almost similar as well. Help would be appreciated :)

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
  • I haven't really added the problem statement in the question because it won't be easy to explain here... The problem statement explains it better.. – Samridhi Sharma Aug 17 '20 at 05:32
  • 4
    `int t; while(t)` Here `t` is uninitialized – Support Ukraine Aug 17 '20 at 05:33
  • @4386427 The problem statement clearly states `Input ends with number 0.` So the input continues until we get 0 as input. The t is actually to run the loop infinitely.. – Samridhi Sharma Aug 17 '20 at 05:40
  • 1
    The problem statement belongs here. When that link goes bad this question becomes useless. – Retired Ninja Aug 17 '20 at 05:41
  • 1
    [Why should I not `#include `?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) [Why is `using namespace std;` considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Evg Aug 17 '20 at 06:18
  • @SamridhiSharma The problem statement is irrelevant here. You are using `t` uninitialized. It may be zero, it may be 1 and so on. You can't know. So maybe the `while` end immediately. – Support Ukraine Aug 17 '20 at 07:21

1 Answers1

0

You haven't initialized the t value. And also you're not decreasing it.

As per question there is no input of no of test cases. So remove variable t and while loop as well(i mean remove while loop not the code inside it). Run it again.

Mahesh Sv
  • 41
  • 4