var arr = '';
process.stdin.on('data',function(chunk){
arr += chunk;
});
process.stdin.on('end',function(){
arr = arr.split('\n');
let t = parseInt(arr[0]),i=1,n,radii,line=1,len,j,k,stack;
while(i<=t){
n = parseInt(arr[line++]);
radii = arr[line++].split(' ');
len = radii.length;
stack = [];
for(j=0;j<len;j++){
if(stack.length === 0) stack.push(parseInt(radii[j]));
else{
if(parseInt(radii[j]) > stack[stack.length-1]) stack.push(parseInt(radii[j]));
else{
for(k=0;k<stack.length;k++){
if(stack[k]>parseInt(radii[j])){
stack[k] = parseInt(radii[j]);
break;
}
}
}
}
}
console.log(stack.length+" "+stack.join(' '));
i++;
}
});
I wrote the program of “Stack” in node.js but the output is coming “time limit exceeded”. The same program when I am writing in C then it is getting executed successfully. Where the time complexity in both the case is O(n3). Anyone have any idea to do it in javascript?