Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

665. Non-decreasing Array

Medium24.9% Acceptance
Array
Asked by:
B
Bloomberg
ProblemSolutions (12)VideosCompanies (1)Notes

Problem Statement

Given an array nums with n integers, your task is to check if it could become non-decreasing by modifying at most one element.

We define an array is non-decreasing if nums[i] <= nums[i + 1] holds for every i (0-based) such that (0 <= i <= n - 2).

Example 1:

Input: nums = [4,2,3]
Output: true
Explanation: You could modify the first 4 to 1 to get a non-decreasing array.

Example 2:

Input: nums = [4,2,1]
Output: false
Explanation: You cannot get a non-decreasing array by modifying at most one element.

Constraints:

  • n == nums.length
  • 1 <= n <= 104
  • -105 <= nums[i] <= 105
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions

Approach

The key idea in #665 Non-decreasing Array is to determine whether the array can become non-decreasing by modifying at most one element. A non-decreasing array means every element satisfies nums[i] <= nums[i+1]. While scanning the array, we look for violations where this condition fails.

A practical approach is to perform a single pass through the array and count how many times a decreasing pair appears. If more than one violation occurs, it is impossible to fix the array with just one modification. When a violation is found, we conceptually decide whether adjusting the current element or the previous element would maintain the non-decreasing property for the remaining array.

This method relies on a greedy decision using nearby elements to keep the array valid without breaking earlier order. Because the array is scanned only once and modifications are simulated rather than fully applied, the approach runs efficiently in linear time with constant extra space.

Complexity

ApproachTime ComplexitySpace Complexity
Greedy Single Pass CheckO(n)O(1)
Brute Force Modification CheckO(n^2)O(1)

Video Solution Available

NeetCode

View all video solutions

Solutions (12)

Single Pass Approach

This approach makes a single pass through the array while keeping a count of occurrences where nums[i] > nums[i+1]. If more than one such occurrence is found, it returns false. During the traversal, if such an occurrence is found, we will attempt to resolve it by modifying nums[i] or nums[i+1] optimally based on their neighbors.

Time Complexity: O(n), where n is the length of the array.
Space Complexity: O(1), since no additional space is used dependent on input size.

CC++JavaPythonC#JavaScript
1#include <stdbool.h>
2
3bool checkPossibility(int* nums, int numsSize) {
4    int count = 0;
5    for (int

Explanation

The C solution uses a single loop to traverse the array and a counter to track violations of the non-decreasing order. If a violation is found and it's the first one, the program attempts to adjust the values strategically by evaluating neighbors.

Two-Pointer Strategy

This approach involves using two pointers to traverse from the start and end of the array. We simulate potential changes to resolve any issues at both ends, using the pointers to converge inwards and analyze changes required to achieve a non-decreasing state.

Time Complexity: O(n)
Space Complexity: O(1)

CC++JavaPythonC#JavaScript
1#include

Video Solutions

Watch expert explanations and walkthroughs

Non-Decreasing Array - Leetcode 665 - Python

NeetCode
10:5960,253 views

Asked By Companies

1 companies
B
Bloomberg

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Two SumEasy
Median of Two Sorted ArraysHard
Container With Most WaterMedium
3SumMedium
More similar problems

Related Topics

Array

Problem Stats

Acceptance Rate24.9%
DifficultyMedium
Companies1

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Is Non-decreasing Array asked in FAANG interviews?

Yes, this type of array validation and greedy decision-making problem is common in technical interviews. It tests understanding of edge cases, array traversal, and how to make optimal local decisions.

What is the optimal approach for Non-decreasing Array?

The optimal approach uses a greedy single-pass scan of the array. Whenever a decreasing pair is detected, we decide whether modifying the current element or the previous one would maintain the non-decreasing property while ensuring only one modification is allowed.

Why is a greedy approach suitable for Non-decreasing Array?

A greedy strategy works because the decision at each violation only depends on nearby elements. By adjusting the current or previous value conceptually, we can maintain the non-decreasing condition without affecting earlier validated elements.

What data structure is best for solving Non-decreasing Array?

The problem can be solved using just the input array without any additional data structures. A simple iteration with constant variables to track violations is sufficient, making the solution space-efficient.

i
=
0
;
i
<
numsSize
-
1
;
i
++
)
{
6
if
(
nums
[
i
]
>
nums
[
i
+
1
]
)
{
7
count
++
;
8
if
(
count
>
1
)
{
9
return
false
;
10
}
11
if
(
i
>
0
&&
nums
[
i
+
1
]
<
nums
[
i
-
1
]
)
{
12
nums
[
i
+
1
]
=
nums
[
i
]
;
13
}
14
}
15
}
16
return
true
;
17
}
<stdbool.h>
2
3
bool
checkPossibility
(
int
*
nums
,
int
numsSize
)
{
4
int
count
=
0
;
5
int
left
=
0
,
right
=
numsSize
-
1
;
6
while
(
left
<
right
)
{
7
if
(
nums
[
left
]
>
nums
[
left
+
1
]
)
{
8
count
++
;
9
if
(
left
>
0
&&
nums
[
left
+
1
]
<
nums
[
left
-
1
]
)
{
10
nums
[
left
+
1
]
=
nums
[
left
]
;
11
}
12
}
13
if
(
nums
[
right
]
<
nums
[
right
-
1
]
)
{
14
count
++
;
15
if
(
right
<
numsSize
-
1
&&
nums
[
right
-
1
]
>
nums
[
right
+
1
]
)
{
16
nums
[
right
-
1
]
=
nums
[
right
]
;
17
}
18
}
19
if
(
count
>
1
)
return
false
;
20
left
++
;
21
right
--
;
22
}
23
return
true
;
24
}

Explanation

This C solution utilizes a two-pointer method to inspect boundary conditions, adjusting wrong elements while ensuring the count of such modifications doesn't exceed one. Left and right pointers indicate positions to examine simultaneously.