Amazon interview question

Some array question (leetcode medium difficulty)