Rearrange given Array such that each elements is not equal to mean of adjacent elements
Last Updated :
25 Aug, 2021
Given an array arr consisting of N unique integers, the task is to rearrange the array such that element at index i of array should not be mean of adjacent elements (i.e., of index i-1 and i+1). Any possible rearrangement can be returned.
Example:
Input: arr = [5, 4, 3, 2, 1]
Output: [5, 3, 4, 2, 1]
Explanation: In the input array:
Mean(5, 3) = (5 + 3)/2 = 4,
Mean(4, 2) = (4+ 2 )/2 = 3,
Mean(3, 1) = (3 + 1)/2 = 2.
After rearranging the array as [5, 3, 4, 2, 1], now no element is the mean of adjacent elements: (5 + 4)/2 ? 3, (3 + 2)/2 ? 4, (4 + 1)/2 ? 2
Input: arr = [6, 9, 12, 25, 50 75]
Output: [6, 12, 9, 25, 50, 75 ]
Approach: The main observation to solve this problem is that for 3 numbers a, b, and c to satisfy the condition that b shouldn’t be the mean of a and c, [a, b, c] mustn’t be sorted. Therefore, this problem can be solved by following steps:
- Iterate over the array from 1 to (N-1)
- Check whether (arr[i – 1] + arr[i + 1]) / 2 == arr[i])
- If the condition is satisfied swap the elements arr[i] and arr[i+1]
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void Rearrange( int arr[], int N)
{
for ( int i = 1; i < (N - 1); i++) {
if ((arr[i - 1] + arr[i + 1]) / 2 == arr[i]) {
swap(arr[i], arr[i + 1]);
}
}
for ( int i = 0; i < N; i++) {
cout << arr[i] << " " ;
}
}
int main()
{
int arr[] = { 6, 9, 12, 25, 50, 75 };
int N = sizeof (arr) / sizeof ( int );
Rearrange(arr, N);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void Rearrange( int arr[], int N)
{
for ( int i = 1 ; i < (N - 1 ); i++) {
if ((arr[i - 1 ] + arr[i + 1 ]) / 2 == arr[i]) {
int temp = arr[i];
arr[i] = arr[i + 1 ];
arr[i + 1 ] = temp;
}
}
for ( int i = 0 ; i < N; i++) {
System.out.print(arr[i] + " " );
}
}
public static void main (String[] args) {
int arr[] = { 6 , 9 , 12 , 25 , 50 , 75 };
int N = arr.length;
Rearrange(arr, N);
}
}
|
C#
// C# program for the above approach
using System;
class GFG {
// Function to rearrange the array
static void Rearrange(int []arr, int N)
{
// Iterating for array
for (int i = 1; i < (N – 1); i++) {
// Checking whether the element i
// is mean of i-1 and i+1
if ((arr[i – 1] + arr[i + 1]) / 2 == arr[i]) {
// Rearrange by swapping arr[i] and arr[i+1]
int temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
// Printing the output array
for (int i = 0; i < N; i++) {
Console.Write(arr[i] +" ");
}
}
// Driver code
public static void Main (String[] args)
{
int []arr = { 6, 9, 12, 25, 50, 75 };
int N = arr.Length;
// calling the function
Rearrange(arr, N);
}
}
// This code is contributed by shivanisinghss2110
Python3
def Rearrange(arr, N) :
for i in range ( 1 , N - 1 ) :
if ((arr[i - 1 ] + arr[i + 1 ]) / / 2 = = arr[i]) :
arr[i], arr[i + 1 ] = arr[i + 1 ], arr[i];
for i in range (N) :
print (arr[i],end = " " )
if __name__ = = "__main__" :
arr = [ 6 , 9 , 12 , 25 , 50 , 75 ];
N = len (arr);
Rearrange(arr, N);
|
Javascript
<script>
function Rearrange(arr, N)
{
for ( var i = 1; i < (N - 1); i++) {
if ((arr[i - 1] + arr[i + 1]) / 2 == arr[i]) {
var temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
for ( var i = 0; i < N; i++) {
document.write(arr[i] + " " );
}
}
var arr = [ 6, 9, 12, 25, 50, 75 ];
var N = arr.length;
Rearrange(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...