1287. 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.

1
2
3
4
5
6
7
8
9
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 <= 10^4
  • 0 <= arr[i] <= 10^5

Solution

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class Solution {
    
    public int findSpecialInteger2(int[] arr) {
        int n = arr.length;
        int val1 = arr[n / 4];
        int val2 = arr[n / 4 * 2] ;  
        int val3 = arr[n / 4 * 3];
        int val4 = arr[n - 1];
        
        int count1 = countFreq(arr, val1);
        int count2 = countFreq(arr, val2);
        int count3 = countFreq(arr, val3);
        int count4 = countFreq(arr, val4);
        
        if (count1 >= count2 && count1 >= count3 && count1 >= count4) {
            return val1;
        }
        
        if (count2 >= count1 && count2 >= count3 && count2 >= count4) {
            return val2;
        }
        
        if (count3 >= count1 && count3 >= count2 && count3 >= count4) {
            return val3;
        } 
        
        return val4;
    }
    
    int countFreq(int[] arr, int val) {
        int mid = Arrays.binarySearch(arr, val);
        int right = mid;
        while (right < arr.length - 1 && arr[right] == arr[right + 1]) {
            right++;
        }
        int left = mid;
        while (left > 0 && arr[left] == arr[left - 1]) {
            left--;
        }
        return right - left;
    }
}

Solution 2

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class Solution {
   
   public int findSpecialInteger(int[] arr) {
       int n = arr.length;
       int t = n / 4;
       
       for (int i = 0; i < n - t; i++) {
           if (arr[i] == arr[i + t]) {
               return arr[i];
           }
       }
       
       return -1;
   }
}