Operations to Sort an Array in non-decreasing order
Given an array arr[] of integers of size n, the task is to check if we can sort the given array in non-decreasing order(i, e.arr[i] ≤ arr[i+1]) by using two types of operation by performing any numbers of time:
- You can choose any index from 1 to n-1(1-based indexing) and increase arr[i] and arr[i+1] by any positive numbers.
- You can choose any index from 1 to n-1 and decrease arr[i] and arr[i+1] by any positive numbers.
Note: Array elements can be negative at any point.
Examples:
Input: arr[] = {6, 4, 3, 5, 2 }
Output: YES
Explanation:
- Perform type-2 operation on index 1 and choose 4 and Now the array is- {2, 0, 3, 5, 2}
- Perform type-2 operation on index 3 and choose 4, Now the array becomes {2, 0, -1, 1, 2}
- Perform type-1 operation on index 2 and choose 2, Now the array becomes {2, 2, 1, 1, 2}
- Perform type-1 operation on index 3 and choose 1, Now the array becomes {2, 2, 2, 2, 2}
Now array {2, 2, 2, 2, 2}is sorted.
Input: arr[] = {3, 1, 5, 4 }
Output: NO
Explanation: If we make easily make arr[] = {3, 3, 3, 0} using type-1 operation, Now it is impossible to make the array sorted because we can’t perform an operation on the last index.
Approach: To solve the problem follow the below idea:
- If n is odd, Answer will be always Yes,
- Let’s prove: We will start from index 2 to n-1 and try to make arr[i] equal to arr[i-1], but we can not perform an operation on the last index. So, After performing the operation, our array will be like this {x, x….., x, y } y can be equal to x. But if y is not equal to x, we can make all array elements equal by pairing from the initial index and performing an operation on this like this {x, x, x, x, y}. After performing the operation, our array will be like this {x, x ….., x, x } which is sorted.
- If n is even, We will start iterating from index 2 to n-1 and try to make arr[i] equal to arr[i-1] by using increasing or decreasing operations. And in the last, check if
arr[n-1] ≤ arr[n], if this condition is satisfied, the Answer will be YES, else NO.
Below are the steps to implement the above idea:
- If n is odd, then print “YES” and return.
- If n is even, then start iterating from index 2 to n-1 and try to make all elements equal to the previous element.
- But we can’t perform an operation on the last index.
- If after iterating from index 2 to n-1, if arr[n-1] ≤ arr[n], so print the answer “YES”, else “NO”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool makesorted( int * arr, int n)
{
if (n % 2 != 0) {
return true ;
}
for ( int i = 1; i <= i - 2; i++) {
int diff = arr[i - 1] - arr[i];
arr[i] += diff;
arr[i + 1] += diff;
}
if (arr[n - 2] <= arr[n - 1]) {
return true ;
}
else {
return false ;
}
}
int main()
{
int arr[] = { 3, 1, 5, 4 };
int n = sizeof (arr) / sizeof ( int );
if (makesorted(arr, n)) {
cout << "YES" << endl;
}
else {
cout << "NO" << endl;
}
return 0;
}
|
Java
import java.util.Arrays;
public class GFG {
static boolean makeSorted( int [] arr, int n)
{
if (n % 2 != 0 ) {
return true ;
}
for ( int i = 1 ; i <= i - 2 ; i++) {
int diff = arr[i - 1 ] - arr[i];
arr[i] += diff;
arr[i + 1 ] += diff;
}
if (arr[n - 2 ] <= arr[n - 1 ]) {
return true ;
}
else {
return false ;
}
}
public static void main(String[] args)
{
int [] arr = { 3 , 1 , 5 , 4 };
int n = arr.length;
if (makeSorted(arr, n)) {
System.out.println( "YES" );
}
else {
System.out.println( "NO" );
}
}
}
|
Python3
def makesorted(arr, n):
if n % 2 ! = 0 :
return True
for i in range ( 1 , n - 1 ):
diff = arr[i - 1 ] - arr[i]
arr[i] + = diff
arr[i + 1 ] + = diff
if arr[n - 2 ] < = arr[n - 1 ]:
return True
else :
return False
arr = [ 3 , 1 , 5 , 4 ]
n = len (arr)
if makesorted(arr, n):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
class GFG {
static bool MakeSorted( int [] arr, int n)
{
if (n % 2 != 0) {
return true ;
}
for ( int i = 1; i <= n - 2; i++) {
int diff = arr[i - 1] - arr[i];
arr[i] += diff;
arr[i + 1] += diff;
}
if (arr[n - 2] <= arr[n - 1]) {
return true ;
}
else {
return false ;
}
}
static void Main( string [] args)
{
int [] arr = { 3, 1, 5, 4 };
int n = arr.Length;
if (MakeSorted(arr, n)) {
Console.WriteLine( "YES" );
}
else {
Console.WriteLine( "NO" );
}
}
}
|
Javascript
function makesorted(arr) {
const n = arr.length;
if (n % 2 !== 0) {
return true ;
}
for (let i = 1; i <= n - 2; i++) {
const diff = arr[i - 1] - arr[i];
arr[i] += diff;
arr[i + 1] += diff;
}
if (arr[n - 2] <= arr[n - 1]) {
return true ;
} else {
return false ;
}
}
const arr = [3, 1, 5, 4];
if (makesorted(arr)) {
console.log( "YES" );
} else {
console.log( "NO" );
}
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
25 Aug, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...