You are given with an array. For each element present in the array your
task is to print the next smallest than that number. If it is not
smallest print -1
Sample Input : 7 10 7 9 3 2 1 15
Sample Output : 7 3 3 2 1 -1 -1
const readline = require('readline');
const inp = readline.createInterface({
input: process.stdin
});
const userInput = [];
inp.on("line", (data) => {
userInput.push(data);
});
inp.on("close", () =>
{var a =Number(userInput[0]);
var inp =String(userInput[1]);
var arr =inp.split(" ").map(val=>Number(val))
var final=""
for(i=0;i<a;i++)
{
for(j=i+1;j<a;j++)
{ if(arr[i]>arr[j])
{final = final+arr[j]
break}
if(!arr[i]>arr[j])
{final = final+"-1"
break}
}
}
console.log(final);
});
Comments
Post a Comment