703451. Total count

Easy

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:

  1. Initialize a variable count to 0.
  2. Iterate through the array.
  3. For each element in the array, check if it is positive.
  4. If the element is positive, increment the count.
  5. 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

View on GeeksforGeeks
public int totalCount(int[] arr) {
    int count = 0;
    for (int num : arr) {
        if (num > 0) {
            count++;
        }
    }
    return count;
}

DevExCode

Master coding interviews with Leetcode, system design, TechBit, QuickLearn, DevTips, Tech Battles, and career services.

© 2026 DevExCode. All rights reserved.