Problem · Binary Search

Array Operations

Learn this problem
MediumGRGreyOrangeOA

Problem statement

You are given an array of n elements and two variables, x and y.

In each operation, you can:

  • Subtract x from one element of the array.
  • Subtract y from the remaining elements.
  • Your task is to find the minimum number of operations required to make every element of the array ≤ 0.

    Input Format:

  • The first line contains an integer n, the number of elements in the array.
  • The second line contains n space-separated integers representing the array.
  • The third line contains two integers, x and y, where x > y.
  • Output:

    Print the minimum number of operations to make each element ≤ 0.

    Function

    minOperations(arr: int[], x: int, y: int) → int

    Examples

    Example 1

    arr = [10, 20, 30, 40, 50]x = 5y = 2return = 14

    With 13 operations, the elements 30, 40, and 50 would require at least 2 + 5 + 8 = 15 selections in total, so 13 operations are impossible. With 14 operations, the shared subtraction is 28 and only 1 + 4 + 8 = 13 selections are required, so the minimum is 14.

    Constraints

    🍊🍊

    More GreyOrange problems

    drafts saved locally
    public int minOperations(int[] arr, int x, int y) {
      // write your code here
    }
    
    arr[10, 20, 30, 40, 50]
    x5
    y2
    expected14
    checking account