Non-Decreasing Array

You have been given an integer array/list 'ARR' of size 'N'. Write a solution to check if it could become non-decreasing by modifying at most 1 element.

We define an array as non-decreasing, if ARR[i] <= ARR[i + 1] holds for every i (0-based) such that (0 <= i <= N - 2).

Input format :
The first line contains an integer 'T' which denotes the number of test cases or queries to be run. Then the test cases follow.

The first line of each test case contains an Integer 'N' denoting the size of the array/list.

The second line of each test case contains 'N' space-separated Integers denoting the array/list.
Output format :
For each test case, print a single line containing "true" if it's possible to make 'ARR' non-decreasing array with modifying at most one element or "false" otherwise. 

The output for every test case will be printed in a separate line.
Note :
You do not need to print anything, it has already been taken care of.  Just implement the given function.
Constraints :
1 <= T <= 50
1 <= N <= 10 ^ 4
- 10 ^ 9 <= ARR[i] <= 10 ^ 9

Where 'N' is the size of the given array/list.
And, ARR[i] denotes the i-th element in the array/list 'ARR'.

Time Limit: 1sec
AnswerBot
1y

The solution checks if an integer array can become non-decreasing by modifying at most one element.

  • Iterate through the array and check if there are more than one decreasing pairs of elements.

  • If there ...read more

CodingNinjas
author
2y
Brute Force

As we are allowed at most one modification, we can try it on every index separately and naively check whether the array becomes non-decreasing by any modification. For this, we can modify t...read more

CodingNinjas
author
2y
Prune the range of Brute Force

The idea is to trim the array ‘ARR’ on both sides and check for the part in the ARRay where the modification is actually required. Suppose if ARR[0] <= ARR[1] <= ARR[2], ...read more

CodingNinjas
author
2y
Finding Indices which are not in non-decreasing order

We need to find all the indices ‘IDX’ which are not in non-decreasing order or we can say all the indices which satisfy ARR[IDX] > ARR[IDX + 1].

Now...read more

IRFAN BEG
5mo
good decision
IRFAN BEG
5mo
thats called time waisting please call me & joined the date
Add answer anonymously...
Josh Technology Group Front end Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter