703454. Surpasser Count
Medium
Surpasser Count
Summary
The Surpasser Count problem involves finding the number of elements in an array that are greater than a given element. This problem requires understanding basic array manipulation and comparison techniques.
Detailed Explanation
To solve this problem, we can iterate through the array and count the number of elements that are greater than the given element. We start by initializing a counter to zero. Then, for each element in the array, we compare it with the given element. If the current element is greater, we increment the counter.
Here's the step-by-step breakdown:
- Initialize a counter
countto 0. - Iterate through the array from left to right.
- For each element
arr[i], compare it with the given elementtarget. - If
arr[i] > target, increment thecountvariable by 1. - Return the final value of
count.
Time complexity: O(n), where n is the length of the array, since we're iterating through the array once.
Space complexity: O(1), as we're not using any additional space that scales with the input size.
Optimized Solutions
Java
public int surpasserCount(int[] arr, int target) {
int count = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] > target) {
count++;
}
}
return count;
}