- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Maximum occurred integer in n ranges using C++

In this problem, we are given N ranges. Our task is to *maximum occurred integer in n ranges*.

For the starting and ending value of all ranges. We need to find the value which occurs the most.

**Let’s take an example to understand the problem,**

**Input**

S1 = 1, E1 = 3 S2 = 2, E2 = 6 S3 = 3, E3 = 4

**Output**

2

## Solution Approach

A simple approach t o solve the problem is by using hashing, we will use a hash table to count all members and their count. We will traverse all ranges and store count in the hash table, then we will find the maximum count.

Another approach to solve the problem in linear time is using a range array. In this array, we will update the index of all start values of range by adding 1 and all end values of range by subtracting 1 from it. The will find prefix sum and find the maximum prefix sum value.

## Example

Program to illustrate the working of our solution

#include <bits/stdc++.h> #define MAX 1000000 using namespace std; int findMaxOccrEle(int L[], int R[], int n){ int occurenceConut[MAX]; memset(occurenceConut, 0, sizeof occurenceConut); int maxi = -1; for (int i = 0; i < n; i++) { occurenceConut[L[i]] += 1; occurenceConut[R[i] + 1]; if(R[i]>maxi){ maxi=R[i]; } } int prefSum = occurenceConut[0],maxEleIndex; for (int i = 1; i < maxi+1; i++) { occurenceConut[i] += occurenceConut[i - 1]; if (prefSum < occurenceConut[i]) { prefSum = occurenceConut[i]; maxEleIndex = i; } } return maxEleIndex; } int main(){ int L[] = { 1, 2, 3 }; int R[] = { 3, 6, 4 }; int n = sizeof(L) / sizeof(L[0]); cout<<"The maximum occurred integer in the range is "<<findMaxOccrEle(L, R, n); return 0; }

## Output

The maximum occurred integer in the range is 3

- Related Articles
- Queries for maximum difference between prime numbers in given ranges in C++
- Find k-th smallest element in given n ranges in C++
- Missing Ranges in C++
- Maximum subarray sum in O(n) using prefix sum in C++
- Maximum XOR using K numbers from 1 to n in C++
- How to print the maximum occurred character of a string in Java?
- Count Primes in Ranges in C++
- Find a range that covers all the elements of given N ranges in C++
- Python - Consecutive Ranges of K greater than N
- Queries to check if a number lies in N ranges of L-R in C++
- Find the kth element in the series generated by the given N ranges in C++
- What is the common error occurred while using scanf() statement in C language?
- Queries to check if a number lies in N ranges of L-R in C++ Program
- Maximum circular subarray sum in C++\n
- What is the maximum possible value of an integer in C# ?