Binary Search to Find Next Greater Element II
Very similar problem as solved in a different blog post, the only caveat here is that if the result of the Binary Search does not yield a greater value than the target, return the smallest one ( this is the difference that I'm referring to). Code is down below, cheers, ACC. Maximize Greatness of an Array - LeetCode 2592. Maximize Greatness of an Array Medium 220 4 Add to List Share You are given a 0-indexed integer array nums . You are allowed to permute nums into a new array perm of your choosing. We define the greatness of nums be the number of indices 0 <= i < nums.length for which perm[i] > nums[i] . Return the maximum possible greatness you can achieve after permuting nums . Example 1: Input: nums = [1,3,5,2,1,3,1] Output: 4 Explanation: One of the optimal rearrangements is perm = [2,5,1,3,3,1,1]. At indices = 0, 1, 3, and 4, perm[i] > nums[i]. Hence, we retu...