Sunday, February 23, 2020

1287. Element Appearing More Than 25% In Sorted Array (easy)

Q:

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

Constraints:
  • 1 <= arr.length <= 10^4
  • 0 <= arr[i] <= 10^5
A:




class Solution {
public:
    int findSpecialInteger(vector<int>& arr) {
        int curVal = arr[0];
        int count =0;
        int one4 = arr.size()/4;
        for(auto v:arr)
        {
            if(v == curVal)
            {
                count++;
                if(count > one4)
                {
                    return curVal;
                }
            }else{
                count =1;
                curVal = v;
            }
        }
        return -1; // should never hit
    }
};

1 comment:

  1. Strange "water hack" burns 2lbs overnight

    Well over 160,000 men and women are trying a simple and secret "liquids hack" to drop 1-2lbs every night in their sleep.

    It's scientific and works all the time.

    This is how to do it yourself:

    1) Get a drinking glass and fill it up with water half glass

    2) And now use this amazing hack

    and you'll become 1-2lbs thinner as soon as tomorrow!

    ReplyDelete