Number of Senior Citizens

Easy
#2427Time: O(N), where N is the number of passengers (the length of the `details` array). We perform a single pass through the array. The operations inside the loop (`substring`, `parseInt`) are constant time because the input string format is fixed.Space: O(1). The extra space used is constant, regardless of the input size. We only need a counter and a temporary string variable within the loop's scope.
Data structures

Prompt

You are given a 0-indexed array of strings details. Each element of details provides information about a given passenger compressed into a string of length 15. The system is such that:

  • The first ten characters consist of the phone number of passengers.
  • The next character denotes the gender of the person.
  • The following two characters are used to indicate the age of the person.
  • The last two characters determine the seat allotted to that person.

Return the number of passengers who are strictly more than 60 years old.

 

Example 1:

Input: details = ["7868190130M7522","5303914400F9211","9273338290F4010"]
Output: 2
Explanation: The passengers at indices 0, 1, and 2 have ages 75, 92, and 40. Thus, there are 2 people who are over 60 years old.

Example 2:

Input: details = ["1313579440F2036","2921522980M5644"]
Output: 0
Explanation: None of the passengers are older than 60.

 

Constraints:

  • 1 <= details.length <= 100
  • details[i].length == 15
  • details[i] consists of digits from '0' to '9'.
  • details[i][10] is either 'M' or 'F' or 'O'.
  • The phone numbers and seat numbers of the passengers are distinct.

Approaches

2 approaches with complexity analysis and trade-offs.

This straightforward approach iterates through each passenger's detail string. For each string, it extracts the age portion using the substring method. This extracted string is then converted to an integer using Integer.parseInt(). Finally, it checks if the age is greater than 60 and increments a counter if it is.

Algorithm

  • Initialize a counter seniorCount to 0.
  • For each detail string in the details array:
    • Extract the age string using detail.substring(11, 13).
    • Convert the age string to an integer age.
    • If age > 60, increment seniorCount.
  • Return seniorCount.

Walkthrough

The algorithm is simple:

  1. Initialize a counter for senior citizens, seniorCount, to 0.
  2. Loop through every detail string in the input array details.
  3. Inside the loop, for the current detail, isolate the age information. The age is a two-digit number located at indices 11 and 12. We can extract this using detail.substring(11, 13).
  4. The result of the substring operation is a String (e.g., "75"). This needs to be converted to a number for comparison. The Integer.parseInt() method is used for this conversion.
  5. An if condition checks if the parsed age is strictly greater than 60.
  6. If the condition is true, seniorCount is incremented.
  7. After the loop has processed all the strings, the final seniorCount is returned.
class Solution {    public int countSeniors(String[] details) {        int seniorCount = 0;        for (String detail : details) {            // Extract the age substring from index 11 up to (but not including) 13.            String ageString = detail.substring(11, 13);            // Convert the age string to an integer.            int age = Integer.parseInt(ageString);            // If the passenger is older than 60, increment the count.            if (age > 60) {                seniorCount++;            }        }        return seniorCount;    }}

Complexity

Time

O(N), where N is the number of passengers (the length of the `details` array). We perform a single pass through the array. The operations inside the loop (`substring`, `parseInt`) are constant time because the input string format is fixed.

Space

O(1). The extra space used is constant, regardless of the input size. We only need a counter and a temporary string variable within the loop's scope.

Trade-offs

Pros

  • Highly readable and easy to understand.

  • Directly translates the problem statement into code.

Cons

  • Incurs a small performance overhead from creating a new substring object and calling the Integer.parseInt() method for each passenger. While negligible for the given constraints, it's technically less efficient than direct character manipulation.

Solutions

class Solution {public  int countSeniors(String[] details) {    int ans = 0;    for (var x : details) {      int age = Integer.parseInt(x.substring(11, 13));      if (age > 60) {        ++ans;      }    }    return ans;  }}

Video walkthrough

Newsletter

One sharp idea, every week

System design and interview prep — short enough to finish.

No spam. Unsubscribe anytime.

Practice

Same difficulty — related problems to reinforce the pattern.