समस्या
Return the number of days in month of year in the Gregorian calendar. February has 29 days in a leap year: years divisible by 4, except those divisible by 100 unless also divisible by 400. A helper function is_leap(year) keeps the code tidy.
Implement days_in_month(year, month).
Input
year: The year. month: The month, 1 (January) to 12 (December).
Output
Return the number of days.
सीमाएँ
1 ≤ year ≤ 9,999; 1 ≤ month ≤ 12
उदाहरण
इनपुट:
year = 2024
month = 2
अपेक्षित आउटपुट:
expected = 29
इनपुट:
long long year = 2024;
long long month = 2;
अपेक्षित आउटपुट:
long long expected = 29;
इनपुट:
long year = 2024;
long month = 2;
अपेक्षित आउटपुट:
long expected = 29;
व्याख्या
2024 is divisible by 4 and not by 100, so it is a leap year: 29 days.
इनपुट:
year = 1900
month = 2
अपेक्षित आउटपुट:
expected = 28
इनपुट:
long long year = 1900;
long long month = 2;
अपेक्षित आउटपुट:
long long expected = 28;
इनपुट:
long year = 1900;
long month = 2;
अपेक्षित आउटपुट:
long expected = 28;
व्याख्या
1900 is divisible by 100 but not 400, so it is not a leap year: 28 days.
कैसे हल करें
पहले खुद कोशिश करें, फिर एक-एक करके संकेत खोलें।
संकेत 1
ज़्यादातर months में day count fixed होता है; February leap year पर निर्भर करता है।
संकेत 2
Month lengths के लिए छोटा lookup use करो, फिर leap-year rule से February adjust करो।
संकेत 3
Century years में सावधान रहो: सिर्फ 100 से divisible होना काफी नहीं, 400 से भी divisible होना चाहिए।
अपेक्षित जटिलता
समय O(1), अतिरिक्त स्थान O(1)
संदर्भ
यह चुनौती CodeUtility के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।