How to Print the Next N Leap Years in Java
The Leap year is an interval of four years. The consecutive years maintain consistency as a leap year is crucial for reliable calendar functionality. Leap year is divisible by 4, but not divisible by 100 unless also divisible by 400.
Example 1
Input: Year = 2024, N = 5
Output: 2024
2028 2032 2036 2040
Example 2
Input: Year = 2021, N = 6
Output: 2024
2028 2032 2036 2040 2044
Example 3
Input: Year = 2022, N = 2
Output: 2024
2028
Approach 1: Using Iteration Approach
The iterative approach is simple and efficient. This approach iteratively checks each year from the starting year. This approach is to generate the next N leap years starting from a given year.
Algorithm
Step 1: Initialize the starting year and the number of leap years to print.
Step 2: Initialize a counter to keep track of leap years printed.
Step 3: Iterate through the years starting from the given starting year.
Step 4: For each year, check if it is a leap year using the isLeapYear() method.
Step 5: If it is a leap year, print the year and increment the leap year count.
Step 6: Repeat the steps until the desired number of leap years is printed.
Filename: NextLeapYearsUisngIteration.java
import java.util.*; public class NextLeapYearsUisngIteration { public static void main(String[] args) { // Initialize starting year and number of leap years to print int startYear = 2021; int n = 6; // Initialize counter for leap years printed int count = 0; // Iterate through years starting from the given year for (int year = startYear; count < n; year++) { // Check if the current year is a leap year if (isLeapYear(year)) { // Print the leap year System.out.println(year); // Increment leap year count count++; } } } // Method to check if a year is a leap year public static boolean isLeapYear(int year) { // Leap year condition: divisible by 4 and not divisible by 100 unless also divisible by 400 return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); } }
Output
2024 2028 2032 2036 2040 2044
Time Complexity: The time complexity of an algorithm is O(n), where n is the number of leap years. This is because the algorithm iterates through a fixed number of years (N) to find the next N leap years.
Space Complexity: The space complexity of an algorithm is O(1). This is because the algorithm uses a fixed amount of extra space for variables, independent of the input size.
Approach 2: Using Recursion
The alternative approach to generate the next N leap years is using recursion. This approach recursively checks each year and invokes the method to print the next leap year.
Algorithm
Step 1: Initialize the starting year and the number of leap years to print.
Step 2: Call the recursive function printNextNLeapYears() with the starting year and the desired count of leap years.
Step 3: If the desired count of leap years is zero or less, return.
Step 4: Check if the current year is a leap year using the isLeapYear() method.
Step 4.1: If it is a leap year, print the year and recursively call the function with the next year and decremented count.
Step 4.2: If it is not a leap year, recursively call the function with the next year and the same count.
Step 5: Repeat the steps until the desired count of leap years is printed.
Filename: NextLeapYearsUsingRecursion.java
public class NextLeapYearsUsingRecursion { public static void main(String[] args) { // Initialize starting year and number of leap years to print int startYear = 2021; int n = 6; // Call the recursive function to print the next N leap years printNextNLeapYears(startYear, n); } // Recursive method to print the next N leap years public static void printNextNLeapYears(int year, int n) { // Base case: stop recursion when N leap years are printed if (n <= 0) { return; } // Check if the current year is a leap year if (isLeapYear(year)) { // Print the leap year System.out.println(year); // Recursive call to print the next leap year printNextNLeapYears(year + 1, n - 1); } else { // If the current year is not a leap year, check the next year printNextNLeapYears(year + 1, n); } } // Method to check if a year is a leap year public static boolean isLeapYear(int year) { // Leap year condition: divisible by 4 and not divisible by 100 unless also divisible by 400 return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); } }
Output
2024 2028 2032 2036 2040 2044
Time Complexity: The time complexity of an algorithm is O(n), where n is the number of leap years. This is because the algorithm may iterate through up to N years to find the next N leap years.
Space Complexity: The space complexity of an algorithm is O(n). This is because the space complexity is linear due to the recursion stack, which may grow up to N frames deep in the worst case.
Approach 3: Using ArrayList
The approach is used to print the next N leap years starting from a given year. This algorithm iteratively checks each year and adds leap years to an ArrayList until the desired count is reached. Finally, we print the leap years stored in the ArrayList.
Algorithm
Step 1: Initialize the starting year and the number of leap years to print.
Step 2: Create an ArrayList to store leap years.
Step 3: Iterate through years starting from the given year until the ArrayList size reaches N.
Step 4: For each year, check if it is a leap year using the isLeapYear() method.
Step 5: If it is a leap year, add it to the ArrayList.
Step 6: Print the leap years stored in the ArrayList.
Filename: NextLeapYearsUsingArrayList.java
import java.util.ArrayList; public class NextLeapYearsUsingArrayList { public static void main(String[] args) { // Initialize starting year and number of leap years to print int startYear = 2021; int n = 6; // ArrayList to store leap years ArrayListleapYears = new ArrayList<>(); // Iterate through years starting from the given year int year = startYear; while (leapYears.size() < n) { // Check if the current year is a leap year if (isLeapYear(year)) { // Add leap year to the ArrayList leapYears.add(year); } // Move to the next year year++; } // Print the leap years for (int leapYear : leapYears) { System.out.println(leapYear); } } // Method to check if a year is a leap year public static boolean isLeapYear(int year) { // Leap year condition: divisible by 4 and not divisible by 100 unless also divisible by 400 return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); } }
Output
2024 2028 2032 2036 2040 2044
Time Complexity: The time complexity of an algorithm is O(n), where n is the number of leap years. This is because the algorithm iterates through a fixed number of years (N) to find the next N leap years.
Space Complexity: The space complexity of an algorithm is O(n). This is because the space complexity is linear; we use an ArrayList to store the leap years, which may grow up to N elements in the worst case.
Approach 4: Using Java 8 Stream API
Java Streams to generate the next N leap years starting from a given year. By Stream API, we can filter and limit the stream of years to find the desired leap years efficiently.
Algorithm
Step 1: Initialize the starting year and the number of leap years to print.
Step 2: Generate a stream of years starting from the given year using IntStream.iterate().
Step 3: Apply a filter operation to keep only the leap years using the isLeapYear() method.
Step 4: Limit the stream to the next N leap years using the limit() operation.
Step 5: Box the int values to Integer objects using the boxed() operation.
Step 6: Collect the leap years into a List
Step 7: Print the leap years stored in the list.
Filename: NextLeapYearsUsingStreams.java
import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; public class NextLeapYearsUsingStreams { public static void main(String[] args) { // Initialize starting year and number of leap years to print int startYear = 2021; int n = 6; // Generate a stream of years starting from the given year ListleapYears = IntStream.iterate(startYear, year -> year + 1) .filter(NextLeapYearsUsingStreams::isLeapYear) .limit(n) .boxed() .collect(Collectors.toList()); // Print the leap years leapYears.forEach(System.out::println); } // Method to check if a year is a leap year public static boolean isLeapYear(int year) { // Leap year condition: divisible by 4 and not divisible by 100 unless also divisible by 400 return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); } }
Output
2024 2028 2032 2036 2040 2044
Time Complexity: The time complexity of an algorithm is O(n), where n is the number of leap years. This is because the algorithm iterates through a fixed number of years (N) to find the next N leap years.
Space Complexity: The space complexity of an algorithm is O(n). This is because the space complexity is linear; we use a List to store the leap years, which may grow up to N elements in the worst case.