Number of Senior Citizens - Problem

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.

Input & Output

Example 1 — Mixed Ages
$ Input: details = ["7868190130M7522","5303914400F9211","9273338640F4317"]
Output: 2
💡 Note: First passenger: age 75 > 60 (senior), second passenger: age 92 > 60 (senior), third passenger: age 43 ≤ 60 (not senior). Total seniors: 2
Example 2 — No Seniors
$ Input: details = ["1313579440F2036","2921522980M5644"]
Output: 0
💡 Note: First passenger: age 20 ≤ 60, second passenger: age 56 ≤ 60. No seniors found.
Example 3 — All Seniors
$ Input: details = ["9751302862F0693","3888560693F7262"]
Output: 1
💡 Note: First passenger: age 06 ≤ 60, second passenger: age 72 > 60. Only one senior found.

Constraints

  • 1 ≤ details.length ≤ 100
  • details[i].length == 15
  • details[i] consists of digits and an uppercase English letter
  • All the phone numbers and seat numbers in the given strings are distinct

Visualization

Tap to expand
Number of Senior Citizens INPUT details[] - Passenger Info Strings "7868190130M7522" Phone(0-9) G Age Seat "5303914400F9211" Phone(0-9) G Age Seat "9273338640F4317" Phone(0-9) G Age Seat String Format (15 chars): [0-9]: Phone | [10]: Gender [11-12]: Age | [13-14]: Seat Focus: Age at index 11-12 Check if age > 60 ALGORITHM STEPS 1 Initialize Counter count = 0 2 Loop Through Array for each string in details[] 3 Extract Age age = str[11:13] as integer 4 Check & Count if age > 60: count++ Age Extraction Process: "...M75..." --> 75 > 60 Senior "...F92..." --> 92 > 60 Senior "...F43..." --> 43 < 60 Not count = 2 FINAL RESULT Senior Citizens Found: 2 Breakdown: Passenger 1: Age 75 [OK] Passenger 2: Age 92 [OK] Passenger 3: Age 43 [X] Output: return 2 Key Insight: The optimal solution extracts age directly using substring indices [11:13] from each string. Time Complexity: O(n) where n = number of passengers. Space Complexity: O(1) - only counter needed. TutorialsPoint - Number of Senior Citizens | Optimal Solution
Asked in
Amazon 15 Microsoft 8
12.5K Views
Medium Frequency
~10 min Avg. Time
245 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen