- Related Questions & Answers
- 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 smallest number K such that K % p = 0 and q % K = 0 in C++
- Find maximum value of x such that n! % (k^x) = 0 in C++
- Find minimum x such that (x % k) * (x / k) == n in C++
- Maximum sum subsequence with at-least k distant elements in C++ program
- Place k elements such that minimum distance is maximized in C++
- Pick maximum sum M elements such that contiguous repetitions do not exceed K in C++
- Print direction of moves such that you stay within the [-k, +k] boundary in C++
- Maximum elements that can be made equal with k updates 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 subarray size, such that all subarrays of that size have sum less than k in C++
- Shortest Subarray with Sum at Least K 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
- Program to find k where given matrix has k by k square of same value in C++

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

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++.

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

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

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.

#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; }

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

Advertisements