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++
// C++ code for above implementation
#include <bits/stdc++.h>
using namespace std;
// Function to rearrange the array
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]
swap(arr[i], arr[i + 1]);
}
}
// Printing the output array
for (int i = 0; i < N; i++) {
cout << arr[i] << " ";
}
}
// Driver code
int main()
{
int arr[] = { 6, 9, 12, 25, 50, 75 };
int N = sizeof(arr) / sizeof(int);
// calling the function
Rearrange(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
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++) {
System.out.print(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 Potta Lokesh
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
# Python3 program for the above approach
# Function to rearrange the array
def Rearrange(arr, N) :
# Iterating for array
for i in range(1, N - 1) :
# 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]
arr[i], arr[i + 1] = arr[i + 1], arr[i];
# Printing the output array
for i in range(N) :
print(arr[i],end= " " )
# Driver code
if __name__ == "__main__" :
arr = [ 6, 9, 12, 25, 50, 75 ];
N = len(arr);
# calling the function
Rearrange(arr, N);
# This code is contributed by AnkThon
JavaScript
<script>
// JavaScript program for the above approach
// Function to rearrange the array
function Rearrange(arr, N)
{
// Iterating for array
for (var 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]
var temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
// Printing the output array
for (var i = 0; i < N; i++) {
document.write(arr[i] +" ");
}
}
// Driver code
var arr = [ 6, 9, 12, 25, 50, 75 ];
var N = arr.length;
// calling the function
Rearrange(arr, N);
// This code is contributed by shivanisinghss2110
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Cost of rearranging the array such that no element exceeds the sum of its adjacent elements
Given an array arr[] of N unique integers, the task is to find the cost to arrange them in a circular arrangement in such a way that every element is less than or equal to the sum of its adjacent elements. Cost of moving an element from index i in original array to index j in final arrangement is |i
8 min read
Minimum elements to be removed such that sum of adjacent elements is always even
Given an array of N integers. The task is to eliminate the minimum number of elements such that in the resulting array the sum of any two adjacent values is even. Examples: Input : arr[] = {1, 2, 3} Output : 1 Remove 2 from the array. Input : arr[] = {1, 3, 5, 4, 2} Output : 2 Remove 4 and 2. Approa
4 min read
Rearrange array such that difference of adjacent elements is in descending order
Given an array a[] with n integers the task is to rearrange the elements of the array in such a way that the differences of the adjacent elements are in descending order.Examples: Input : arr[] = {1, 2, 3, 4, 5, 6} Output : 6 1 5 2 4 3 Explanation: For first two elements the difference is abs(6-1)=5
6 min read
Minimum elements to be removed such that sum of adjacent elements is always odd
Given an array of N integers. The task is to eliminate the minimum number of elements such that in the resulting array the sum of any two adjacent values is odd. Examples: Input: arr[] = {1, 2, 3} Output: 0 Sum of all adjacent elements is already odd. Input: arr[] = {1, 3, 5, 4, 2} Output: 3 Elimina
5 min read
Rearrange given Array by replacing every element with the element located at mean of adjacent elements
Given an array arr[]. The array contains numbers from 0 to N-1 only, where N is the size of arr[]. The task is to modify the array in such that for each i from 0â¤i<N, arr[i] = arr[ (arr[i-1] + arr[i+1]) / 2 ] There are a few exceptions: For first element of the array, arr[i-1] = 0; because previo
9 min read
Modify Array such that no element is smaller/greater than half/double of its adjacent elements
Given an array, arr[] of size N. Find the minimum number of elements we need to insert between array elements such that the maximum element from two adjacent elements is not more than twice bigger than the minimum element i.e., max(arr[i], arr[i+1]) ? 2 * min(arr[i], arr[i+1]) where 0 ? i < N - 1
7 min read
Check if given array can be rearranged such that mean is equal to median
Given sorted float array arr[]. Check if arr[] can be rearranged such that its mean is equal to its median. Examples: Input: arr[] = {1.0, 3.0, 6.0, 9.0, 12.0, 32.0}Output: YesExplanation: The mean of given array is (1.0 + 3.0 + 6.0 + 9.0 + 12.0 + 32.0) / 6 = 10.5. Rearranging given array as {1.0, 3
11 min read
Make all array elements even by replacing adjacent pair of array elements with their sum
Given an array arr[] of size N, the task is to make all array elements even by replacing a pair of adjacent elements with their sum. Examples: Input: arr[] = { 2, 4, 5, 11, 6 }Output: 1Explanation:Replacing a pair (arr[2], arr[3]) with their sum ( = 5 + 11 = 16) modifies arr[] to { 2, 4, 16, 16, 6 }
8 min read
Rearrange the Array to maximize the elements which is smaller than both its adjacent elements
Given an array arr[] consisting of N distinct integers, the task is to rearrange the array elements such that the count of elements that are smaller than their adjacent elements is maximum. Note: The elements left of index 0 and right of index N-1 are considered as -INF. Examples: Input: arr[] = {1,
7 min read
Rearrange array such that even index elements are smaller and odd index elements are greater
Given an array, rearrange the array such that : If index i is even, arr[i] <= arr[i+1]If index i is odd, arr[i] >= arr[i+1] Note: There can be multiple answers. Examples: Input : arr[] = {2, 3, 4, 5} Output : arr[] = {2, 4, 3, 5} Explanation : Elements at even indexes are smaller and elements
10 min read