TORONTO KIDS COMPUTER CLUB | PMCA Sunday 10:00 Practice -03.21
19198
post-template-default,single,single-post,postid-19198,single-format-standard,ajax_fade,page_not_loaded,,qode-theme-ver-7.6.2,wpb-js-composer js-comp-ver-6.8.0,vc_responsive

PMCA Sunday 10:00 Practice -03.21

23 Mar PMCA Sunday 10:00 Practice -03.21

Use Stack to solve the following question:

You are given an array A of Q integers and Q queries. In each query, you are given an integer i (1≤i≤N).
Your task is to find the minimum index greater than i (1≤i≤N) such that:
Sum of digits of Ai is greater than the sum of digits of Aj
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
(1≤N, Q≤105)(1≤Ai≤109)(1≤Qi≤N)

Sample Input
5 5
62 70 28 62 92
1
5
3
4
2

Sample Output
2
-1
4
-1
-1

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)
No Comments

Sorry, the comment form is closed at this time.