Minimum value of maximum absolute difference of all adjacent pairs in an Array
Last Updated :
25 Apr, 2023
Given an array arr, containing non-negative integers and (-1)s, of size N, the task is to replace those (-1)s with a common non-negative integer such that the maximum absolute difference of all adjacent pairs is minimum. Print this minimum possible value of the maximum absolute difference.
Examples:
Input: arr = {-1, -1, 11, -1, 3, -1}
Output: 4
Replace every -1 element with 7. Now the maximum absolute difference of all adjacent pairs is minimum which is equal to 4
Input: arr = {4, -1}
Output: 0
Approach:
- Consider only those non-missing elements that are adjacent to at least one missing element.
- Find the maximum element and the minimum element among them.
- We need to find a value that minimizes the maximum absolute difference between the common value and these values.
- The optimal value is equals to
(minimum element + maximum element) / 2
Below is the implementation of the above approach:
C++
// C++ program to find the minimum value
// of maximum absolute difference of
// all adjacent pairs in an Array
#include <bits/stdc++.h>
using namespace std;
// Function to find the minimum possible
// value of the maximum absolute difference.
int maximumAbsolute(int arr[], int n)
{
// To store minimum and maximum elements
int mn = INT_MAX;
int mx = INT_MIN;
for (int i = 0; i < n; i++) {
// If right side element is equals -1
// and left side is not equals -1
if (i > 0
&& arr[i] == -1
&& arr[i - 1] != -1) {
mn = min(mn, arr[i - 1]);
mx = max(mx, arr[i - 1]);
}
// If left side element is equals -1
// and right side is not equals -1
if (i < n - 1
&& arr[i] == -1
&& arr[i + 1] != -1) {
mn = min(mn, arr[i + 1]);
mx = max(mx, arr[i + 1]);
}
}
// Calculating the common integer
// which needs to be replaced with
int common_integer = (mn + mx) / 2;
// Replace all -1 elements
// with the common integer
for (int i = 0; i < n; i++) {
if (arr[i] == -1)
arr[i] = common_integer;
}
int max_diff = 0;
// Calculating the maximum
// absolute difference
for (int i = 0; i < n - 1; i++) {
int diff = abs(arr[i] - arr[i + 1]);
if (diff > max_diff)
max_diff = diff;
}
// Return the maximum absolute difference
return max_diff;
}
// Driver Code
int main()
{
int arr[] = { -1, -1, 11, -1, 3, -1 };
int n = sizeof(arr) / sizeof(arr[0]);
// Function call
cout << maximumAbsolute(arr, n);
return 0;
}
Java
// Java program to find the minimum value
// of maximum absolute difference of
// all adjacent pairs in an Array
import java.util.*;
class GFG{
// Function to find the minimum possible
// value of the maximum absolute difference.
static int maximumAbsolute(int arr[], int n)
{
// To store minimum and maximum elements
int mn = Integer.MAX_VALUE;
int mx = Integer.MIN_VALUE;
for (int i = 0; i < n; i++) {
// If right side element is equals -1
// and left side is not equals -1
if (i > 0
&& arr[i] == -1
&& arr[i - 1] != -1) {
mn = Math.min(mn, arr[i - 1]);
mx = Math.max(mx, arr[i - 1]);
}
// If left side element is equals -1
// and right side is not equals -1
if (i < n - 1
&& arr[i] == -1
&& arr[i + 1] != -1) {
mn = Math.min(mn, arr[i + 1]);
mx = Math.max(mx, arr[i + 1]);
}
}
// Calculating the common integer
// which needs to be replaced with
int common_integer = (mn + mx) / 2;
// Replace all -1 elements
// with the common integer
for (int i = 0; i < n; i++) {
if (arr[i] == -1)
arr[i] = common_integer;
}
int max_diff = 0;
// Calculating the maximum
// absolute difference
for (int i = 0; i < n - 1; i++) {
int diff = Math.abs(arr[i] - arr[i + 1]);
if (diff > max_diff)
max_diff = diff;
}
// Return the maximum absolute difference
return max_diff;
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { -1, -1, 11, -1, 3, -1 };
int n = arr.length;
// Function call
System.out.print(maximumAbsolute(arr, n));
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 program to find the minimum value
# of maximum absolute difference of
# all adjacent pairs in an Array
# Function to find the minimum possible
# value of the maximum absolute difference.
def maximumAbsolute(arr, n):
# To store minimum and maximum elements
mn = 10**9
mx = -10**9
for i in range(n):
# If right side element is equals -1
# and left side is not equals -1
if (i > 0
and arr[i] == -1
and arr[i - 1] != -1):
mn = min(mn, arr[i - 1])
mx = max(mx, arr[i - 1])
# If left side element is equals -1
# and right side is not equals -1
if (i < n - 1
and arr[i] == -1
and arr[i + 1] != -1):
mn = min(mn, arr[i + 1])
mx = max(mx, arr[i + 1])
# Calculating the common integer
# which needs to be replaced with
common_integer = (mn + mx) // 2
# Replace all -1 elements
# with the common integer
for i in range(n):
if (arr[i] == -1):
arr[i] = common_integer
max_diff = 0
# Calculating the maximum
# absolute difference
for i in range(n-1):
diff = abs(arr[i] - arr[i + 1])
if (diff > max_diff):
max_diff = diff
# Return the maximum absolute difference
return max_diff
# Driver Code
if __name__ == '__main__':
arr=[-1, -1, 11, -1, 3, -1]
n = len(arr)
# Function call
print(maximumAbsolute(arr, n))
# This code is contributed by mohit kumar 29
C#
// C# program to find the minimum value
// of maximum absolute difference of
// all adjacent pairs in an Array
using System;
class GFG{
// Function to find the minimum possible
// value of the maximum absolute difference.
static int maximumAbsolute(int []arr, int n)
{
// To store minimum and maximum elements
int mn = int.MaxValue;
int mx = int.MinValue;
for (int i = 0; i < n; i++) {
// If right side element is equals -1
// and left side is not equals -1
if (i > 0
&& arr[i] == -1
&& arr[i - 1] != -1) {
mn = Math.Min(mn, arr[i - 1]);
mx = Math.Max(mx, arr[i - 1]);
}
// If left side element is equals -1
// and right side is not equals -1
if (i < n - 1
&& arr[i] == -1
&& arr[i + 1] != -1) {
mn = Math.Min(mn, arr[i + 1]);
mx = Math.Max(mx, arr[i + 1]);
}
}
// Calculating the common integer
// which needs to be replaced with
int common_integer = (mn + mx) / 2;
// Replace all -1 elements
// with the common integer
for (int i = 0; i < n; i++) {
if (arr[i] == -1)
arr[i] = common_integer;
}
int max_diff = 0;
// Calculating the maximum
// absolute difference
for (int i = 0; i < n - 1; i++) {
int diff = Math.Abs(arr[i] - arr[i + 1]);
if (diff > max_diff)
max_diff = diff;
}
// Return the maximum absolute difference
return max_diff;
}
// Driver Code
public static void Main(string[] args)
{
int []arr = { -1, -1, 11, -1, 3, -1 };
int n = arr.Length;
// Function call
Console.Write(maximumAbsolute(arr, n));
}
}
// This code is contributed by Yash_R
JavaScript
<script>
// Javascript program to find the minimum value
// of maximum absolute difference of
// all adjacent pairs in an Array
// Function to find the minimum possible
// value of the maximum absolute difference.
function maximumAbsolute(arr, n)
{
// To store minimum and maximum elements
var mn = Number.MAX_VALUE;
var mx = Number.MIN_VALUE;
for(i = 0; i < n; i++)
{
// If right side element is equals -1
// and left side is not equals -1
if (i > 0 && arr[i] == -1 &&
arr[i - 1] != -1)
{
mn = Math.min(mn, arr[i - 1]);
mx = Math.max(mx, arr[i - 1]);
}
// If left side element is equals -1
// and right side is not equals -1
if (i < n - 1 && arr[i] == -1 &&
arr[i + 1] != -1)
{
mn = Math.min(mn, arr[i + 1]);
mx = Math.max(mx, arr[i + 1]);
}
}
// Calculating the common integer
// which needs to be replaced with
var common_integer = (mn + mx) / 2;
// Replace all -1 elements
// with the common integer
for(i = 0; i < n; i++)
{
if (arr[i] == -1)
arr[i] = common_integer;
}
var max_diff = 0;
// Calculating the maximum
// absolute difference
for(i = 0; i < n - 1; i++)
{
var diff = Math.abs(arr[i] - arr[i + 1]);
if (diff > max_diff)
max_diff = diff;
}
// Return the maximum absolute difference
return max_diff;
}
// Driver Code
var arr = [ -1, -1, 11, -1, 3, -1 ];
var n = arr.length;
// Function call
document.write(maximumAbsolute(arr, n));
// This code is contributed by umadevi9616
</script>
Time complexity: O(N), The time complexity of the given program is O(n), where n is the size of the input array. This is because the program iterates through the input array twice in two separate for-loops, which have a time complexity of O(n) each.
Auxiliary Space: O(1), The space complexity of the given program is O(1), which means it uses a constant amount of memory regardless of the size of the input array. This is because the program does not create any new data structures that depend on the size of the input array. It only uses a fixed number of integer variables to store the minimum, maximum, and common values, as well as the maximum absolute difference.
Similar Reads
Minimum and Maximum sum of absolute differences of pairs Given an array of N integers where N is even, find the minimum and maximum sum of absolute difference of N/2 pairs formed by pairing every element with one other element. Examples: Input: a[] = {10, -10, 20, -40} Output: min_sum = 40, max_sum = 80 Explanation: Pairs selected for minimum sum (-10, -4
8 min read
Count of all pairs in an Array with minimum absolute difference Given an integer array arr[] of size N, the task is to count the total number of distinct pairs having minimum absolute difference. Examples: Input: arr[] = {4, 2, 1, 3} Output: 3 Explanation: The minimum absolute difference between the pairs {1, 2}, {2, 3}, {3, 4} is 1.Input: arr[] = {1, 3, 8, 10,
5 min read
Sum of absolute differences of all pairs in a given array Given a sorted array of distinct elements, the task is to find the summation of absolute differences of all pairs in the given array. Examples: Input : arr[] = {1, 2, 3, 4} Output: 10 Sum of |2-1| + |3-1| + |4-1| + |3-2| + |4-2| + |4-3| = 10 Input : arr[] = {1, 8, 9, 15, 16} Output: 74 Input : arr[]
11 min read
Find all pairs in an Array in sorted order with minimum absolute difference Given an integer array arr[] of size N, the task is to find all distinct pairs having minimum absolute difference and print them in ascending order. Examples:Input: arr[] = {4, 2, 1, 3}Output: {1, 2}, {2, 3}, {3, 4}Explanation: The minimum absolute difference between pairs is 1.Input: arr[] = {1, 3,
9 min read
Sum of minimum absolute differences in an array Given an array of n distinct integers. The task is to find the sum of minimum absolute difference of each array element. For an element arr[i] present at index i in the array, its minimum absolute difference is calculated as: Min absolute difference (arr[i]) = min(abs(arr[i] - arr[j])), where 0 <
7 min read
Find maximum absolute difference with min sum of ratios in an Array Given an array of positive integers arr[], the task is to find the maximum absolute difference between the pairs from the array such that the sum of their ratios is minimum. Examples: Input: arr[] = {2, 6, 3, 4, 8, 9}Output: 4Explanation: In the above example, the ratios of every pair are calculated
7 min read