
- 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
Maximum consecutive numbers present in an array in C++
We are given with an array of positive integers. The goal is to find the maximum number of consecutive numbers present in it. First of all we will sort the array and then compare adjacent elements arr[j]==arr[i]+1 (j=i+1), if difference is 1 then increment count and indexes i++,j++ else change count=1. Store the maximum count found so far stored in maxc.
Input
Arr[]= { 100,21,24,73,22,23 }
Output
Maximum consecutive numbers in array : 4
Explanation − Sorted array is − { 21,22,23,24,73,100 } initialize count=1,maxcount=1
1. 22=21+1 count=2 maxcount=2 i++,j++ 2. 23=22+2 count=3 maxcount=3 i++,j++ 3. 24=23+1 count=4 maxcount=4 i++,j++ 4. 73=24+1 X count=1 maxcount=4 i++,j++ 5. 100=73+1 X count=1 maxcount=4 i++,j++
Maximum consecutive numbers is 4 { 21,22,23,24 }
Input
Arr[]= { 11,41,21,42,61,43,9,44 }
Output
Maximum consecutive numbers in array : 4
Explanation − Sorted array is − { 9,11,21,41,42,43,44,61 } initialize count=1,maxcount=1
1. 11=9+1 X count=1 maxcount=1 i++,j++ 2. 21=11+1 X count=1 maxcount=1 i++,j++ 3. 41=21+1 X count=1 maxcount=1 i++,j++ 4. 42=41+1 count=2 maxcount=2 i++,j++ 5. 43=42+1 count=3 maxcount=3 i++,j++ 6. 44=43+1 count=4 maxcount=4 i++,j++ 7. 61=44+1 X count=1 maxcount=4 i++,j++
Maximum consecutive numbers is 4 { 41,42,43,44 }
Approach used in the below program is as follows
The integer array Arr[] is used to store the integers.
Integer ‘n’ stores the length of the array.
Function subs( int arr[], int n) takes an array , its size as input and returns the maximum consecutive numbers present in the array.
First of all we will sort the array using sort(arr,arr+n)
Now initialize the count=1 and maxc=1.
Starting from the first two elements, arr[0] and arr[1] inside two for loops, compare if arr[j]==arr[i]+1 ( j=i+1), if true then increment count and i by 1.
If the above condition is false again change count to 1. Update maxc with highest count found so far ( maxc=count>maxc?count:maxc ).
In the end return maxc as number of maximum consecutive elements as result.
Example
#include <iostream> #include <algorithm> using namespace std; int subs(int arr[],int n){ std::sort(arr,arr+n); int count=1; int maxc=1; for(int i=0;i<n-1;i++){ for(int j=i+1;j<n;j++){ if(arr[j]==arr[i]+1){ count++; i++; } else count=1; maxc=count>maxc?count:maxc; } } return maxc; } int main(){ int arr[] = { 10,9,8,7,3,2,1,4,5,6 }; int n = sizeof(arr) / sizeof(int); cout << "Maximum consecutive numbers present in an array :"<<subs(arr, n); return 0; }
Output
Maximum consecutive numbers present in an array : 10
- Related Articles
- Maximum XOR of Two Numbers in an Array in C++
- Three strictly increasing numbers (consecutive or non-consecutive). in an array in JavaScript
- Maximum no. of contiguous Prime Numbers in an array in C++
- Divide Array in Sets of K Consecutive Numbers in C++
- C Program to Minimum and Maximum prime numbers in an array
- Difference between numbers and string numbers present in an array in JavaScript
- Maximum consecutive one’s (or zeros) in a binary circular array in C++
- JavaScript to check consecutive numbers in array?
- Consecutive Numbers Sum in C++
- Maximum count of equal numbers in an array after performing given operations in C++
- Find missing element in a sorted array of consecutive numbers in C++
- Count of pairs in an array that have consecutive numbers using JavaScript
- Maximum consecutive repeating character in string in C++
- Construct an array from GCDs of consecutive elements in given array in C++
- Maximum equlibrium sum in an array in C++
