fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int maxArea(int arr[],int n);
  5.  
  6. int main()
  7. {
  8. int t;
  9. cin>>t;
  10. while(t--)
  11. {
  12. int n;
  13. cin>>n;
  14. int arr[n];
  15. for(int i = 0;i<n;i++)
  16. cin>>arr[i];
  17. cout<<maxArea(arr,n)<<endl;
  18. }
  19. return 0;
  20. }
  21.  
  22. bool isSquarePossible(int arr[], int n, int l)
  23. {
  24.  
  25. // To store the count of elements
  26. // greater than or equal to l
  27. int cnt = 0;
  28. for (int i = 0; i < n; i++) {
  29.  
  30. // Increment the count
  31. if (arr[i] >= l)
  32. cnt++;
  33.  
  34. // If the count becomes greater
  35. // than or equal to l
  36. if (cnt >= l)
  37. return true;
  38. }
  39.  
  40. return false;
  41. }
  42.  
  43. int maxArea(int arr[], int n)
  44. {
  45. int l = 0, r = n;
  46. int len = 0;
  47. while (l <= r) {
  48. int m = l + ((r - l) / 2);
  49.  
  50. // If square is possible with
  51. // side length m
  52. if (isSquarePossible(arr, n, m)) {
  53. len = m;
  54. l = m + 1;
  55. }
  56.  
  57. // Try to find a square with
  58. // smaller side length
  59. else
  60. r = m - 1;
  61. }
  62.  
  63. // Return the area
  64. return len;
  65. }
Success #stdin #stdout 0s 4508KB
stdin
1
5
1 3 4 5 5 
stdout
3