HackerRank: Prime Dates

Fun problem from HackerRank, and this time the task is to FIX the BUGS in a code that someone else wrote - boy that really hits close to home!

https://www.hackerrank.com/challenges/prime-date/problem

The code that needs to be fixed is down below (the original code). You can only modify no more than 5 lines, so here are some hints:

a) The leap year calculation is wrong
b) The conversion from date to an integer is wrong
c) The logic to increment the date (right after the call to the LeapYear function) is wrong

Give it a try, for me it worked out! Cheers, ACC.

PS: that's in C++... I'm usually a C# programmer.


#include <bits/stdc++.h> using namespace std; int month[15]; void updateLeapYear(int year) { if(year % 400 == 0) { month[2] = 28; } else if(year % 100 == 0) { month[2] = 29; } else if(year % 4 == 0) { month[2] = 29; } else { month[2] = 28; } } void storeMonth() { month[1] = 31; month[2] = 28; month[3] = 31; month[4] = 30; month[5] = 31; month[6] = 30; month[7] = 31; month[8] = 31; month[9] = 30; month[10] = 31; month[11] = 30; month[12] = 31; } int findLuckyDates(int d1, int m1, int y1, int d2, int m2, int y2) { storeMonth(); int result = 0; while(true) { int x = d1; x = x * 100 + m1; x = x * 1000 + y1; if(x % 4 == 0 && x % 7 == 0) { result = result + 1; } if(d1 == d2 && m1 == m2 && y1 == y2) { break; } updateLeapYear(y1); d1 = d1 + 1; if(d1 > month[m1]) { m1 = m1 + 1; d1 = 1; if(m1 > 12) { y1 = y1 + 1; m1 = m1 + 1; } } } return result; } int main() { string str; int d1, m1, y1, d2, m2, y2; getline(cin, str); for(int i = 0; i < str.size(); i++) { if(str[i] == '-') { str[i] = ' '; } } stringstream ss; ss << str; ss >> d1 >> m1 >> y1 >> d2 >> m2 >> y2; int result = findLuckyDates(d1, m1, y1, d2, m2, y2); cout << result << endl; }

Comments

Popular posts from this blog

Count Binary Substrings

Count Vowels Permutation: Standard DP

Maximum Number of Balloons