Minimum absolute difference of adjacent elements in a Circular Array
Last Updated :
18 Sep, 2023
Given a circular array arr[] of length N, the task is to find the minimum absolute difference between any adjacent pair. If there are many optimum solutions, output any of them.
Examples:
Input: arr[] = {10, 12, 13, 15, 10}
Output: 0
Explanation: |10 - 10| = 0 is the minimum possible difference.
Input: arr[] = {10, 20, 30, 40}
Output: 10
Explanation: |10 - 20| = 10 is the minimum, 20 30 or 30 40 could be the answer too.
Approach: Below is the idea to solve the problem
Traverse from second element to last an check the difference of every adjacent pair and store the minimum value. The edge case is to check difference between last element and first element.
Follow the steps below to implement the idea:
- Create a variable res to store the minimum difference between any adjacent pair.
- Run a for loop of i from 1 to N-1 and for each iteration.
- Calculate the absolute difference of Arr[i] and Arr[i-1].
- Update the value of res if the value of | Arr[i] - Arr[i-1] | is smaller than res.
- Return res as the required answer.
Below is the implementation of the above approach.
C++
// C++ program to find maximum difference
// between adjacent elements in a circular array.
#include <bits/stdc++.h>
using namespace std;
void minAdjDifference(int arr[], int n)
{
if (n < 2)
return;
// Checking normal adjacent elements
int res = abs(arr[1] - arr[0]);
for (int i = 2; i < n; i++)
res = min(res, abs(arr[i] - arr[i - 1]));
// Checking circular link
res = min(res, abs(arr[n - 1] - arr[0]));
cout << "Min Difference = " << res;
}
// Driver code
int main()
{
int a[] = { 10, 12, 13, 15, 10 };
int n = sizeof(a) / sizeof(a[0]);
//Function Call
minAdjDifference(a, n);
return 0;
}
Java
// Java program to find maximum difference
// between adjacent elements in a circular
// array.
class GFG {
static void minAdjDifference(int arr[], int n)
{
if (n < 2)
return;
// Checking normal adjacent elements
int res = Math.abs(arr[1] - arr[0]);
for (int i = 2; i < n; i++)
res = Math.min(res, Math.abs(arr[i] - arr[i - 1]));
// Checking circular link
res = Math.min(res, Math.abs(arr[n - 1] - arr[0]));
System.out.print("Min Difference = " + res);
}
// driver code
public static void main(String arg[])
{
int a[] = { 10, 12, 13, 15, 10 };
int n = a.length;
minAdjDifference(a, n);
}
}
// This code is contributed by Anant Agarwal
// and improved by Anuj Sharma.
Python3
# Python3 program to find maximum
# difference between adjacent
# elements in a circular array.
def minAdjDifference(arr, n):
if (n < 2): return
# Checking normal adjacent elements
res = abs(arr[1] - arr[0])
for i in range(2, n):
res = min(res, abs(arr[i] - arr[i - 1]))
# Checking circular link
res = min(res, abs(arr[n - 1] - arr[0]))
print("Min Difference = ", res)
# Driver Code
a = [10, 12, 13, 15, 10]
n = len(a)
minAdjDifference(a, n)
# This code is contributed by Anant Agarwal
# and improved by Anuj Sharma.
C#
// C# program to find maximum difference
// between adjacent elements in a circular array.
using System;
class GFG {
static void minAdjDifference(int[] arr, int n)
{
if (n < 2)
return;
// Checking normal adjacent elements
int res = Math.Abs(arr[1] - arr[0]);
for (int i = 2; i < n; i++)
res = Math.Min(res, Math.Abs(arr[i] - arr[i - 1]));
// Checking circular link
res = Math.Min(res, Math.Abs(arr[n - 1] - arr[0]));
Console.Write("Min Difference = " + res);
}
// driver code
public static void Main()
{
int[] a = { 10, 12, 13, 15, 10 };
int n = a.Length;
minAdjDifference(a, n);
}
}
// This code is contributed by Anant Agarwal
// and improved by Anuj Sharma.
PHP
<?php
// PHP program to find maximum
// difference between adjacent
// elements in a circular array.
function minAdjDifference($arr, $n)
{
if ($n < 2)
return;
// Checking normal
// adjacent elements
$res = abs($arr[1] - $arr[0]);
for ($i = 2; $i < $n; $i++)
$res = min($res,
abs($arr[$i] -
$arr[$i - 1]));
// Checking circular link
$res = min($res, abs($arr[$n - 1] -
$arr[0]));
echo "Min Difference = ", $res;
}
// Driver Code
$a = array(10, 12, 13, 15, 10);
$n = count($a);
minAdjDifference($a, $n);
//This code is contributed by anuj_67
//and improved by Anuj Sharma.
?>
JavaScript
<script>
// Javascript program to find maximum difference
// between adjacent elements in a circular array.
function minAdjDifference( arr, n)
{
if (n < 2)
return;
// Checking normal adjacent elements
let res = Math.abs(arr[1] - arr[0]);
for (let i = 2; i < n; i++)
res = Math.min(res, Math.abs(arr[i] - arr[i - 1]));
// Checking circular link
res = Math.min(res, Math.abs(arr[n - 1] - arr[0]));
document.write("Min Difference = " + res);
}
// driver code
let a = [ 10, 12, 13, 15, 10 ];
let n = a.length;
minAdjDifference(a, n);
</script>
Time Complexity: O(N), as we are using a loop to traverse N times.
Auxiliary Space: O(1), as we are not using any extra space.
Similar Reads
Minimize the maximum absolute difference of adjacent elements in a circular array Given a circular array arr of N integers, the task is to minimize the maximum absolute difference of adjacent elements of the array without any removals. Examples: Input: arr[] = {1, 3, 10, 2, 0, 9, 6} Output: {0, 2, 6, 10, 9, 3, 1} Explanation: In the above example, the maximum difference between a
6 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 minimum difference with adjacent elements in Array Given an array A[] of N integers, the task is to find min(A[0], A[1], ..., A[i-1]) - min(A[i+1], A[i+2], ..., A[n-1]) for each i (1 ⤠i ⤠N). Note: If there are no elements at the left or right of i then consider the minimum element towards that part zero. Examples: Input: N = 4, A = {8, 4, 2, 6}Out
12 min read
Sort an Array based on the absolute difference of adjacent elements Given an array arr[] containing N integers, the task is to rearrange all the elements of array such that absolute difference between consecutive elements of the array are sorted in increasing order.Examples Input: arr[] = { 5, -2, 4, 8, 6, 5 } Output: 5 5 6 4 8 -2 Explanation: |5 - 5| = 0 |5 - 6| =
7 min read
Split array into K subarrays with minimum sum of absolute difference between adjacent elements Given an array, arr[] of size N and an integer K, the task is to split the array into K subarrays minimizing the sum of absolute difference between adjacent elements of each subarray. Examples: Input: arr[] = {1, 3, -2, 5, -1}, K = 2Output: 13Explanation: Split the array into following 2 subarrays:
8 min read