Operations to Sort an Array in non-decreasing order
Last Updated :
25 Aug, 2023
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++
// C++ code for the above approach:
#include <bits/stdc++.h>
using namespace std;
// Function to check can we sort the
// given operation using these two
// types of operation any numbers of time
bool makesorted(int* arr, int n)
{
// If n is odd,
if (n % 2 != 0) {
return true;
}
// Iterating from index 1 to n-2
for (int i = 1; i <= i - 2; i++) {
// diff = difference between
// arr[i-1] and arr[i]
int diff = arr[i - 1] - arr[i];
// Adding diff to arr[i]
// and arr[i+1]
arr[i] += diff;
// Making all elements equal
// to first element
arr[i + 1] += diff;
}
if (arr[n - 2] <= arr[n - 1]) {
return true;
}
else {
return false;
}
}
// Driver code
int main()
{
int arr[] = { 3, 1, 5, 4 };
int n = sizeof(arr) / sizeof(int);
// Function call
if (makesorted(arr, n)) {
cout << "YES" << endl;
}
else {
cout << "NO" << endl;
}
return 0;
}
Java
// Java code for the above approach:
import java.util.Arrays;
public class GFG {
// Function to check if we can sort the given operation
// using these two types of operations any number of
// times
static boolean makeSorted(int[] arr, int n)
{
// If n is odd
if (n % 2 != 0) {
return true;
}
// Iterating from index 1 to n-2
for (int i = 1; i <= i - 2; i++) {
// diff = difference between arr[i-1] and arr[i]
int diff = arr[i - 1] - arr[i];
// Adding diff to arr[i] and arr[i+1]
arr[i] += diff;
// Making all elements equal to the first
// element
arr[i + 1] += diff;
}
if (arr[n - 2] <= arr[n - 1]) {
return true;
}
else {
return false;
}
}
// Driver code
public static void main(String[] args)
{
int[] arr = { 3, 1, 5, 4 };
int n = arr.length;
// Function call
if (makeSorted(arr, n)) {
System.out.println("YES");
}
else {
System.out.println("NO");
}
}
}
// This code is contributed by shivamgupta0987654321
Python3
# Python3 code for the above approach:
def makesorted(arr, n):
# If n is odd,
if n % 2 != 0:
return True
# Iterating from index 1 to n-2
for i in range(1, n - 1):
# diff = difference between arr[i-1] and arr[i]
diff = arr[i - 1] - arr[i]
# Adding diff to arr[i] and arr[i+1]
arr[i] += diff
arr[i + 1] += diff
# Checking if the last two elements are in non-decreasing order
if arr[n - 2] <= arr[n - 1]:
return True
else:
return False
# Driver code
arr = [3, 1, 5, 4]
n = len(arr)
# Function call
if makesorted(arr, n):
print("YES")
else:
print("NO")
# This code is contributed by shivamgupta310570
C#
using System;
class GFG {
// Function to check if we can sort the given array
// using the two types of operations any number of times
static bool MakeSorted(int[] arr, int n)
{
// If n is odd
if (n % 2 != 0) {
return true;
}
// Iterating from index 1 to n-2
for (int i = 1; i <= n - 2; i++) {
// diff = difference between arr[i-1] and arr[i]
int diff = arr[i - 1] - arr[i];
// Adding diff to arr[i] and arr[i+1]
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;
// Function call
if (MakeSorted(arr, n)) {
Console.WriteLine("YES");
}
else {
Console.WriteLine("NO");
}
}
}
// This code is contribute by shivamgupta0987654321
JavaScript
// Function to check if we can sort the given array using the operations described
function makesorted(arr) {
const n = arr.length;
// If n is odd, we can sort using any number of operations
if (n % 2 !== 0) {
return true;
}
// Iterating from index 1 to n-2
for (let i = 1; i <= n - 2; i++) {
// diff = difference between arr[i-1] and arr[i]
const diff = arr[i - 1] - arr[i];
// Adding diff to arr[i] and arr[i+1]
arr[i] += diff;
// Making all elements equal to the first element
arr[i + 1] += diff;
}
if (arr[n - 2] <= arr[n - 1]) {
return true;
} else {
return false;
}
}
// Driver code
const arr = [3, 1, 5, 4];
// Function call
if (makesorted(arr)) {
console.log("YES");
} else {
console.log("NO");
}
// This code is contributed by shivamgupta0987654321
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Minimum range increment operations to Sort an array Given an array containing N elements. It is allowed to do the below move any number of times on the array: Choose any L and R and increment all numbers in range L to R by 1. The task is to find the minimum number of such moves required to sort the array in non decreasing order. Examples: Input : arr
5 min read
Make the array non-decreasing with the given operation Given an array arr[] of size N, the task is to check if it is possible to make the array non-decreasing by applying the given operation at most once on each array element. In a single operation, one can decrease the element by one i.e. arr[i] = arr[i] - 1.Examples: Input: arr[] = {1, 2, 1, 2, 3} Out
6 min read
Check if Array can be sorted in non-decreasing order using given operations Given an array arr[] of size N consisting of positive integers, the task is to check if the array can be sorted in non-decreasing order by performing the following operations: Select two adjacent elements.Swap the elements and invert their signs.All elements in the sorted array received at the end,
8 min read
Sort a K-Increasing-Decreasing Array Given a K-increasing-decreasing array arr[], the task is to sort the given array. An array is said to be K-increasing-decreasing if elements repeatedly increase upto a certain index after which they decrease, then again increase, a total of K times. The diagram below shows a 4-increasing-decreasing
12 min read
Minimum increment operations to make the array in increasing order Given an array of size N and X. Find minimum moves required to make the array in increasing order. In each move one can add X to any element in the array. Examples: Input : a = { 1, 3, 3, 2 }, X = 2 Output : 3 Explanation : Modified array is { 1, 3, 5, 6 } Input : a = { 3, 5, 6 }, X = 5 Output : 0 O
6 min read