Minimum indexes

3.3

86 votes
Data Structures, Basics of Stacks, Stacks
Problem

You are given an array A of Q integers and Q queries. In each query, you are given an integer i (1iN).

Your task is to find the minimum index greater than i (1iN) such that:

  1. Sum of digits of Ai is greater than the sum of digits of Aj
  2. Ai < Aj

If there is no answer, then print -1.

Input format

  • The first line contains two numbers N and Q.
  • The next line contains N numbers.
  • Next Q lines contain Q queries.

Output format

Print the answer as described in the problem

Constraints

(1N, Q105)(1Ai109)(1QiN)    

Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

In the first quary 70 grater than 62 and sum of digits of 62 = 8 grater than sum f digits of 70=7 (8>7)

Editor Image

?