2678. Number of Senior Citizens
Easy
You are given a 0indexed 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.
Hint:
 Convert the value at index
11
and12
to a numerical value.  The age of the person at index i is equal to
details[i][11]*10+details[i][12]
.
Solution:
To solve this problem, we can follow these steps:
 Extract the Age: The age is located at the 12th and 13th positions in each string.
 Convert to Integer: Convert the extracted age substring to an integer.
 Count Senior Citizens: Count how many of these ages are strictly greater than 60.
Let's implement this solution in PHP: 2678. Number of Senior Citizens
<?php
// Example usage:
$details1 = ["7868190130M7522","5303914400F9211","9273338290F4010"];
$details2 = ["1313579440F2036","2921522980M5644"];
echo countSeniorCitizens($details1); // Output: 2
echo countSeniorCitizens($details2); // Output: 0
?>
Explanation:

Function Definition: The function
countSeniorCitizens
takes an array of stringsdetails
as input. 
Initialize Counter: We initialize a counter
$seniorCount
to keep track of the number of senior citizens. 
Loop Through Each Detail: We loop through each element in the
details
array. 
Extract Age: For each detail string, we extract the substring from index 11 to 12 (2 characters) and convert it to an integer using
intval
.  Check Age Condition: We check if the extracted age is strictly greater than 60.

Increment Counter: If the condition is true, we increment the
$seniorCount
.  Return Count: After looping through all the details, we return the count of senior citizens.
Testing:
 For the input
["7868190130M7522","5303914400F9211","9273338290F4010"]
, the output is2
because the ages are 75, 92, and 40, with 75 and 92 being greater than 60.  For the input
["1313579440F2036","2921522980M5644"]
, the output is0
because the ages are 20 and 56, neither of which are greater than 60.
This solution is efficient and straightforward, working within the constraints provided.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!
If you want more helpful content like this, feel free to follow me:
Top comments (0)