Generate an N-length array having length of non-decreasing subarrays maximized and minimum difference between first and last array elements
Given an array arr[ ] of size N, the task is to print an N-length array whose sum of lengths of all non-decreasing subarrays is maximum and the difference between the first and last elements is minimum.
Examples:
Input: N = 5, arr = {4, 3, 5, 3, 2}
Output: {3, 4, 5, 2, 3}
Explanation: Difference between the first and last element is minimum, i.e. 3 – 3 = 0, and sum of non-decreasing subarrays is maximum, i.e.
1. {3, 4, 5}, length = 3
2. {2, 3}, length = 2
therefore sum of non-decreasing sub-arrays is 5.
Input: N = 8, arr = {4, 6, 2, 6, 8, 2, 6, 4}
Output: {2, 4, 4, 6, 6, 6, 8, 2}
Approach: The problem can be solved greedily. Follow the steps below to solve the problem:
- Sort the array arr[ ] in non-decreasing order.
- Find the index of two consecutive elements with minimum difference, say i and i + 1.
- Swap arr[0] with arr[i] and arr[N] with arr[i + 1].
- Swap arr[1 : i – 1] with arr[i + 2 : N – 1].
- Print the array arr[ ].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void printArr( int arr[], int n)
{
sort(arr, arr + n);
int minDifference = INT_MAX;
int minIndex = -1;
for ( int i = 1; i < n; i++) {
if (minDifference
> abs (arr[i] - arr[i - 1])) {
minDifference = abs (arr[i] - arr[i - 1]);
minIndex = i - 1;
}
}
int Arr[n];
Arr[0] = arr[minIndex];
Arr[n - 1] = arr[minIndex + 1];
int pos = 1;
for ( int i = minIndex + 2; i < n; i++) {
Arr[pos++] = arr[i];
}
for ( int i = 0; i < minIndex; i++) {
Arr[pos++] = arr[i];
}
for ( int i = 0; i < n; i++) {
cout << Arr[i] << " " ;
}
}
int main()
{
int N = 8;
int arr[] = { 4, 6, 2, 6, 8, 2, 6, 4 };
printArr(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
public static void printArr( int arr[], int n)
{
Arrays.sort(arr);
int minDifference = 1000000007 ;
int minIndex = - 1 ;
for ( int i = 1 ; i < n; i++)
{
if (minDifference >
Math.abs(arr[i] - arr[i - 1 ]))
{
minDifference = Math.abs(arr[i] -
arr[i - 1 ]);
minIndex = i - 1 ;
}
}
int Arr[] = new int [n];
Arr[ 0 ] = arr[minIndex];
Arr[n - 1 ] = arr[minIndex + 1 ];
int pos = 1 ;
for ( int i = minIndex + 2 ; i < n; i++)
{
Arr[pos++] = arr[i];
}
for ( int i = 0 ; i < minIndex; i++)
{
Arr[pos++] = arr[i];
}
for ( int i = 0 ; i < n; i++)
{
System.out.print(Arr[i] + " " );
}
}
public static void main(String[] args)
{
int N = 8 ;
int arr[] = { 4 , 6 , 2 , 6 , 8 , 2 , 6 , 4 };
printArr(arr, N);
}
}
|
Python3
import sys
def printArr(arr, n):
arr.sort()
minDifference = sys.maxsize
minIndex = - 1
for i in range ( 1 ,n, 1 ):
if (minDifference > abs (arr[i] - arr[i - 1 ])):
minDifference = abs (arr[i] - arr[i - 1 ])
minIndex = i - 1
Arr = [ 0 for i in range (n)]
Arr[ 0 ] = arr[minIndex]
Arr[n - 1 ] = arr[minIndex + 1 ]
pos = 1
for i in range (minIndex + 2 ,n, 1 ):
Arr[pos] = arr[i]
pos + = 1
for i in range (minIndex):
Arr[pos] = arr[i]
pos + = 1
for i in range (n):
print (Arr[i],end = " " )
if __name__ = = '__main__' :
N = 8
arr = [ 4 , 6 , 2 , 6 , 8 , 2 , 6 , 4 ]
printArr(arr, N)
|
C#
using System;
class GFG{
public static void printArr( int []arr, int n)
{
Array.Sort(arr);
int minDifference = 1000000007;
int minIndex = -1;
for ( int i = 1; i < n; i++)
{
if (minDifference >
Math.Abs(arr[i] - arr[i - 1]))
{
minDifference = Math.Abs(arr[i] -
arr[i - 1]);
minIndex = i - 1;
}
}
int []Arr = new int [n];
Arr[0] = arr[minIndex];
Arr[n - 1] = arr[minIndex + 1];
int pos = 1;
for ( int i = minIndex + 2; i < n; i++)
{
Arr[pos++] = arr[i];
}
for ( int i = 0; i < minIndex; i++)
{
Arr[pos++] = arr[i];
}
for ( int i = 0; i < n; i++)
{
Console.Write(Arr[i] + " " );
}
}
public static void Main(String[] args)
{
int N = 8;
int []arr = { 4, 6, 2, 6, 8, 2, 6, 4 };
printArr(arr, N);
}
}
|
Javascript
<script>
function printArr(arr, n)
{
arr.sort((a, b) => a - b);
let minDifference = Number.MAX_SAFE_INTEGER;
let minIndex = -1;
for (let i = 1; i < n; i++)
{
if (minDifference > Math.abs(arr[i] - arr[i - 1]))
{
minDifference = Math.abs(arr[i] - arr[i - 1]);
minIndex = i - 1;
}
}
let Arr = new Array(n);
Arr[0] = arr[minIndex];
Arr[n - 1] = arr[minIndex + 1];
let pos = 1;
for (let i = minIndex + 2; i < n; i++)
{
Arr[pos++] = arr[i];
}
for (let i = 0; i < minIndex; i++)
{
Arr[pos++] = arr[i];
}
for (let i = 0; i < n; i++)
{
document.write(Arr[i] + " " );
}
}
let N = 8;
let arr = [ 4, 6, 2, 6, 8, 2, 6, 4 ];
printArr(arr, N);
</script>
|
Time complexity: O(N*logN)
Auxiliary Space: O(N)
Last Updated :
30 Jul, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...