मध्यम 0 हल किए गए 0% स्वीकृति

Counting Sundays

Count the months in a range of years that begin on a Sunday.

आप क्या सीखेंगे

  • Apply the Gregorian leap year rule
  • Track the weekday as days pass
  • Count events over a range of months

समस्या

अनुवाद दिखाएँ

A club meets on the first day of every month, and those meetings that land on a Sunday get a brunch. Count the months from January of start_year to December of end_year whose first day is a Sunday, using the Gregorian calendar. As a fixed point: 1 January 2023 was a Sunday. Remember the leap year rule: divisible by 4, except century years, which must be divisible by 400.

Implement counting_sundays(start_year, end_year).

Input

start_year: The first year, counted from January.

end_year: The last year, counted through December.

Output

Return the number of months in that range whose first day is a Sunday.

सीमाएँ

1600 ≤ start_year ≤ end_year ≤ 2400

उदाहरण

उदाहरण 1
इनपुट:
start_year = 2023
end_year = 2023

अपेक्षित आउटपुट:
expected = 2
इनपुट:
long long start_year = 2023;
long long end_year = 2023;

अपेक्षित आउटपुट:
long long expected = 2;
इनपुट:
long start_year = 2023;
long end_year = 2023;

अपेक्षित आउटपुट:
long expected = 2;

व्याख्या

In 2023 only January and October began on a Sunday, so the answer is 2.

उदाहरण 2
इनपुट:
start_year = 2024
end_year = 2025

अपेक्षित आउटपुट:
expected = 3
इनपुट:
long long start_year = 2024;
long long end_year = 2025;

अपेक्षित आउटपुट:
long long expected = 3;
इनपुट:
long start_year = 2024;
long end_year = 2025;

अपेक्षित आउटपुट:
long expected = 3;

व्याख्या

2024 had September and December, 2025 had June: 3 months.

कैसे हल करें

पहले खुद कोशिश करें, फिर एक-एक करके संकेत खोलें।

संकेत 1

Find the weekday of January 1 of the start year from a known date, or use a weekday formula.

संकेत 2

Moving to the next month shifts the weekday by that month's length mod 7.

संकेत 3

February has 29 days in leap years: divisible by 4, but century years only when divisible by 400.

अपेक्षित जटिलता

O(years) time, O(1) extra space

संदर्भ

यह चुनौती CodeUtility के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।

हर कोडर का सबसे अच्छा पास हुआ समाधान। तेज़, कम मेमोरी वाले और बेहतर रेटिंग वाले समाधान ऊपर रहते हैं।

अभी कोई पास हुआ समाधान नहीं है। इसे हल करें और पहला स्थान पाएँ।

दूसरों से सीखें

समुदाय समाधान

अभी तक कोई प्रकाशित समाधान नहीं। पहला साझा करने के लिए इस चुनौती को हल करें।