

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Largest gap in an array in C++
In this tutorial, we are going to write a program that finds the largest difference between the two elements in the given array.
Let's see the steps to solve the problem.
- Initialise the array.
- Find the max and min elements in the array.
- Return max - min.
Example
Let's see the code.
#include <bits/stdc++.h> using namespace std; int findLargestGap(int arr[], int n) { int max = arr[0], min = arr[0]; for (int i = 0; i < n; i++) { if (arr[i] > max) { max = arr[i]; } if (arr[i] < min) { min = arr[i]; } } return max - min; } int main() { int arr[] = {3, 4, 1, 6, 5, 6, 9, 10}; cout << findLargestGap(arr, 8) << endl; return 0; }
Output
If you run the above code, then you will get the following result.
9
Conclusion
If you have any queries in the tutorial, mention them in the comment section.
- Related Questions & Answers
- Largest Gap in Python
- Kth Largest Element in an Array
- Kth Largest Element in an Array in Python
- Constructing largest number from an array in JavaScript
- Java program to find Largest, Smallest, Second Largest, Second Smallest in an array
- Python Program to find largest element in an array
- Program to find largest element in an array in C++
- Find the largest three elements in an array in C++
- Rearrange An Array In Order – Smallest, Largest, 2nd Smallest, 2nd Largest,. Using C++
- Java program to find the largest number in an array
- Python Program to find the largest element in an array
- Finding the largest non-repeating number in an array in JavaScript
- Find the largest pair sum in an unsorted array in C++
- Maximum Gap in C++
- Binary Gap in Python
Advertisements