Given an array arr[] consisting of N integers, rearrange the array such that it satisfies the following conditions:
- arr[0] must be 1.
- Difference between adjacent array elements should not exceed 1, that is, arr[i] – arr[i-1] ≤ 1 for all 1 ≤ i < N.
The permissible operations are as follows:
- Rearrange the elements in any way.
- Reduce any element to any number ≥ 1.
The task is to find the maximum possible value that can be placed at the last index of the array.
Examples:
Input: arr[] = {3, 1, 3, 4}
Output: 4
Explanation:
Subtracting 1 from the first element modifies the array to {2, 1, 3, 4}.
Swapping the first two elements modifes the array to {1, 2, 3, 4}.
Therefore, maximum value placed at the last index is 4.
Input: arr[] = {1, 1, 1, 1}
Output: 1
Approach:
To solve the given problem, sort the given array and balance it according to the given condition starting from left towards right. Follow the below steps to solve the problem:
- Sort the array in ascending order.
- If the first element is not 1, make it 1.
- Traverse the array over the indices [1, N – 1) and check if every adjacent element has a difference of ≤ 1.
- If not, decrement the value till the difference becomes ≤ 1.
- Return the last element of the array.
Below is the implementation of above problem:
C++
// C++ Program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Function to find the maximum possible value // that can be placed at the last index int maximizeFinalElement(int arr[], int n) { // Sort array in ascending order sort(arr, arr + n); // If the first element // is not equal to 1 if (arr[0] != 1) arr[0] = 1; // Traverse the array to make // difference between adjacent // elements <=1 for (int i = 1; i < n; i++) { if (arr[i] - arr[i - 1] > 1) { arr[i] = arr[i - 1] + 1; } } return arr[n - 1]; } // Driver Code int main() { int n = 4; int arr[] = { 3, 1, 3, 4 }; int max = maximizeFinalElement(arr, n); cout << max; return 0; } |
Java
// Java program to implement // the above approach import java.io.*; import java.util.*; class GFG{ // Function to find the maximum possible value // that can be placed at the last index public static int maximizeFinalElement(int arr[], int n) { // Sort the array elements // in ascending order Arrays.sort(arr); // If the first element is // is not equal to 1 if (arr[0] != 1) arr[0] = 1; // Traverse the array to make // difference between adjacent // elements <=1 for(int i = 1; i < n; i++) { if (arr[i] - arr[i - 1] > 1) { arr[i] = arr[i - 1] + 1; } } return arr[n - 1]; } // Driver Code public static void main (String[] args) { int n = 4; int arr[] = { 3, 1, 3, 4 }; int max = maximizeFinalElement(arr, n); System.out.print(max); } } |
Python3
# Python3 program to implement # the above approach # Function to find the maximum possible value # that can be placed at the last index def maximizeFinalElement(arr, n): # Sort the array elements # in ascending order arr.sort(); # If the first element is # is not equal to 1 if (arr[0] != 1): arr[0] = 1; # Traverse the array to make # difference between adjacent # elements <=1 for i in range(1, n): if (arr[i] - arr[i - 1] > 1): arr[i] = arr[i - 1] + 1; return arr[n - 1]; # Driver Code if __name__ == '__main__': n = 4; arr = [3, 1, 3, 4]; max = maximizeFinalElement(arr, n); print(max); # This code is contributed by Princi Singh |
C#
// C# Program to implement // the above approach using System; class GFG{ // Function to find the maximum possible value // that can be placed at the last index public static int maximizeFinalElement(int []arr, int n) { // Sort the array elements // in ascending order Array.Sort(arr); // If the first element is // is not equal to 1 if (arr[0] != 1) arr[0] = 1; // Traverse the array to make // difference between adjacent // elements <=1 for (int i = 1; i < n; i++) { if (arr[i] - arr[i - 1] > 1) { arr[i] = arr[i - 1] + 1; } } return arr[n - 1]; } // Driver Code public static void Main(String[] args) { int n = 4; int []arr = { 3, 1, 3, 4 }; int max = maximizeFinalElement(arr, n); Console.WriteLine(max); } } // This code is contributed by sapnasingh4991 |
4
Time Complexity: O(NlogN)
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Minimum value by which each Array element must be added as per given conditions
- Maximum modified Array sum possible by choosing elements as per the given conditions
- Maximum number of objects that can be created as per given conditions
- Minimum cost to reduce the integer N to 1 as per given conditions
- Count of Binary Strings possible as per given conditions
- Rearrange Array to maximize number having Array elements as digits based on given conditions
- Maximize Profit by trading stocks based on given rate per day
- Maximize the Sum of a Subsequence from an Array based on given conditions
- Find the last remaining Character in the Binary String according to the given conditions
- Last seen array element (last appearance is earliest)
- Generate an array B[] from the given array A[] which satisfies the given conditions
- Generate an array using given conditions from a given array
- Arrange the numbers in the Array as per given inequalities
- Number of K's such that the given array can be divided into two sets satisfying the given conditions
- Find last element after deleting every second element in array of n integers
- Arrange array elements such that last digit of an element is equal to first digit of the next element
- Is it possible to reach N and M from 1 and 0 respectively as per given conditio
- Nth angle of a Polygon whose initial angle and per angle increment is given
- Perform range sum queries on string as per given condition
- Remove an element to maximize the GCD of the given array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

