Maximum difference between a pair of adjacent elements by excluding every element once
Given an array arr[] consisting of positive integers, the task for every array element is to find the maximum difference between any two adjacent arrays excluding that element.
Examples:
Input: arr[] = {1, 3, 4, 7, 8}
Output: 3, 4, 4
Explanation:
Excluding i = 2, arr[] = {1, 4, 7, 8}. Therefore, the maximum adjacent element difference is 3.
Excluding i = 3, arr[] = {1, 3, 7, 8}. Therefore, the maximum adjacent element difference is 4.
Excluding i = 4, arr[] = {1, 3, 4, 8}. Therefore, the maximum adjacent element difference is 4.
Input: arr[] = {1, 2, 7}
Output: 6
Naive Approach: The simplest approach is to use traverse the array and, for every element, calculate the difference between adjacent elements excluding that element, and print the maximum difference obtained.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void maxAdjacent( int * arr, int N)
{
vector< int > res;
for ( int i = 1; i < N - 1; i++) {
int prev = arr[0];
int maxi = INT_MIN;
for ( int j = 1; j < N; j++) {
if (i == j)
continue ;
maxi = max(maxi, abs (arr[j] - prev));
prev = arr[j];
}
res.push_back(maxi);
}
for ( auto x : res)
cout << x << " " ;
cout << endl;
}
int main()
{
int arr[] = { 1, 3, 4, 7, 8 };
int N = sizeof (arr) / sizeof (arr[0]);
maxAdjacent(arr, N);
}
|
Java
import java.util.*;
class GFG
{
static void maxAdjacent( int [] arr, int N)
{
ArrayList<Integer> res = new ArrayList<Integer>();
for ( int i = 1 ; i < N - 1 ; i++)
{
int prev = arr[ 0 ];
int maxi = Integer.MIN_VALUE;
for ( int j = 1 ; j < N; j++)
{
if (i == j)
continue ;
maxi = Math.max(maxi, Math.abs(arr[j] - prev));
prev = arr[j];
}
res.add(maxi);
}
for ( int x : res)
{
System.out.print(x + " " );
}
System.out.println();
}
public static void main(String[] args)
{
int [] arr = { 1 , 3 , 4 , 7 , 8 };
int N = arr.length;
maxAdjacent(arr, N);
}
}
|
Python3
def maxAdjacent(arr, N):
res = []
for i in range ( 1 , N - 1 ):
prev = arr[ 0 ]
maxi = - 1 * float ( 'inf' )
for j in range ( 1 ,N):
if (i = = j):
continue
maxi = max (maxi, abs (arr[j] - prev))
prev = arr[j]
res.append(maxi)
for x in res:
print (x,end = ' ' )
print ()
arr = [ 1 , 3 , 4 , 7 , 8 ]
N = len (arr)
maxAdjacent(arr, N)
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
static void maxAdjacent( int [] arr, int N)
{
List< int > res = new List< int >();
for ( int i = 1; i < N - 1; i++)
{
int prev = arr[0];
int maxi = Int32.MinValue;
for ( int j = 1; j < N; j++)
{
if (i == j)
continue ;
maxi = Math.Max(maxi, Math.Abs(arr[j] - prev));
prev = arr[j];
}
res.Add(maxi);
}
foreach ( int x in res)
{
Console.Write(x + " " );
}
Console.WriteLine();
}
static public void Main ()
{
int [] arr = { 1, 3, 4, 7, 8 };
int N = arr.Length;
maxAdjacent(arr, N);
}
}
|
Javascript
<script>
function maxAdjacent(arr, N)
{
var res = [];
for ( var i = 1; i < N - 1; i++) {
var prev = arr[0];
var maxi = Number.MIN_VALUE;
for ( var j = 1; j < N; j++) {
if (i == j)
continue ;
maxi = Math.max(maxi, Math.abs(arr[j] - prev));
prev = arr[j];
}
res.push(maxi);
}
for ( var j = 0; j < res.length; j++)
document.write(res[j] + " " );
document.write( "<br>" );
}
var arr = [ 1, 3, 4, 7, 8 ];
var N = arr.length;
maxAdjacent(arr, N);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Method: It can be clearly observed that if an element is excluded from the array, then the maximum difference either remains the same or will become equal to the difference between the next and previous of the excluded element. Follow the steps below to solve the problem:
- Calculate the maximum difference between adjacent elements of the array.
- Traverse the array and perform the following operations:
- Evaluate the difference between the previous and the array element next to the excluded element.
- If the maximum adjacent difference calculated exceeds the difference obtained in the previous step, insert the maximum adjacent difference for the whole array into the vector res.
- Otherwise, insert the difference between the previous and the array element next to the excluded element into the vector res.
- Print the vector res.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void maxAdjacent( int * arr, int N)
{
vector< int > res;
int arr_max = INT_MIN;
for ( int i = 1; i < N; i++) {
arr_max = max(arr_max,
abs (arr[i - 1] - arr[i]));
}
for ( int i = 1; i < N - 1; i++) {
int curr_max = abs (arr[i - 1]
- arr[i + 1]);
int ans = max(curr_max, arr_max);
res.push_back(ans);
}
for ( auto x : res)
cout << x << " " ;
cout << endl;
}
int main()
{
int arr[] = { 1, 3, 4, 7, 8 };
int N = sizeof (arr) / sizeof (arr[0]);
maxAdjacent(arr, N);
}
|
Java
import java.util.*;
class GFG
{
static void maxAdjacent( int []arr, int N)
{
Vector<Integer> res = new Vector<Integer>();
int arr_max = Integer.MIN_VALUE;
for ( int i = 1 ; i < N; i++)
{
arr_max = Math.max(arr_max,
Math.abs(arr[i - 1 ] - arr[i]));
}
for ( int i = 1 ; i < N - 1 ; i++)
{
int curr_max = Math.abs(arr[i - 1 ]
- arr[i + 1 ]);
int ans = Math.max(curr_max, arr_max);
res.add(ans);
}
for ( int x : res)
System.out.print(x + " " );
System.out.println();
}
public static void main(String[] args)
{
int arr[] = { 1 , 3 , 4 , 7 , 8 };
int N = arr.length;
maxAdjacent(arr, N);
}
}
|
Python3
import sys
def maxAdjacent(arr, N):
res = []
arr_max = - sys.maxsize - 1
for i in range ( 1 , N):
arr_max = max (arr_max,
abs (arr[i - 1 ] - arr[i]))
for i in range ( 1 , N - 1 ):
curr_max = abs (arr[i - 1 ]
- arr[i + 1 ])
ans = max (curr_max, arr_max)
res.append(ans)
for x in res:
print (x, end = " " )
print ()
if __name__ = = "__main__" :
arr = [ 1 , 3 , 4 , 7 , 8 ]
N = len (arr)
maxAdjacent(arr, N)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void maxAdjacent( int []arr, int N)
{
List< int > res = new List< int >();
int arr_max = Int32.MinValue;
for ( int i = 1; i < N; i++)
{
arr_max = Math.Max(arr_max,
Math.Abs(arr[i - 1] - arr[i]));
}
for ( int i = 1; i < N - 1; i++)
{
int curr_max = Math.Abs(arr[i - 1]
- arr[i + 1]);
int ans = Math.Max(curr_max, arr_max);
res.Add(ans);
}
foreach ( int x in res)
Console.Write(x + " " );
Console.WriteLine();
}
public static void Main(String[] args)
{
int [] arr = { 1, 3, 4, 7, 8 };
int N = arr.Length;
maxAdjacent(arr, N);
}
}
|
Javascript
<script>
function maxAdjacent(arr,N)
{
let res = [];
let arr_max = Number.MIN_VALUE;
for (let i = 1; i < N; i++)
{
arr_max = Math.max(arr_max,
Math.abs(arr[i - 1] - arr[i]));
}
for (let i = 1; i < N - 1; i++)
{
let curr_max = Math.abs(arr[i - 1]
- arr[i + 1]);
let ans = Math.max(curr_max, arr_max);
res.push(ans);
}
document.write(res.join( " " ));
}
let arr=[1, 3, 4, 7, 8];
let N = arr.length;
maxAdjacent(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
14 Apr, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...