703451. Total count
Total Count
Slug: total-count2415 Difficulty: Easy Id: 703451 Topic Tags: Arrays, Data Structures, Division, Mathematical Company Tags: Zoho
Summary
The problem is about finding the total count of elements in an array. The given array contains only positive integers and one negative integer. We need to find the total count of all the numbers in the array except the negative integer.
Detailed Explanation
To solve this problem, we can iterate through the array and count the number of positive integers. We will ignore the negative integer and add up the remaining positive integers.
Here's a step-by-step breakdown of the solution:
- Initialize a variable
countto 0. - Iterate through the array.
- For each element in the array, check if it is positive.
- If the element is positive, increment the
count. - After iterating through the entire array, return the
count.
Here's an ASCII art diagram representing the array:
[1, 2, -3, 4, 5]
| |
positive integers
------------------
^^^^
|
count = 5 (because there are 5 positive integers)
Time complexity: O(n), where n is the size of the input array.
Space complexity: O(1), because we only use a constant amount of space to store the count.
Optimized Solutions
Java
public int totalCount(int[] arr) {
int count = 0;
for (int num : arr) {
if (num > 0) {
count++;
}
}
return count;
}