📜  阵列中最大的间隙

📅  最后修改于: 2021-05-04 07:10:45             🧑  作者: Mango

给定一个长度为N的未排序数组,我们必须找到该数组的任何两个元素之间的最大间隙。简单地说,找到max(| A i -A j |)其中1≤i≤N和1≤j≤N.

例子:

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

简单方法:
一个简单的解决方案是,我们可以使用朴素的方法,我们将检查数组中每对的绝对差并找到它的最大值,因此我们将运行两个循环,一个用于i,一个用于j的复杂度。方法是O(N ^ 2)

C++
// A C++ program to find largest gap 
// between two elements in an array.
#include
using namespace std;
  
// 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;
}
  
// Driver Code
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;
}
  
// This code is contributed 
// by Akanksha Rai


C
// A C program to find largest gap 
// between two elements in an array.
#include 
#include 
#include 
  
// 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.


Python3
# A Python 3 program to find largest gap 
# between two elements in an array.
import sys
  
# function to solve the given problem
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
  
# Driver Code
if __name__ == '__main__':
    arr = [-1, 2, 3, -4, -10, 22]
    size = len(arr)
    print("Largest gap is :", solve(arr, size))
  
# This code is contributed by
# Sanjit_Prasad


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
 $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.
?>


C++
// A C++ program to find largest gap between 
// two elements in an array.
#include
using namespace std;
  
// 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);
}
  
// Driver code
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;
}
  
//This code is contributed by Mukul Singh.


C
// A C program to find largest gap between 
// two elements in an array.
#include 
#include 
#include 
  
// 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.


Python3
# A python 3 program to find largest gap between 
# two elements in an array.
   
# function to solve the given problem
def solve(a,  n):
  
    min1 = a[0]
    max1 = a[0]
   
    # finding maximum and minimum of an array
    for i in range ( n):
      
        if (a[i] > max1):
            max1 = a[i]
        if (a[i] < min1):
            min1 = a[i]
      
    return abs(min1 - max1)
  
# Driver code
if __name__ == "__main__":
  
    arr = [ -1, 2, 3, 4, -10 ]
    size = len(arr)
    print("Largest gap is : " ,solve(arr, size))
  
# This code is contributed by chitranayal


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
 $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.
?>


输出:
Largest gap is : 32

更好的方法:
现在我们将看到一个更好的方法是贪婪方法,它可以解决O(N)中的这一问题。我们将找到可以在O(N)中完成的数组的最大和最小元素,然后返回(maximum-minimum)的值。

C++

// A C++ program to find largest gap between 
// two elements in an array.
#include
using namespace std;
  
// 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);
}
  
// Driver code
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;
}
  
//This code is contributed by Mukul Singh.

C

// A C program to find largest gap between 
// two elements in an array.
#include 
#include 
#include 
  
// 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.

Python3

# A python 3 program to find largest gap between 
# two elements in an array.
   
# function to solve the given problem
def solve(a,  n):
  
    min1 = a[0]
    max1 = a[0]
   
    # finding maximum and minimum of an array
    for i in range ( n):
      
        if (a[i] > max1):
            max1 = a[i]
        if (a[i] < min1):
            min1 = a[i]
      
    return abs(min1 - max1)
  
# Driver code
if __name__ == "__main__":
  
    arr = [ -1, 2, 3, 4, -10 ]
    size = len(arr)
    print("Largest gap is : " ,solve(arr, size))
  
# This code is contributed by chitranayal  

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

 $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.
?>
输出:
Largest gap is : 14