Given two years Y1 and Y2 where 103 ≤ Y1 ≤ Y2 ≤ 106, the task is to find and print all the dates which are palindromes between the given years.
Examples:
Input: Y1 = 2001, Y2 = 2005
Output:
10022001
20022002
Input: Y1 = 5000, Y2 = 5010
Output:
10055001
20055002
30055003
01055010
Approach: Since the first four digits (i.e. the day and the month) have to be the reverse of the last four digits (the year) in order for the date to be a palindrome. So, every year will have at most one palindrome date. In order to find the dates, for every year reverse the year number and check whether the date formed is valid or not. If it is valid then print it.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; const int MAX_VALID_YR = 9999; const int MIN_VALID_YR = 1800; // Returns true if // given year is valid bool isLeap( int year) { // Return true if year // is a multiple of 4 and // not multiple of 100. // OR year is multiple of 400. return (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)); } // Returns true if given // year is valid or not. bool isValidDate( int d, int m, int y) { // If year, month and day // are not in given range if (y > MAX_VALID_YR || y < MIN_VALID_YR) return false ; if (m < 1 || m > 12) return false ; if (d < 1 || d > 31) return false ; // Handle February month // with leap year if (m == 2) { if (isLeap(y)) return (d <= 29); else return (d <= 28); } // Months of April, June, // Sept and Nov must have // number of days less than // or equal to 30. if (m == 4 || m == 6 || m == 9 || m == 11) return (d <= 30); return true ; } // Function to print the palindrome // dates between the given years void printPalindromeDates( int y1, int y2) { // For every year for ( int year = y1; year <= y2; year++) { // Current year as a string string str = to_string(year); // To store the reverse of year string rev = str; reverse(rev.begin(), rev.end()); // Get the day and the month int day = stoi(rev.substr(0, 2)); int month = stoi(rev.substr(2, 2)); // If the current palindrome date is valid if (isValidDate(day, month, year)) { cout << rev << str << endl; } } } // Driver code int main() { int y1 = 2001, y2 = 2005; printPalindromeDates(y1, y2); return 0; } |
Java
// Java implementation of the approach class GFG { static int MAX_VALID_YR = 9999 ; static int MIN_VALID_YR = 1800 ; // Returns true if // given year is valid static boolean isLeap( int year) { // Return true if year // is a multiple of 4 and // not multiple of 100. // OR year is multiple of 400. return (((year % 4 == 0 ) && (year % 100 != 0 )) || (year % 400 == 0 )); } // Returns true if given // year is valid or not. static boolean isValidDate( int d, int m, int y) { // If year, month and day // are not in given range if (y > MAX_VALID_YR || y < MIN_VALID_YR) return false ; if (m < 1 || m > 12 ) return false ; if (d < 1 || d > 31 ) return false ; // Handle February month // with leap year if (m == 2 ) { if (isLeap(y)) return (d <= 29 ); else return (d <= 28 ); } // Months of April, June, // Sept and Nov must have // number of days less than // or equal to 30. if (m == 4 || m == 6 || m == 9 || m == 11 ) return (d <= 30 ); return true ; } // Function to print the palindrome // dates between the given years static void printPalindromeDates( int y1, int y2) { // For every year for ( int year = y1; year <= y2; year++) { // Current year as a String String str = String.valueOf(year); // To store the reverse of year String rev = str; rev = reverse(rev); // Get the day and the month int day = Integer.parseInt(rev.substring( 0 , 2 )); int month = Integer.parseInt(rev.substring( 2 , 2 + 2 )); // If the current palindrome date is valid if (isValidDate(day, month, year)) { System.out.print(rev + str + "\n" ); } } } static String reverse(String input) { char [] a = input.toCharArray(); int l, r = a.length - 1 ; for (l = 0 ; l < r; l++, r--) { char temp = a[l]; a[l] = a[r]; a[r] = temp; } return String.valueOf(a); } // Driver code public static void main(String[] args) { int y1 = 2001 , y2 = 2005 ; printPalindromeDates(y1, y2); } } // This code is contributed by PrinciRaj1992 |
Python 3
# Python 3 implementation of the approach MAX_VALID_YR = 9999 MIN_VALID_YR = 1800 # Returns true if given year is valid def isLeap(year): # Return true if year # is a multiple of 4 and # not multiple of 100. # OR year is multiple of 400. return (((year % 4 = = 0 ) and (year % 100 ! = 0 )) or (year % 400 = = 0 )) # Returns true if given # year is valid or not. def isValidDate(d, m, y): # If year, month and day # are not in given range if (y > MAX_VALID_YR or y < MIN_VALID_YR): return False if (m < 1 or m > 12 ): return False if (d < 1 or d > 31 ): return False # Handle February month # with leap year if (m = = 2 ): if (isLeap(y)): return (d < = 29 ) else : return (d < = 28 ) # Months of April, June, # Sept and Nov must have # number of days less than # or equal to 30. if (m = = 4 or m = = 6 or m = = 9 or m = = 11 ): return (d < = 30 ) return True # Function to print the palindrome # dates between the given years def printPalindromeDates(y1, y2): # For every year for year in range (y1, y2 + 1 , 1 ): # Current year as a string str1 = str (year) # To store the reverse of year rev = str1 rev = rev[:: - 1 ] # Get the day and the month day = int (rev[ 0 : 2 ]) month = int (rev[ 2 : 4 ]) # If the current palindrome date is valid rev + = str1 if (isValidDate(day, month, year)): print (rev) # Driver code if __name__ = = '__main__' : y1 = 2001 y2 = 2005 printPalindromeDates(y1, y2) # This code is contributed by Surendra_Gangwar |
C#
// C# implementation of the approach using System; class GFG { static int MAX_VALID_YR = 9999; static int MIN_VALID_YR = 1800; // Returns true if // given year is valid static bool isLeap( int year) { // Return true if year // is a multiple of 4 and // not multiple of 100. // OR year is multiple of 400. return (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)); } // Returns true if given // year is valid or not. static bool isValidDate( int d, int m, int y) { // If year, month and day // are not in given range if (y > MAX_VALID_YR || y < MIN_VALID_YR) return false ; if (m < 1 || m > 12) return false ; if (d < 1 || d > 31) return false ; // Handle February month // with leap year if (m == 2) { if (isLeap(y)) return (d <= 29); else return (d <= 28); } // Months of April, June, // Sept and Nov must have // number of days less than // or equal to 30. if (m == 4 || m == 6 || m == 9 || m == 11) return (d <= 30); return true ; } // Function to print the palindrome // dates between the given years static void printPalindromeDates( int y1, int y2) { // For every year for ( int year = y1; year <= y2; year++) { // Current year as a String String str = String.Join( "" , year); // To store the reverse of year String rev = str; rev = reverse(rev); // Get the day and the month int day = Int32.Parse(rev.Substring(0, 2)); int month = Int32.Parse(rev.Substring(2, 2)); // If the current palindrome date is valid if (isValidDate(day, month, year)) { Console.Write(rev + str + "\n" ); } } } static String reverse(String input) { char [] a = input.ToCharArray(); int l, r = a.Length - 1; for (l = 0; l < r; l++, r--) { char temp = a[l]; a[l] = a[r]; a[r] = temp; } return String.Join( "" , a); } // Driver code public static void Main(String[] args) { int y1 = 2001, y2 = 2005; printPalindromeDates(y1, y2); } } // This code is contributed by 29AjayKumar |
Javascript
// JS implementation of the approach let MAX_VALID_YR = 9999; let MIN_VALID_YR = 1800; // Returns true if // given year is valid function isLeap(year) { // Return true if year // is a multiple of 4 and // not multiple of 100. // OR year is multiple of 400. return (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)); } // Returns true if given // year is valid or not. function isValidDate(d, m, y) { // If year, month and day // are not in given range if (y > MAX_VALID_YR || y < MIN_VALID_YR) return false ; if (m < 1 || m > 12) return false ; if (d < 1 || d > 31) return false ; // Handle February month // with leap year if (m == 2) { if (isLeap(y)) return (d <= 29); else return (d <= 28); } // Months of April, June, // Sept and Nov must have // number of days less than // or equal to 30. if (m == 4 || m == 6 || m == 9 || m == 11) return (d <= 30); return true ; } // Function to print the palindrome // dates between the given years function printPalindromeDates(y1, y2) { // For every year for (let year = y1; year <= y2; year++) { // Current year as a string let str = "" + year; // To store the reverse of year let rev = str.split( "" ).reverse().join( "" ); // Get the day and the month let day = parseInt(rev.substr(0, 2)); let month = parseInt(rev.substr(2, 2)); // If the current palindrome date is valid if (isValidDate(day, month, year)) { console.log(rev + str); } } } // Driver code let y1 = 2001, y2 = 2005; printPalindromeDates(y1, y2); // This code is contributed by phasing17 |
10022001 20022002
Time Complexity: O(y2 – y1)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!