← Home

1608. Special Array With X Elements Greater Than or Equal X

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
greedyC++Markdown
160

The trick here is to name the state correctly, then let the implementation follow. For 1608. Special Array With X Elements Greater Than or Equal X, the solution in this repository is mainly a greedy solution.

Guide

What?

Before optimizing anything, pin down what information is still useful after each move. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: greedy.

Guide

When?

Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are specialArray.

Guide

Why?

The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.

  • Sorting is used to make local choices comparable, so the later scan does not have to rediscover order.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Read the setup variables first.
  2. Follow the main loop or recursive helper next.
  3. Watch where invalid states get skipped.
  4. Check which value survives to the return statement.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 4 ms, faster than 100.00% of C++ online submissions for Special Array With X Elements Greater Than or Equal X.
02//Memory Usage: 8.6 MB, less than 16.67% of C++ online submissions for Special Array With X Elements Greater Than or Equal X.
03class Solution {
04public:
05    int specialArray(vector<int>& nums) {
06        int n = nums.size();
07        
08        sort(nums.begin(), nums.end());
09        
10        int cur = 0;
11        for(int x = 0; x <= n; ++x){
12            while(cur < n && nums[cur] < x){
13                ++cur;
14            }
15            
16            //cur : count of elements < x
17            //n-cur: count of elements >= x
18            // cout << x << " : " << n-cur << endl;
19            if(n-cur == x) return x;
20        }
21        
22        return -1;
23    }
24};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.