Problem · Array
Count Skipped Numbers After Subtractions
Learn this problemProblem statement
You are given two positive integers x and y, and a sequence of positive integers numbers. Your task is to change x and y through this process: iterate through numbers from left to right, subtract each integer numbers[i] from the largest number among x and y (x in case of a tie), or skip numbers[i] if it is greater than both x and y.
Return the number of integers in numbers that will be skipped based on these criteria.
Note: You are not expected to provide the most optimal solution, but a solution with time complexity not worse than O(numbers.length^2) will fit within the execution time limit.
Function
solution(x: int, y: int, numbers: int[]) → intExamples
Example 1
x = 8y = 12numbers = [5, 6, 6, 3, 1, 1, 2]return = 2- At the beginning,
x = 8andy = 12. - Since
x < ycurrently, processingnumbers[0] = 5results inx = 8andy = 7. - Since
x > ynow, processingnumbers[1] = 6results inx = 2andy = 7. - Since
x < ynow, processingnumbers[2] = 6results inx = 2andy = 1.