Given n integers, which form a circle. Find the minimal absolute value of any adjacent pair. If there are many optimum solutions, output any of them.
Note: they are in circle
Examples:
Input : arr[] = {10, 12, 13, 15, 10}
Output : 0
Explanation: |10 - 10| = 0 which is the
minimum possible.
Input : arr[] = {10, 20, 30, 40}
Output : 10
Explanation: |10 - 20| = 10 which is the
minimum, 2 3 or 3 4 can be the answers also.
Initially consider the minimum value to be of first and second elements. Traverse from second element to last. Check the difference of every adjacent pair and store the minimum value. When last element is reached, check its difference with first element.
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 program to check the above function int main() { int a[] = { 10, 12, 13, 15, 10 }; int n = sizeof(a) / sizeof(a[0]); 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. ?> |
Output:
Min Difference = 0
Time complexity: O(n)
This article is contributed by striver. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Minimize the maximum absolute difference of adjacent elements in a circular array
- Missing occurrences of a number in an array such that maximum absolute difference of adjacent elements is minimum
- Sort elements of an array in increasing order of absolute difference of adjacent elements
- Maximise sum of absolute difference between adjacent elements in Array with sum K
- Minimum value of maximum absolute difference of all adjacent pairs in an Array
- Sort an Array based on the absolute difference of adjacent elements
- Sort array such that absolute difference of adjacent elements is in increasing order
- Maximum sum in circular array such that no two elements are adjacent
- Maximum sum in circular array such that no two elements are adjacent | Set 2
- Maximize removal of adjacent array elements based on their absolute value
- Arrange N elements in circular fashion such that all elements are strictly less than sum of adjacent elements
- Sum of minimum absolute difference of each array element
- Count elements in first Array with absolute difference greater than K with an element in second Array
- Count of all pairs in an Array with minimum absolute difference
- k-th smallest absolute difference of two elements in an array
- Absolute Difference of even and odd indexed elements in an Array
- Array value by repeatedly replacing max 2 elements with their absolute difference
- Absolute Difference of all pairwise consecutive elements in an array
- Maximum absolute difference between distinct elements in an Array
- Find K elements whose absolute difference with median of array is maximum

