Largest gap in an array

Given an unsorted array of length N and we have to find largest gap between any two elements of 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 naive approach.We will check 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)

C

// A C program to find largest gap 
// between two elements in an array.
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
  
// function to solve the given problem
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

// A Java program to find 
// largest gap between 
// two elements in an array.
import java .io.*;
  
class GFG
{
  
// function to solve
// the given problem
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;
}
  
// Driver Code
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));
}
}
  
// This code is contributed
// by anuj_67.

C#

// A C# program to find 
// largest gap between 
// two elements in an array.
using System;
  
class GFG
{
  
// function to solve
// the given problem
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;
}
  
// Driver Code
static public void Main ()
{
    int []arr = {-1, 2, 3, 
                 -4, -10, 22};
    int size = arr.Length;
    Console.WriteLine("Largest gap is : " +
                         solve(arr, size));
}
}
  
// This code is contributed
// by anuj_67.

PHP

<?php
// A PHP program to find 
// largest gap between 
// two elements in an array.
  
// function to solve 
// the given problem
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;
}
  
// Driver Code
$arr = array(-1, 2, 3, 
             -4, -10, 22);
$size = count($arr);
echo "Largest gap is : "
      solve($arr, $size);
  
// This code is contributed
// by anuj_67.
?>

Output:

Largest gap is : 32

Better Approach:
Now we will see a better approach it is greedy approach which can solve this problem in O(N). we will find maximum and minimum element of the array which can be done in O(N) and then we will return value of (maximum-minimum).

C

// A C program to find largest gap between 
// two elements in an array.
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
  
// function to solve the given problem
int solve(int a[], int n)
{
    int min1 = a[0];
    int max1 = a[0];
  
    // finding maximum and minimum of an array
    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

// A Java program to find largest gap
// between two elements in an array.
import java.io.*;
  
class GFG {
  
    // function to solve the given
    // problem
    static int solve(int a[], int n)
    {
        int min1 = a[0];
        int max1 = a[0];
      
        // finding maximum and minimum 
        // of an array
        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);
    }
  
    // Driver code
    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));
    }
}
  
// This code is contributed by anuj_67.

C#

// A C# program to find 
// largest gap between 
// two elements in an array.
using System;
  
class GFG 
{
  
    // function to solve 
    // the given problem
    static int solve(int []a, 
                     int n)
    {
        int min1 = a[0];
        int max1 = a[0];
      
        // finding maximum and 
        // minimum of an array
        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);
    }
  
    // Driver code
    public static void Main ()
    {
        int []arr = {-1, 2, 3, 4, -10};
        int size = arr.Length;
        Console.WriteLine("Largest gap is : "
                             solve(arr, size));
    }
}
  
// This code is contributed
// by anuj_67.

PHP

<?php
// A PHP program to find 
// largest gap between 
// two elements in an array.
  
// function to solve
// the given problem
function solve($a, $n)
{
    $min1 = $a[0];
    $max1 = $a[0];
  
    // finding maximum and
    // minimum of an array
    for ($i = 0; $i < $n; $i++) 
    {
        if ($a[$i] > $max1)
            $max1 = $a[$i];
        if ($a[$i] < $min1)
            $min1 = $a[$i];
    }
      
    return abs($min1 - $max1);
}
  
// Driver Code
$arr = array(-1, 2, 3, 4, -10);
$size = count($arr);
echo "Largest gap is : "
      solve($arr, $size);
  
// This code is contributed
// by anuj_67.
?>

Output:

Largest gap is : 14


My Personal Notes arrow_drop_up


[ Intern at GeeksForGeeks ] I am a Computer Science and Engineering Undergraduate at MNNIT Allahabad I love to do competitive programming and software development I like to contribute open source projects I always believe in learning new technologies

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : vt_m

Article Tags :
Practice Tags :



Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.