Click here to Skip to main content
15,877,675 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to beat a HackerRank problem where am required to concatenate the date elements into a single integer and check whether the number formed by the concatenation is divisible by either 4 or 7. I am using a while loop to iterate through the dates between the given dates but my code is printing 8 instead of 5 to the terminal for the test case with start date as "02-08-2025" and end date"04-09-2025". Both dates are inclusive

What I have tried:

public class Solution{
static void Main(string args[]){
     //read date from the user
     var input = Console.ReadLine();
     var date1 = input.Split(new char[]{' '})[0];
     var date2 = input.Split(new char[]{' '})[1];
     //Parse date objects out of the strings
     DateTime A = DateTime.Parse(date1);
     DateTime B = DateTime.Parse(date2);
     int lucky = 0;
     while(A<= B){
           //Get a string representation of the date and    //replace the hyphens
           var str = A.ToString("dd-MM-yyyy").Replace("-","");
            var i = Int32.Parse(str);
            if(i%4==0 || i%7 ==0)
                lucky ++;
         //Update the day A
         A = A.AddDays(1);

Richard MacCutchan 26-Feb-24 12:47pm    
I just ran that code and it gave 5 as the answer.

Add some print statements so you can see the values being used as the code runs. Or just run it in the debugger.
Tim the Gamer 26-Feb-24 12:56pm    
When I ran it on HackerRank web portal, their output shows my code giving 8 as a response. Does their site have a bug?
0x01AA 26-Feb-24 13:04pm    
I would guess it is maybe something with 'dd-mm-yyyy' vs. 'mm-dd-yyyy'
I'm pretty sure your code will print 5 when you enter "08-02-2025 09-04-2025" ;)
Tim the Gamer 26-Feb-24 13:16pm    
No Hacker Rank explicitly say that the format should be dd-MM-yyyy
0x01AA 26-Feb-24 13:22pm    
I mean it will show the correct result on your system. On what OS HackerRank runs I dont' know.

To be on the safe side then use explicit format definition for DateTime.Parse(date);

Something like this:
DateTime A = DateTime.ParseExact(date1, "dd-MM-yyyy", null);

I don't know Hacker Rank, that why a way too much assumptions in my answer, sorry for that. Btw. see also the comments to the question.

Assumption: Hacker Rank runs on a US OS and therefore will take e.g. for date parsing the US conventions for date which is 'mm/dd/yyyy' and by chance this works with your test interval without exception. In case you would e.g. enter '29-12-2025 31-12-2025' your current implementation in the question will throw an exception.

Hacker Rank does explicitely request to supply the date as 'dd/mm/yyyy'

Instead of simply use DateTime.Parse which uses the OS settings you need to use a more specific method like DateTime.ParseExact

Finally using ...
DateTime A = DateTime.ParseExact(date1, "dd-MM-yyyy", null);
DateTime B = DateTime.ParseExact(date2, "dd-MM-yyyy", null);
... should solve your issue.

With DateTime.TryParseExact you can parse a Date without throwing an exception in case an invalid date has been typed in. It is then your job to handle this situation in code.

I hope it helps and sorry for my English ;)
Share this answer
Maciej Los 26-Feb-24 14:30pm    
0x01AA 26-Feb-24 14:34pm    
Thank you Maciej!
Richard MacCutchan 26-Feb-24 15:03pm    
Your English, and your explanation, are both fine.
0x01AA 26-Feb-24 15:26pm    
Thank you very much :-)

I would suggest doing something along these lines.

string[] input = "02-08-2025 04-09-2025".Split(' ');
DateTime startDate = DateTime.Parse(input[0]);
DateTime endDate = DateTime.Parse(input[1]);
int luckyNumberCount = 0;
for (DateTime date = startDate; date <= endDate; date = date.AddDays(1))
    var n = int.Parse(date.ToString("dd-MM-yyyy").Replace("-", ""));
    if (n % 4 == 0 || n % 7 == 0) luckyNumberCount++;
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900