In this program, we are going to share a c program to find the largest gap between two elements in an array. If you are a beginner and want to start learning the C programming, then keep your close attention in this tutorial as I am going to share a c program to find the largest gap between two elements in an array with the output.
We have designed this program for beginners for learning purpose. Copy below c program and execute it with c compiler to see the output of the program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #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; } |
Largest gap is : 32
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.