Home > Enterprise >  numbers which are equal to their index value
numbers which are equal to their index value

Time:05-25

how do i Find the numbers which are equal to their index value and print them in sorted order. Given a number n followed by n numbers

for example: INPUT 6 7 3 3 4 5 OUTPUT 3 4 5

CodePudding user response:

Perfect use case for enumerate

print ([n for i, n in enumerate([6,7,3,3,4,5]) if i == n])

i is your index
n is your list entry

CodePudding user response:

You can just do a listcomp:

   output = [x for x in range(len(input)) if input[x]==x]

CodePudding user response:

Sorry it is in js but the logic same

let arr = "INPUT 67 3 3 4 5 OUTPUT 3 4 5"

let joined = arr.split("")
let box = []
let arrayIndex = joined.forEach((item,idx)=>{
  if(idx == item){
    box.push(item)
  }
})

console.log(box.sort())
  • Related