Check if Array can be sorted by changing elements to Absolute difference with j
Last Updated :
01 Aug, 2023
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++
#include <bits/stdc++.h>
using namespace std;
bool makesorted( int * arr, int n)
{
int l = 0, r = 1e9;
for ( int i = 0; i + 1 < n; i++) {
int x = arr[i];
int y = arr[i + 1];
int l1 = (x + y) / 2;
int r1 = (x + y + 1) / 2;
if (x < y) {
r = min(r, l1);
}
if (x > y) {
l = max(l, r1);
}
}
if (l <= r) {
return true ;
}
return false ;
}
int main()
{
int arr[] = { 3, 3, 1 };
int n = sizeof (arr) / sizeof ( int );
if (makesorted(arr, n)) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
return 0;
}
|
Java
import java.io.*;
class GFG {
static boolean makesorted( int [] arr, int n)
{
int l = 0 , r = ( int )1e9;
for ( int i = 0 ; i + 1 < n; i++) {
int x = arr[i];
int y = arr[i + 1 ];
int l1 = (x + y) / 2 ;
int r1 = (x + y + 1 ) / 2 ;
if (x < y) {
r = Math.min(r, l1);
}
if (x > y) {
l = Math.min(l, r1);
}
}
if (l <= r) {
return true ;
}
return false ;
}
public static void main(String[] args)
{
int [] arr = { 3 , 3 , 1 };
int n = arr.length;
if (makesorted(arr, n)) {
System.out.println( "Yes" );
}
else {
System.out.println( "No" );
}
}
}
|
Python3
def makesorted(arr, n):
l, r = 0 , int ( 1e9 )
for i in range (n - 1 ):
x = arr[i]
y = arr[i + 1 ]
l1 = (x + y) / / 2
r1 = (x + y + 1 ) / / 2
if x < y:
r = min (r, l1)
if x > y:
l = max (l, r1)
if l < = r:
return True
return False
arr = [ 3 , 3 , 1 ]
n = len (arr)
if makesorted(arr, n):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
public class Program
{
public static bool MakeSorted( int [] arr, int n)
{
int l = 0;
int r = ( int )1e9;
for ( int i = 0; i < n - 1; i++)
{
int x = arr[i];
int y = arr[i + 1];
int l1 = (x + y) / 2;
int r1 = (x + y + 1) / 2;
if (x < y)
{
r = Math.Min(r, l1);
}
if (x > y)
{
l = Math.Max(l, r1);
}
}
if (l <= r)
{
return true ;
}
return false ;
}
public static void Main( string [] args)
{
int [] arr = { 3, 3, 1 };
int n = arr.Length;
if (MakeSorted(arr, n))
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
}
|
Javascript
function makesorted(arr) {
let l = 0, r = 1e9;
for (let i = 0; i + 1 < arr.length; i++) {
let x = arr[i];
let y = arr[i + 1];
let l1 = Math.floor((x + y) / 2);
let r1 = Math.floor((x + y + 1) / 2);
if (x < y) {
r = Math.min(r, l1);
}
if (x > y) {
l = Math.max(l, r1);
}
}
if (l <= r) {
return true ;
}
return false ;
}
let arr = [3, 3, 1];
if (makesorted(arr)) {
console.log( "Yes" );
}
else {
console.log( "No" );
}
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...