STRICTLY INCREASING SEQUENCE - HackerEarth Solution
STRICTLY INCREASING SEQUENCE - HackerEarth Solution
Problem Name - STRICTLY INCREASING SEQUENCE
Problem Statement
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.
STRICTLY INCREASING SEQUENCE - HackerEarth Solution
Code -
#include <bits/stdc++.h>using namespace std;#define ll long longint main(){ll t;cin>>t;assert(t<=10&&t>=1);while(t--){ll n,i,ans=1,x;cin>>n;assert(n<=100000&&n>=1);for(i=0;i<n;++i){cin>>x;if(x<i)ans=0;assert(x<=100000&&x>=0);}if(ans)cout<<"Yes\n";else cout<<"No\n";}return 0;}
Post a Comment