Can You Please tell me the hint how to solve this problem
WITHOUT THE SOLUTION
Or How you think about this solution
From Basic approach to the most optimized approach.
Can You Please tell me the hint how to solve this problem
hello @cbcao263
in this problem we are allowed to modify atmost 1 element to make array non decreasing /sorted.
so we can split this problem in three cases.
a) no modification needed
if given array is already non decreasing then return true otherwise look for case b
b) one modification needed
here we will go to each element of array (say index is i)
and will check whether array from index [0,i-1] and [i+1,…n-1] is sorted and a[i-1] <=a[i+1] if this hold then we return true
if even after complete iteration we dont find any such index i ,then simply we return false.
c) not possible (if above two fails)
Okayy Sir.
Sir is it like pehle waala part sorted hai
fir ek element jisne kaam khrab kiya
nd Then After all array sorted.
And What I did is: -
I took 2 variables inc=0,dec=0
And If I found a[i+1]>a[i] inc++
else ifa[i+1]<a[i] dec++.
Like that.
I think that too will be correct.
Bro One More doubt, What If I Pause the course.
And What If I directly post the doubt here.
Will it get solved or not?
yeah this should work
public:
bool checkPossibility(vector<int>& nums) {
int n=nums.size();
int inc=0,dec=0;
for(int i=0;i<n-1;i++)
{
int d=nums[i+1]-nums[i];
if(d<0)
dec++;
else
inc++;
}
if(dec==1)
return true;
else
return false;
}
};
This is what I used, But it’s giving error on this one.
[3,4,2,3]
it needs two modifications
if i is the false element of the array then comparee a[i-1] with a[i+1] if a[i-1]<=a[i+1] then increment ur dec otherwise return false
Smjha nhi Sir
Aur yeh kisliye
element jiske wajah se gadbad ho rahi hai.
to ensure ki baaki element sorted hai ya nahi.
1 3 1 4 6 ( 1 is the false element , to usko hata ke baaki a[i-1] <=a[i+1] condition follow karna chaiye otherwise single modification mein possible nahi hain)
example->
1 3 1 2 6 (yaha pe 1 false element hai right? but a[i-1]<=a[i+1] nahi hai ( 3 < = 2 nahi hai ) isiliye ye possible nahi hai)
ton mtlb jo loop hain usme ek if else condition lgaadu
if a[i-1]<=a[i+1]
return false;
bas shi hain?
ha agar ye condition satisfy nahi ho rahi to returrn false
kam kar jayega
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.