Pink Floyd and Happiness

Asked by At

Pink is sad because of some reasons, he wants to cheer up by listening to some songs from his favourite band, Pink Floyd.

There are N records and Pink will be happy if he listens to them in the ascending order, i.e, first the song No. 1, then No.2 and so on(He has to listen to all the N songs to become Happy).

Pink is delivered his records in some given order, he can either add the record to the Playlist in the delivered order or put some on an another table. After being put on the table only the topmost record can be added to the playlist at any time.

Print whether Pink will be sad or happy after the delivery of the records.

    process.stdin.resume();
    process.stdin.setEncoding("utf-8");
    var stdin_input =

 "";

process.stdin.on("data", function (input) {
    stdin_input += input;                               // Reading input from STDIN
});

process.stdin.on("end", function () {
   main(stdin_input);
});

class Stack {`enter code here`
  constructor(){
    this.array = [];
  }
  peek() {
    return this.array[this.array.length-1];
  }
  push(value){
    this.array.push(value);
    return this;
  }
  pop(){
    this.array.pop();
    return this;
  }

  length(){
      return this.array.length;
  }

}



function main(input) {
        const myStack = new Stack();
        let a,b =[];
        let l,j=1,index=0 ,x=0,y=0,v=0;
        input  =  input.split(/\n/g);
        a = input[1].split(' ');
        l = a.length;

        while(j<l){

            x=Number(a[index]);

            z=myStack.peek();

            if(x == j){
                b.push(j);
                j++;
                index++;
            }
            else if(myStack.length !== 0  &&  z == j)
                {
                //console.log(j);
                    y=myStack.peek();

                    b.push(y);
                    myStack.pop();
                    j++;
                }
            else
                {
                    if(myStack.length === 0 )
                    {
                        myStack.push(x);
                        index++;
                    }
                    else if(z-x !==0)
                    {
                        v=1;
                        break;
                    }
                }

            }

       if(v == 1){
        console.log("Sad");
       }

        else {
            console.log("Happy");
        }

}






process.stdin.resume();
process.stdin.setEncoding("utf-8");
var stdin_input = "";

process.stdin.on("data", function (input) {
    stdin_input += input;                               // Reading input from STDIN
});

process.stdin.on("end", function () {
   main(stdin_input);
});

class Stack {
  constructor(){
    this.array = [];
  }
  peek() {
    return this.array[this.array.length-1];
  }
  push(value){
    this.array.push(value);
    return this;
  }
  pop(){
    this.array.pop();
    return this;
  }

  length(){
      return this.array.length;
  }

}



function main(input) {
        const myStack = new Stack();
        let a,b =[];
        let l,j=1,index=0 ,x=0,y=0,v=0;
        input  =  input.split(/\n/g);
        a = input[1].split(' ');
        l = a.length;

        while(j<l){

            x=Number(a[index]);
            //console.log(x);
            z=myStack.peek();
            //console.log(z);
            //console.log(myStack);
            if(x == j){
                b.push(j);
                j++;
                index++;
            }
            else if(myStack.length !== 0  &&  z == j)
                {
                //console.log(j);
                    y=myStack.peek();
                   // console.log(y);
                    b.push(y);
                    myStack.pop();
                    j++;
                }
            else
                {
                    if(myStack.length === 0 )
                    {
                        myStack.push(x);
                        //console.log(myStack);
                        //console.log(b);
                        index++;
                    }
                    else if(z-x !==0)
                    {
                        v=1;
                        break;
                        //console.log(index);
                    }
                }

            }

            //while((mystack.length)!=0 and song_to_play==stack[-1]){
                    //stack.pop(-1)
                    //song_to_play+=1


       //var c = a.map(function(item) {
      // return parseInt(item, 10);
       //});
       //var f = c.sort();

       if(v == 1){
        console.log("Sad");
        //console.log(index);
       }

        else {
            //console.log(b);
            console.log("Happy");
        }

}

0 Answers