2023-12-11 Daily Challenge

Today I have done leetcode's December LeetCoding Challenge with cpp.

December LeetCoding Challenge 11

Description

Element Appearing More Than 25% In Sorted Array

Given an integer array sorted in non-decreasing order, there is exactly one integer in the array that occurs more than 25% of the time, return that integer.

 

Example 1:

Input: arr = [1,2,2,6,6,6,6,7,10]
Output: 6

Example 2:

Input: arr = [1,1]
Output: 1

 

Constraints:

  • 1 <= arr.length <= 104
  • 0 <= arr[i] <= 105

Solution

class Solution {
public:
  int findSpecialInteger(vector<int>& arr) {
    int current = -1;
    int count = 0;
    for(auto i : arr) {
      if(current != i) {
        count = 0;
        current = i;
      }
      count += 1;
      if(count * 4 > arr.size()) {
        return current;
      }
    }
    return -1;
  }
};

// Accepted
// 25/25 cases passed (8 ms)
// Your runtime beats 78.76 % of cpp submissions
// Your memory usage beats 92.26 % of cpp submissions (12.7 MB)