Open In App

Check if Array can be sorted by changing elements to Absolute difference with j

Last Updated : 01 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given an array of positive numbers arr[] of size n, the task is to check if we can make the array sorted in non-decreasing order by choosing any positive integer j and changing all elements of the array 
a[i] to abs(a[i]-j).

Examples:

Input: arr = { 3, 3, 1 }
Output: Yes 
Explanation: We can make array sorted in non-decreasing order by choosing an integer 2.

Input: arr = { 5, 3, 4, 5 }
Output: No
Explanation: It is impossible to make array sorted in non-decreasing order by choosing any integer value.

Approach: To solve the problem follow the below idea:

The idea is to take two integers l = 0 and r = 109 such that if we choose integer j such that l ≤ j ≤ r for every j, the array will be sorted till index current index i by performing this operation and we will assign l and r by taking overlap of previous and current range. After iterating the whole array, if l ≤ r, then it is possible to make array sorted in non-decreasing order and print “Yes”, else print “No”.

Below are the steps for the above approach:

  • Initialize the range from l to r, assign l to 0, and r = 109.
  • Iterate the array and check,
    • If x is less than y, we update the right endpoint r of the range as the minimum of the previous value of r and (x + y) / 2.
    • If x is greater than y, we update the left endpoint l of the range as the maximum of the previous value of l and (x + y + 1) / 2.
  • After iterating the whole array, check if l <= r, print “Yes” else, print “No”.

Below is the implementation for the above approach:

C++




// C++ code of the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find if we can make array
// sorted in non-decreasing order by
// choosing any positive integer
// and perform operation
bool makesorted(int* arr, int n)
{
 
    // Assign l and r initially because
    // we can choose any positive integer
    int l = 0, r = 1e9;
 
    // Iterate the array
    for (int i = 0; i + 1 < n; i++) {
 
        // Current element
        int x = arr[i];
 
        // Next element
        int y = arr[i + 1];
 
        int l1 = (x + y) / 2;
        int r1 = (x + y + 1) / 2;
 
        // Taking intersection of range
        // l to r and range l1 to r1
        if (x < y) {
            r = min(r, l1);
        }
        if (x > y) {
            l = max(l, r1);
        }
    }
 
    // After iterating whole array,
    // if l <= r
    if (l <= r) {
 
        // Return true
        return true;
    }
 
    // Else return false
    return false;
}
 
// Drive Code
int main()
{
    int arr[] = { 3, 3, 1 };
    int n = sizeof(arr) / sizeof(int);
 
    // Function call
    if (makesorted(arr, n)) {
        cout << "Yes" << endl;
    }
    else {
        cout << "No" << endl;
    }
 
    return 0;
}


Java




// Jaava code for the above approach:
 
import java.io.*;
 
class GFG {
 
    // Function to find if we can make array sorted in
    // non-decreasing order by choosing any positive integer
    // and perform operation
    static boolean makesorted(int[] arr, int n)
    {
        // Assign l and r initially because we can choose
        // any positive integer
        int l = 0, r = (int)1e9;
 
        // iterate the array
        for (int i = 0; i + 1 < n; i++) {
            // Current element
            int x = arr[i];
 
            // Next element
            int y = arr[i + 1];
 
            int l1 = (x + y) / 2;
            int r1 = (x + y + 1) / 2;
 
            // Taking intersection of range l to r and range
            // l1 to r1
            if (x < y) {
                r = Math.min(r, l1);
            }
            if (x > y) {
                l = Math.min(l, r1);
            }
        }
 
        // After iterating whole array, if l<=r
        if (l <= r) {
            // return true
            return true;
        }
        // else, return false
        return false;
    }
 
    public static void main(String[] args)
    {
        int[] arr = { 3, 3, 1 };
        int n = arr.length;
 
        // Function call
        if (makesorted(arr, n)) {
            System.out.println("Yes");
        }
        else {
            System.out.println("No");
        }
    }
}
 
// This code is contributed by karthik.


Python3




def makesorted(arr, n):
    # Assign l and r initially because
    # we can choose any positive integer
    l, r = 0, int(1e9)
 
    # Iterate the array
    for i in range(n - 1):
        # Current element
        x = arr[i]
 
        # Next element
        y = arr[i + 1]
 
        l1 = (x + y) // 2
        r1 = (x + y + 1) // 2
 
        # Taking intersection of range
        # l to r and range l1 to r1
        if x < y:
            r = min(r, l1)
        if x > y:
            l = max(l, r1)
 
    # After iterating whole array,
    # if l <= r
    if l <= r:
        # Return true
        return True
 
    # Else return false
    return False
 
# Drive code
arr = [3, 3, 1]
n = len(arr)
 
# Function call
if makesorted(arr, n):
    print("Yes")
else:
    print("No")
# This code is contributed By Shivam Tiwari


C#




using System;
 
public class Program
{
    public static bool MakeSorted(int[] arr, int n)
    {
        // Assign l and r initially because
        // we can choose any positive integer
        int l = 0;
        int r = (int)1e9;
 
        // Iterate the array
        for (int i = 0; i < n - 1; i++)
        {
            // Current element
            int x = arr[i];
 
            // Next element
            int y = arr[i + 1];
 
            int l1 = (x + y) / 2;
            int r1 = (x + y + 1) / 2;
 
            // Taking intersection of range
            // l to r and range l1 to r1
            if (x < y)
            {
                r = Math.Min(r, l1);
            }
            if (x > y)
            {
                l = Math.Max(l, r1);
            }
        }
 
        // After iterating the whole array,
        // if l <= r
        if (l <= r)
        {
            // Return true
            return true;
        }
 
        // Else return false
        return false;
    }
 
    public static void Main(string[] args)
    {
        int[] arr = { 3, 3, 1 };
        int n = arr.Length;
 
        // Function call
        if (MakeSorted(arr, n))
        {
            Console.WriteLine("Yes");
        }
        else
        {
            Console.WriteLine("No");
        }
    }
}
// This code is contributed by codearcade


Javascript




// JavaScript code of the above approach
function makesorted(arr) {
// Assign l and r initially because
// we can choose any positive integer
let l = 0, r = 1e9;
 
// Iterate the array
for (let i = 0; i + 1 < arr.length; i++) {
 
    // Current element
    let x = arr[i];
 
    // Next element
    let y = arr[i + 1];
 
    let l1 = Math.floor((x + y) / 2);
    let r1 = Math.floor((x + y + 1) / 2);
 
    // Taking intersection of range
    // l to r and range l1 to r1
    if (x < y) {
        r = Math.min(r, l1);
    }
    if (x > y) {
        l = Math.max(l, r1);
    }
}
 
// After iterating whole array,
// if l <= r
if (l <= r) {
 
    // Return true
    return true;
}
 
// Else return false
return false;
}
 
// Drive Code
let arr = [3, 3, 1];
 
// Function call
if (makesorted(arr)) {
console.log("Yes");
}
else {
console.log("No");
}


Output

Yes

Time Complexity: O(N)
Auxiliary Space: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads