Given an unsorted array of length N, and we have to find the largest gap between any two elements of the array. In simple words, find max(|Ai-Aj|) where 1 ≤ i ≤ N and 1 ≤ j ≤ N.
Examples:
Input : arr = {3, 10, 6, 7}
Output : 7
Explanation :
Here, we can see largest gap can be
found between 3 and 10 which is 7
Input : arr = {-3, -1, 6, 7, 0}
Output : 10
Explanation :
Here, we can see largest gap can be
found between -3 and 7 which is 10
Simple Approach:
A simple solution is, we can use a naive approach. We will check the absolute difference of every pair in the array, and we will find the maximum value of it. So we will run two loops one is for i and one is for j complexity of this method is O(N^2)
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
int solve( int a[], int n)
{
int max1 = INT_MIN;
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < n; j++)
{
if ( abs (a[i] - a[j]) > max1)
{
max1 = abs (a[i] - a[j]);
}
}
}
return max1;
}
int main()
{
int arr[] = { -1, 2, 3, -4, -10, 22 };
int size = sizeof (arr) / sizeof (arr[0]);
cout << "Largest gap is : "
<< solve(arr, size);
return 0;
}
|
C
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
int solve( int a[], int n)
{
int max1 = INT_MIN;
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n; j++) {
if ( abs (a[i] - a[j]) > max1) {
max1 = abs (a[i] - a[j]);
}
}
}
return max1;
}
int main()
{
int arr[] = { -1, 2, 3, -4, -10, 22 };
int size = sizeof (arr) / sizeof (arr[0]);
printf ( "Largest gap is : %d" , solve(arr, size));
return 0;
}
|
Java
import java .io.*;
class GFG
{
static int solve( int []a,
int n)
{
int max1 = Integer.MIN_VALUE ;
for ( int i = 0 ; i < n; i++)
{
for ( int j = 0 ; j < n; j++)
{
if (Math.abs(a[i] -
a[j]) > max1)
{
max1 = Math.abs(a[i] -
a[j]);
}
}
}
return max1;
}
static public void main (String[] args)
{
int []arr = {- 1 , 2 , 3 ,
- 4 , - 10 , 22 };
int size = arr.length;
System.out.println( "Largest gap is : " +
solve(arr, size));
}
}
|
Python3
import sys
def solve(a, n):
max1 = - sys.maxsize - 1
for i in range ( 0 , n, 1 ):
for j in range ( 0 , n, 1 ):
if ( abs (a[i] - a[j]) > max1):
max1 = abs (a[i] - a[j])
return max1
if __name__ = = '__main__' :
arr = [ - 1 , 2 , 3 , - 4 , - 10 , 22 ]
size = len (arr)
print ( "Largest gap is :" , solve(arr, size))
|
C#
using System;
class GFG
{
static int solve( int []a,
int n)
{
int max1 = int .MinValue ;
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < n; j++)
{
if (Math.Abs(a[i] -
a[j]) > max1)
{
max1 = Math.Abs(a[i] -
a[j]);
}
}
}
return max1;
}
static public void Main ()
{
int []arr = {-1, 2, 3,
-4, -10, 22};
int size = arr.Length;
Console.WriteLine( "Largest gap is : " +
solve(arr, size));
}
}
|
PHP
<?php
function solve( $a , $n )
{
$max1 = PHP_INT_MIN;
for ( $i = 0; $i < $n ; $i ++)
{
for ( $j = 0; $j < $n ; $j ++)
{
if ( abs ( $a [ $i ] -
$a [ $j ]) > $max1 )
{
$max1 = abs ( $a [ $i ] -
$a [ $j ]);
}
}
}
return $max1 ;
}
$arr = array (-1, 2, 3,
-4, -10, 22);
$size = count ( $arr );
echo "Largest gap is : " ,
solve( $arr , $size );
?>
|
Javascript
<script>
function solve(a,n)
{
let max1 = Number.MIN_VALUE ;
for (let i = 0; i < n; i++)
{
for (let j = 0; j < n; j++)
{
if (Math.abs(a[i] - a[j]) > max1)
{
max1 = Math.abs(a[i] - a[j]);
}
}
}
return max1;
}
let arr=[-1, 2, 3,
-4, -10, 22];
let size = arr.length;
document.write( "Largest gap is : " +
solve(arr, size));
</script>
|
OutputLargest gap is : 32
Better Approach:
Now we will see a better approach it is a greedy approach that can solve this problem in O(N). We will find the maximum and minimum element of the array which can be done in O(N) and then we will return the value of (maximum-minimum).
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
int solve( int a[], int n)
{
int min1 = a[0];
int max1 = a[0];
for ( int i = 0; i < n; i++)
{
if (a[i] > max1)
max1 = a[i];
if (a[i] < min1)
min1 = a[i];
}
return abs (min1 - max1);
}
int main()
{
int arr[] = { -1, 2, 3, 4, -10 };
int size = sizeof (arr) / sizeof (arr[0]);
cout << "Largest gap is : " << solve(arr, size);
return 0;
}
|
C
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
int solve( int a[], int n)
{
int min1 = a[0];
int max1 = a[0];
for ( int i = 0; i < n; i++) {
if (a[i] > max1)
max1 = a[i];
if (a[i] < min1)
min1 = a[i];
}
return abs (min1 - max1);
}
int main()
{
int arr[] = { -1, 2, 3, 4, -10 };
int size = sizeof (arr) / sizeof (arr[0]);
printf ( "Largest gap is : %d" , solve(arr, size));
return 0;
}
|
Java
import java.io.*;
class GFG {
static int solve( int a[], int n)
{
int min1 = a[ 0 ];
int max1 = a[ 0 ];
for ( int i = 0 ; i < n; i++)
{
if (a[i] > max1)
max1 = a[i];
if (a[i] < min1)
min1 = a[i];
}
return Math.abs(min1 - max1);
}
public static void main (String[] args)
{
int []arr = { - 1 , 2 , 3 , 4 , - 10 };
int size = arr.length;
System.out.println( "Largest gap is : "
+ solve(arr, size));
}
}
|
Python3
def solve(a, n):
min1 = a[ 0 ]
max1 = a[ 0 ]
for i in range ( n):
if (a[i] > max1):
max1 = a[i]
if (a[i] < min1):
min1 = a[i]
return abs (min1 - max1)
if __name__ = = "__main__" :
arr = [ - 1 , 2 , 3 , 4 , - 10 ]
size = len (arr)
print ( "Largest gap is : " ,solve(arr, size))
|
C#
using System;
class GFG
{
static int solve( int []a,
int n)
{
int min1 = a[0];
int max1 = a[0];
for ( int i = 0; i < n; i++)
{
if (a[i] > max1)
max1 = a[i];
if (a[i] < min1)
min1 = a[i];
}
return Math.Abs(min1 -
max1);
}
public static void Main ()
{
int []arr = {-1, 2, 3, 4, -10};
int size = arr.Length;
Console.WriteLine( "Largest gap is : " +
solve(arr, size));
}
}
|
PHP
<?php
function solve( $a , $n )
{
$min1 = $a [0];
$max1 = $a [0];
for ( $i = 0; $i < $n ; $i ++)
{
if ( $a [ $i ] > $max1 )
$max1 = $a [ $i ];
if ( $a [ $i ] < $min1 )
$min1 = $a [ $i ];
}
return abs ( $min1 - $max1 );
}
$arr = array (-1, 2, 3, 4, -10);
$size = count ( $arr );
echo "Largest gap is : " ,
solve( $arr , $size );
?>
|
Javascript
<script>
function solve(a,n)
{
let min1 = a[0];
let max1 = a[0];
for (let i = 0; i < n; i++)
{
if (a[i] > max1)
max1 = a[i];
if (a[i] < min1)
min1 = a[i];
}
return Math.abs(min1 - max1);
}
let arr=[-1, 2, 3, 4, -10 ];
let size = arr.length;
document.write( "Largest gap is : "
+ solve(arr, size));
</script>
|
OutputLargest gap is : 14
Please Login to comment...