Algorithm
Problem link- https://www.spoj.com/problems/STPAR/
STPAR - Street Parade
For sure, the love mobiles will roll again on this summer's street parade. Each year, the organisers decide on a fixed order for the decorated trucks. Experience taught them to keep free a side street to be able to bring the trucks into order.
The side street is so narrow that no two cars can pass each other. Thus, the love mobile that enters the side street last must necessarily leave the side street first. Because the trucks and the ravers move up closely, a truck cannot drive back and re-enter the side street or the approach street.
You are given the order in which the love mobiles arrive. Write a program that decides if the love mobiles can be brought into the order that the organisers want them to be.
Input
There are several test cases. The first line of each test case contains a single number n, the number of love mobiles. The second line contains the numbers 1 to n in an arbitrary order. All the numbers are separated by single spaces. These numbers indicate the order in which the trucks arrive in the approach street. No more than 1000 love mobiles participate in the street parade. Input ends with number 0.
Output
For each test case your program has to output a line containing a single word "yes" if the love mobiles can be re-ordered with the help of the side street, and a single word "no" in the opposite case.
Example
Sample input: 5 5 1 2 4 3 0 Sample output: yes
Illustration
The sample input reflects the following situation:
The five trucks can be re-ordered in the following way:
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <bits/stdc++.h>
#include <limits.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
#define MOD (ll)1000000007
#define pb push_back
#define EPS 1e-9
#define FOR(i, n) for(int i = 0;i < n; i++)
#define pi(a) printf("%d\n", a)
#define all(c) c.begin(), c.end()
#define tr(container, it) for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define gc getchar_unlocked
template <typename T> T gcd(T a, T b){return (b==0)?a:gcd(b,a%b);}
template <typename T> T lcm(T a, T b){return a*(b/gcd(a,b));}
template <typename T> T mod_exp(T b, T p, T m){T x = 1;while(p){if(p&1)x=(x*b)%m;b=(b*b)%m;p=p>>1;}return x;}
template <typename T> T invFermat(T a, T p){return mod_exp(a, p-2, p);}
template <typename T> T exp(T b, T p){T x = 1;while(p){if(p&1)x=(x*b);b=(b*b);p=p>>1;}return x;}
void si(int &x){
register int c = gc();
x = 0;
int neg = 0;
for(;((c<48 || c>57) && c != '-');c = gc());
if(c=='-') {neg=1;c=gc();}
for(;c>47 && c<58;c = gc()) {x = (x<<1) + (x<<3) + c - 48;}
if(neg) x=-x;
}
int main(){
int n;
si(n);
while(n != 0){
int arr[n];
for(int i = 0;i < n; i++){
si(arr[i]);
}
stack<int> stk;
int toFound = 1;
int i = 0;
while(i < n){
if(arr[i] == toFound){
// cout<<"HELLO"<<endl;
toFound++;
i++;
}
else if(!stk.empty() && stk.top() == toFound){
// cout<<"karan"<<endl;
stk.pop();
toFound++;
}
else{
// cout<<"batra"<<endl;
stk.push(arr[i]);
i++;
}
}
// cout<<toFound<<endl;
int flag = 0;
while(!stk.empty()){
if(stk.top() != toFound){
cout<<"no"<<endl;
flag = 1;
break;
}
stk.pop();
toFound++;
}
if(!flag)
cout<<"yes"<<endl;
while(!stk.empty()){
stk.pop();
}
si(n);
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
5 1 2 4 3
0
Output
Demonstration
SPOJ Solution-Street Parade-Solution in C, C++, Java, Python