-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNonDecreasingArray.cpp
49 lines (35 loc) · 1.32 KB
/
NonDecreasingArray.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//////////////////////////////////////CODE STUDIO////////////////////////////////////
// EASY
/*
Logic ---> With ONLY ONE MOVE make the array sorted/ascending order/non-decreasing
Solution ---> Start with i=1 assuming arr[0] is smallest in the array
then traverse the array fromm 1--->n
if arr[i] is less than previous element arr[i-1]
then increment the count
and if count is greater than 1 , then return false i.e.
the given array cannot be converted to non--decreasing by only one move
and if i=1 OR arr[i-2] < arr[i] then make arr[i-1]=arr[i]
otherwise if arr[i-2] > arr[i] then make arr[i]=arr[i-1]
*/
// #include <bits/stdc++.h>
// bool isPossible(int *arr, int n)
// {
// // Write your code here.
// int i;
// int count=0;
// for(i=1;i<n;i++){ //IMPORTANT ---> since we checking for non-dec array ,
// //start with i=1 as first element is assumed to be smallest
// if(arr[i]<arr[i-1]){
// count++;
// if(count>1)
// return false;
// if(i==1 || arr[i-2]<=arr[i])
// arr[i-1]=arr[i];
// else
// arr[i]=arr[i-1];
// }
// }
// return true;
// }
//////////////////////////////////////__665__LEETCODE////////////////////////////////////
// MEDIUM