← Home

1287. Element Appearing More Than 25% In Sorted Array

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
128

Let's make this one less mysterious. For 1287. Element Appearing More Than 25% In Sorted Array, the solution in this repository is mainly a straightforward implementation 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: straightforward implementation.

Guide

When?

Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are findSpecialInteger.

Guide

Why?

The solution works because it narrows the problem until every update has a clear reason to exist.

  • 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. Initialize the memory or helper structure.
  2. Process candidates in the order the invariant expects.
  3. Update the answer only when the current state is valid.
  4. Return the value that represents the fully processed input.

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: 16 ms, faster than 85.75% of C++ online submissions for Element Appearing More Than 25% In Sorted Array.
02//Memory Usage: 9.5 MB, less than 100.00% of C++ online submissions for Element Appearing More Than 25% In Sorted Array.
03
04class Solution {
05public:
06    int findSpecialInteger(vector<int>& arr) {
07        int n = arr.size();
08        int cur = arr[0], count = 0;
09        
10        for(int i = 0; i < n; i++){
11            if(arr[i] == cur){
12                count++;
13            }else{
14                count = 1; //cur is counted
15                cur = arr[i];
16            }
17            // cout << i << ", " << count << endl;
18            if(count * 4 > n){
19                return arr[i];
20            }
21        }
22        
23        return 0;
24    }
25};

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.