-
Notifications
You must be signed in to change notification settings - Fork 40
/
max_k_having_k_greater_elements.c
87 lines (82 loc) · 2.03 KB
/
max_k_having_k_greater_elements.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
* Date: 2018-10-20
*
* Description:
* Given an array of positive integers, find maximum possible value K such that
* the array has at-least K elements that are greater than or equal to K. The
* array is unsorted and may contain duplicate values.
*
* For example:
* Input: [2, 3, 4, 5, 6, 7]
* Output: 4
* Explanation : 4 elements [4, 5, 6, 7] are greater than equal to 4
*
* Approach:
* Initialise result with n and decrement result if we encounter an element
* smaller than result
*
* Complexity:
* O(N)
*
* Note:
* If array has duplicates, this solution does not work - check last solution
* from here, it requires O(N) extra space:
* https://www.geeksforgeeks.org/maximum-value-k-such-that-array-has-at-least-k-elements-that-are-k/
*/
#include "stdio.h"
#include "stdlib.h"
int main() {
int i = 0;
int n = 0;
int *a = NULL;
int res = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
a = (int *)malloc(sizeof(int)*n);
for (i = 0; i < n; i++) {
printf("Enter element[%d]: ", i);
scanf("%d", &a[i]);
}
res = n;
for (i = 0; i < n; i++) {
if (res > a[i])
res--;
}
printf("Max value of k such that, k elements are >= k: %d\n", res);
return 0;
}
/*
* Output:
* ------------------------
* Enter number of elements: 6
* Enter element[0]: 2
* Enter element[1]: 3
* Enter element[2]: 4
* Enter element[3]: 5
* Enter element[4]: 6
* Enter element[5]: 7
* Max value of k such that, k elements are >= k: 4
*
* Enter number of elements: 4
* Enter element[0]: 1
* Enter element[1]: 2
* Enter element[2]: 3
* Enter element[3]: 4
* Max value of k such that, k elements are >= k: 2
*
* Enter number of elements: 5
* Enter element[0]: 4
* Enter element[1]: 5
* Enter element[2]: 2
* Enter element[3]: 3
* Enter element[4]: 8
* Max value of k such that, k elements are >= k: 3
*
* Enter number of elements: 5
* Enter element[0]: 6
* Enter element[1]: 7
* Enter element[2]: 9
* Enter element[3]: 8
* Enter element[4]: 10
* Max value of k such that, k elements are >= k: 5
*/