- 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 value K such that array has at-least K elements that are >= K in C++

In this problem, we are given an array arr. Our task is to create a program to find the Maximum value K such that the array has at-least K elements that are >= K in C++.

## Problem Description

We need to find a value K, that fulfills the condition that there are K or more elements in the array that are greater than or equal to K.

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

**Input**:arr[] = {3, 5, 1, 7, 6, 6, 4, 8}

**Output**:5

## Explanation

Elements in the array that are greater than or equal to 5: 5, 6, 6, 7, 8.

## Solution Approach

A simple and effective solution to the problem is by sorting the array and checking from the last index whether the number of elements after the element is greater than the element itself. If yes, return the element.

## Example

#include <bits/stdc++.h> using namespace std; int CalcMaximumVal(int arr[], int N){ sort(arr, arr + N); for(int i = (N - 1); i >= 0; i--){ if(arr[i] <= (N - i) ) return arr[i]; } } int main(){ int arr[] = {4,7,2,3,8}; int N = sizeof(arr)/sizeof(arr[0]); cout<<"The maximum value K such that array has at-least K elements that are >= K is "<<CalcMaximumVal(arr, N); return 0; }

## Output

The maximum value K such that array has at-least K elements that are >= K is 3

- Related Articles
- Program to find k where k elements have value at least k in Python
- Find prime number K in an array such that (A[i] % K) is maximum in C++
- Maximum sum subsequence with at-least k distant elements in C++
- Find maximum value of x such that n! % (k^x) = 0 in C++
- Maximum sum subsequence with at-least k distant elements in C++ program
- Find smallest number K such that K % p = 0 and q % K = 0 in C++
- Pick maximum sum M elements such that contiguous repetitions do not exceed K in C++
- Find minimum x such that (x % k) * (x / k) == n in C++
- Place k elements such that minimum distance is maximized in C++
- Maximum sum possible for a sub-sequence such that no two elements appear at a distance < K in the array in C++
- Maximum sum possible for a sub-sequence such that no two elements appear at a distance < K in the array in C++ program
- Maximum elements that can be made equal with k updates in C++
- Print direction of moves such that you stay within the [-k, +k] boundary in C++
- Maximum subarray size, such that all subarrays of that size have sum less than k in C++
- Maximum subarray sum by flipping signs of at most K array elements in C++

Advertisements