LeetCode 5126. ordered array number occurs more than 25% of the element Element Appearing More Than 25% In Sorted Array

Address  https://leetcode-cn.com/contest/biweekly-contest-15/problems/element-appearing-more-than-25-in-sorted-array/

Entry describes
the orderly array of integers you a non-decreasing, this array is known exactly an integer number of times it occurs more than 25% of the total number of array elements.
Please find and return the integer

Example: 

Input: ARR = [ . 1 , 2 , 2 , 6 , 6 , 6 , 6 , . 7 , 10 ] 
output: 6 


Tip: 

. 1 <= arr.length <= 10 ^ . 4 
0 <= ARR [I] <= 10 ^ 5

Algorithm 1
sorted and more than 25% of
it can be directly compared to 25% of the length of the two numbers are equal intervals

C ++ code

class Solution {
public:
    int findSpecialInteger(vector<int>& arr) {
        int n = arr.size(); int len = n/4;
        for(int i = 0; i < n;i++){
            if(i+len < n && arr[i] == arr[i+len]){
                return arr[i];
            }
        }

        return -1;
    }
};

 

Guess you like

Origin www.cnblogs.com/itdef/p/12041657.html