Strictly increasing sequence

2.5

8 votes
Algorithms, Basics of Greedy Algorithms, Greedy Algorithms
Problem

Given an array consisting of N non-negative integers. You may perform the given operation on an array element but the operation can be performed on each element no more than once.

For an element A[i] of the array , 1<=i<=N :

  • Select a non-negative X number such that, 0<=X<=A[i] and then reduce A[i] to A[i]X.

Check if the entire sequence can be converted into a strictly increasing sequence. Print Yes if the sequence can be converted into a strictly increasing sequence, else print No.

Example

Consider N = 4, and A[] = [1, 15, 10, 15].

  • Initially, the array is not in Strictly Increasing Order.

  • Let's select the array element, A[1] (=15) and X = 10. If we perform A[1] = A[1] - X, the new array is A[] = [1, 5, 10, 15].

  • Now the array A is in strictly increasing order.

Therefore the answer is "Yes".

Function description

Complete the solve function provided in the editor. This function takes the following 2 parameters and returns the answer:

  • N: Represents the size of array A.
  • A: Represents the elements of the array.

Input format

Note: This is the input format that you must use to provide custom input (available above the Compile and Test button).

  • First line: An integer T denoting the number of test cases.
  • Next 2T lines:
    • First line: An integer N denoting the number of elements in the array.
    • Second line: N space separated non negative integers denoting the sequence.

Output Format:
For each test case, print 'Yes' if after all the performed operations the sequence becomes STRICTLY INCREASING else print 'No' on a new line.

Constraints:

1<=T<=10

1<=N<=100000

0<=A[i]<=100000

Code snippets (also called starter code/boilerplate code)

This question has code snippets for C, CPP, Java, and Python.

Time Limit: 1
Memory Limit: 512
Source Limit:
Explanation

The first line represents the number of test cases, t = 1.

The first test case

Given array of five elements as : 2 6 4 5 7

If we take X=3 for array element 6 then the sequence turns to be 2 (6-3) 4 5 7 , or

2 3 4 5 7, which is strictly increasing.

Editor Image

?