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(|A_{i}-A_{j}|) 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)

// 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; }

**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.

**Output:**

Largest gap is : 14

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.