
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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 Articles
- 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
- Rearrange An Array In Order – Smallest, Largest, 2nd Smallest, 2nd Largest,. Using C++
- Program to find largest element in an array in C++
- Find the largest three elements in an array in C++
- Python Program to find 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++
- How to Find the Largest Palindrome in an Array in Java?
- Java program to find the largest number in an array
- Python Program to find the largest element in an array
- Golang Program to Find the Largest Element in an Array

Advertisements