package com.fishercoder.solutions;

/**
 * 1150. Check If a Number Is Majority Element in a Sorted Array
 *
 * Given an array nums sorted in non-decreasing order, and a number target, return True if and only if target is a majority element.
 * A majority element is an element that appears more than N/2 times in an array of length N.
 *
 * Example 1:
 * Input: nums = [2,4,5,5,5,5,5,6,6], target = 5
 * Output: true
 * Explanation:
 * The value 5 appears 5 times and the length of the array is 9.
 * Thus, 5 is a majority element because 5 > 9/2 is true.
 *
 * Example 2:
 * Input: nums = [10,100,101,101], target = 101
 * Output: false
 * Explanation:
 * The value 101 appears 2 times and the length of the array is 4.
 * Thus, 101 is not a majority element because 2 > 4/2 is false.
 *
 * Note:
 * 1 <= nums.length <= 1000
 * 1 <= nums[i] <= 10^9
 * 1 <= target <= 10^9
 **/
public class _1150 {
    public static class Solution1 {
        /**credit: https://leetcode.com/problems/check-if-a-number-is-majority-element-in-a-sorted-array/discuss/358130/Java-just-one-binary-search-O(logN))-0ms-beats-100*/
        public boolean isMajorityElement(int[] nums, int target) {
            int firstIndex = findFirstOccur(nums, target);
            int plusHalfIndex = firstIndex + nums.length / 2;
            return plusHalfIndex < nums.length && nums[plusHalfIndex] == target;
        }

        private int findFirstOccur(int[] nums, int target) {
            int left = 0;
            int right = nums.length;
            while (left < right) {
                int mid = left + (right - left) / 2;
                if (nums[mid] < target) {
                    left = mid + 1;
                } else if (nums[mid] >= target) {
                    right = mid;
                }
            }
            return left;
        }
    }
}

@@ -33,850 +33,8 @@
 s;%0A%0A
-/**%0A * 1150. Check If a Number Is Majority Element in a Sorted Array%0A *%0A * Given an array nums sorted in non-decreasing order, and a number target, return True if and only if target is a majority element.%0A * A majority element is an element that appears more than N/2 times in an array of length N.%0A *%0A * Example 1:%0A * Input: nums = %5B2,4,5,5,5,5,5,6,6%5D, target = 5%0A * Output: true%0A * Explanation:%0A * The value 5 appears 5 times and the length of the array is 9.%0A * Thus, 5 is a majority element because 5 %3E 9/2 is true.%0A *%0A * Example 2:%0A * Input: nums = %5B10,100,101,101%5D, target = 101%0A * Output: false%0A * Explanation:%0A * The value 101 appears 2 times and the length of the array is 4.%0A * Thus, 101 is not a majority element because 2 %3E 4/2 is false.%0A *%0A * Note:%0A * 1 %3C= nums.length %3C= 1000%0A * 1 %3C= nums%5Bi%5D %3C= 10%5E9%0A * 1 %3C= target %3C= 10%5E9%0A **/%0A
 publ
@@ -97,16 +97,28 @@
      /**
+%0A         * 
 credit: 
@@ -268,16 +268,26 @@
 eats-100
+%0A         
 */%0A     

refactor 1150