Check whether an array can be made strictly decreasing by modifying at most one element

Given an array arr[] of positive integers, the task is to find whether it is possible to make this array strictly decreasing by modifying at most one element.
Examples:Â
Input: arr[] = {12, 9, 10, 5, 2}Â
Output: YesÂ
{12, 11, 10, 5, 2} is one of the valid solutions.
Input: arr[] = {1, 2, 3, 4}Â
Output: NoÂ
Approach: For every element arr[i], if it is greater than both arr[i – 1] and arr[i + 1] or it is smaller than both arr[i – 1] and arr[i + 1] then arr[i] needs to be modified.Â
i.e arr[i] = (arr[i – 1] + arr[i + 1]) / 2. If after modification, arr[i] = arr[i – 1] or arr[i + 1] then the array cannot be made strictly decreasing without affecting at most one element else count all such modifications, if the count of modifications in the end is less than or equal to 1 then print Yes else print No.
Below is the implementation of the above approach:Â
C++
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;Â
// Function that returns true if the array// can be made strictly decreasing// with at most one changebool check(vector<int> arr, int n){Â
    // To store the number of modifications    // required to make the array    // strictly decreasing    int modify = 0;Â
    // Check whether the last element needs    // to be modify or not    if (arr[n - 1] >= arr[n - 2]) {        arr[n - 1] = arr[n - 2] - 1;        modify++;    }Â
    // Check whether the first element needs    // to be modify or not    if (arr[0] <= arr[1]) {        arr[0] = arr[1] + 1;        modify++;    }Â
    // Loop from 2nd element to the 2nd last element    for (int i = n - 2; i > 0; i--) {Â
        // Check whether arr[i] needs to be modified        if ((arr[i - 1] <= arr[i] && arr[i + 1] <= arr[i])            || (arr[i - 1] >= arr[i] && arr[i + 1] >= arr[i])) {Â
            // Modifying arr[i]            arr[i] = (arr[i - 1] + arr[i + 1]) / 2;            modify++;Â
            // Check if arr[i] is equal to any of            // arr[i-1] or arr[i+1]            if (arr[i] == arr[i - 1] || arr[i] == arr[i + 1])                return false;        }    }Â
    // If more than 1 modification is required    if (modify > 1)        return false;Â
    return true;}Â
// Driver codeint main(){Â
    vector<int> arr = { 10, 5, 11, 2 };    int n = arr.size();Â
    if (check(arr, n))        cout << "Yes";    else        cout << "No";Â
    return 0;} |
Java
// Java implementation of the approachclass GFG {Â
    // Function that returns true if the array    // can be made strictly decreasing    // with at most one change    public static boolean check(int[] arr, int n)    {Â
        // To store the number of modifications        // required to make the array        // strictly decreasing        int modify = 0;Â
        // Check whether the last element needs        // to be modify or not        if (arr[n - 1] >= arr[n - 2]) {            arr[n - 1] = arr[n - 2] - 1;            modify++;        }Â
        // Check whether the first element needs        // to be modify or not        if (arr[0] <= arr[1]) {            arr[0] = arr[1] + 1;            modify++;        }Â
        // Loop from 2nd element to the 2nd last element        for (int i = n - 2; i > 0; i--) {Â
            // Check whether arr[i] needs to be modified            if ((arr[i - 1] <= arr[i] && arr[i + 1] <= arr[i])                || (arr[i - 1] >= arr[i] && arr[i + 1] >= arr[i])) {Â
                // Modifying arr[i]                arr[i] = (arr[i - 1] + arr[i + 1]) / 2;                modify++;Â
                // Check if arr[i] is equal to any of                // arr[i-1] or arr[i+1]                if (arr[i] == arr[i - 1] || arr[i] == arr[i + 1])                    return false;            }        }Â
        // If more than 1 modification is required        if (modify > 1)            return false;Â
        return true;    }Â
    // Driver code    public static void main(String[] args)    {        int[] arr = { 10, 5, 11, 3 };        int n = arr.length;Â
        if (check(arr, n))            System.out.print("Yes");        else            System.out.print("No");    }} |
Python3
# Python3 implementation of the approachÂ
# Function that returns true if the array # can be made strictly decreasing # with at most one changedef check(arr, n):Â
    modify = 0         # Check whether the last element needs    # to be modify or not    if (arr[n - 1] >= arr[n - 2]):        arr[n-1] = arr[n-2] - 1        modify += 1         # Check whether the first element needs    # to be modify or not    if (arr[0] <= arr[1]):        arr[0] = arr[1] + 1        modify += 1Â
    # Loop from 2nd element to the 2nd last element    for i in range(n-2, 0, -1):Â
        # Check whether arr[i] needs to be modified        if (arr[i - 1] <= arr[i] and arr[i + 1] <= arr[i]) or \        (arr[i - 1] >= arr[i] and arr[i + 1] >= arr[i]):Â
            # Modifying arr[i]            arr[i] = (arr[i - 1] + arr[i + 1]) // 2            modify += 1                         # Check if arr[i] is equal to any of            # arr[i-1] or arr[i + 1]            if (arr[i] == arr[i - 1] or arr[i] == arr[i + 1]):                return FalseÂ
Â
    # If more than 1 modification is required    if (modify > 1):        return FalseÂ
    return TrueÂ
# Driver codeif __name__ == "__main__":Â Â Â Â arr = [10, 5, 11, 3]Â Â Â Â n = len(arr)Â
    if (check(arr, n)):        print("Yes")    else:        print("No") |
C#
// C# implementation of the approachusing System;Â
class GFG{         // Function that returns true if the array    // can be made strictly decreasing    // with at most one change    public static bool check(int[]arr, int n)    {Â
        // To store the number of modifications        // required to make the array        // strictly decreasing        int modify = 0;Â
        // Check whether the last element needs        // to be modify or not        if (arr[n - 1] >= arr[n - 2])        {            arr[n - 1] = arr[n - 2] - 1;            modify++;        }Â
        // Check whether the first element needs        // to be modify or not        if (arr[0] <= arr[1])        {            arr[0] = arr[1] + 1;            modify++;        }Â
        // Loop from 2nd element to the 2nd last element        for (int i = n - 2; i > 0; i--)         {Â
            // Check whether arr[i] needs to be modified            if ((arr[i - 1] <= arr[i] && arr[i + 1] <= arr[i])                || (arr[i - 1] >= arr[i] && arr[i + 1] >= arr[i]))             {Â
                // Modifying arr[i]                arr[i] = (arr[i - 1] + arr[i + 1]) / 2;                modify++;Â
                // Check if arr[i] is equal to any of                // arr[i-1] or arr[i+1]                if (arr[i] == arr[i - 1] || arr[i] == arr[i + 1])                    return false;            }        }Â
        // If more than 1 modification is required        if (modify > 1)            return false;Â
        return true;    }Â
    // Driver code    static public void Main ()    {        int[]arr = { 10, 5, 11, 3 };        int n = arr.Length;Â
        if (check(arr, n))            Console.Write("Yes");        else            Console.Write("No");    }}Â
// This code is contributed by ajit. |
Javascript
<script>    // Javascript implementation of the approach         // Function that returns true if the array    // can be made strictly decreasing    // with at most one change    function check(arr, n)    {          // To store the number of modifications        // required to make the array        // strictly decreasing        let modify = 0;          // Check whether the last element needs        // to be modify or not        if (arr[n - 1] >= arr[n - 2])        {            arr[n - 1] = arr[n - 2] - 1;            modify++;        }          // Check whether the first element needs        // to be modify or not        if (arr[0] <= arr[1])        {            arr[0] = arr[1] + 1;            modify++;        }          // Loop from 2nd element to the 2nd last element        for (let i = n - 2; i > 0; i--)        {              // Check whether arr[i] needs to be modified            if ((arr[i - 1] <= arr[i] && arr[i + 1] <= arr[i])                || (arr[i - 1] >= arr[i] && arr[i + 1] >= arr[i]))            {                  // Modifying arr[i]                arr[i] = parseInt((arr[i - 1] + arr[i + 1]) / 2, 10);                modify++;                  // Check if arr[i] is equal to any of                // arr[i-1] or arr[i+1]                if (arr[i] == arr[i - 1] || arr[i] == arr[i + 1])                    return false;            }        }          // If more than 1 modification is required        if (modify > 1)            return false;          return true;    }         let arr = [ 10, 5, 11, 3 ];    let n = arr.length;Â
    if (check(arr, n))      document.write("Yes");    else      document.write("No");Â
</script> |
Yes
Â
Time Complexity: O(N)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



