The Indian Engineer

Problem 2678 Number of Senior Citizens

Posted on Last edited 2 mins

String

Problem Statement

Link - Problem 2678

Question

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:

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

Solution

class Solution {
public:
    int countSeniors(vector<string>& details) {
        int count=0;
        int age;
        for(string &it:details){
            age = (it[11]-'0')*10+(it[12]-'0');
            if(age>60)
                count++;
        }
        return count;
    }
};

Complexity Analysis

Algorithm Time Complexity Space Complexity
Traversal O(n) O(1)

Explanation

1. Intuition

2. Implementation