Skip to content
thecscience
THECSICENCE

Learn everything about computer science

  • Home
  • Human values
  • NCERT Solutions
  • HackerRank solutions
    • HackerRank Algorithms problems solutions
    • HackerRank C solutions
    • HackerRank C++ solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
thecscience
THECSICENCE

Learn everything about computer science

HackerRank Day of the Programmer Solution

Yashwant Parihar, April 12, 2023April 12, 2023

In this post, We are going to solve the HackerRank Day of the Programmer Problem.

Marie invented a Time Machine and wants to test it by time-traveling to visit Russia on the Day of the Programmer (the 256th day of the year) during a year in the inclusive range from 1700 to 2700.

From 1700 to 1917, Russia’s official calendar was the Julian calendar; since 1919 they used the Gregorian calendar system. The transition from the Julian to Gregorian calendar system occurred in 1918 when the next day after January 31st was February 14th. This means that in 1918, February 14th was the 32nd day of the year in Russia.

In both calendar systems, February is the only month with a variable amount of days; it has 29 days during a leap year, and 28 days during all other years. In the Julian calendar, leap years are divisible by 4; in the Gregorian calendar, leap years are either of the following:

  • Divisible by 400.
  • Divisible by 4 and not divisible by 100.

Given a year, y, find the date of the 256th day of that year according to the official Russian calendar during that year. Then print it in the format dd.mm.yyyy, where dd is the two-digit day, mm is the two-digit month, and yyyy is y.

For example, the given year = is 1984. 1984 is divisible by 4, so it is a leap year. The 256th day of a leap year after 1918 is September 12, so the answer is 12. 09. 1984.

Function Description

Complete the dayOfProgrammer function in the editor below. It should return a string representing the date of the 256th day of the year given.

dayOfProgrammer has the following parameter(s):

  • year: an integer

Input Format

A single integer denoting year y.

Constraints

  • 1700 \le y \le 2700

Output Format

Print the full date of the Day of the Programmer during the year y in the format dd.mm.yyyy, where dd is the two-digit day, mm is the two-digit month, and yyyy is y.

Sample Input 0

2017

Sample Output 0

13.09.2017

Explanation 0

In the year y = 2017, January has 31 days, February has 28 days, March has 31 days, April has 30 days, May has 31 days, June has 30 days, July has 31 days, and August has 31 days. When we sum the total number of days in the first eight months, we get 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 = 243. The day of the Programmer is the 256th day, so then calculate 256 – 243 = 13 to determine that it falls on day 13 of the 9th month (September). We then print the full date in the specified format, which is 13.09.2017.

Sample Input 1

2016

Sample Output 1

12.09.2016

Explanation 1

Year y = 2016 is a leap year, so February has 29 days but all the other months have the same number of days as in 2017. When we sum the total number of days in the first eight months, we get 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 = 244. The day of the Programmer is the 256th day, so then calculate 256 – 244 = 12 to determine that it falls on day 12 of the 9th month (September). We then print the full date in the specified format, which is 12.09.2016.

Sample Input 2

1800

Sample Output 2

12.09.1800

Explanation 2

Since 1800 is a leap year as per the Julian calendar. The day lies on 12 September.

HackerRank Day of the Programmer Problem Solution
HackerRank Day of the Programmer Problem Solution

Day of the Programmer C Solution

#include<stdio.h>
#define DAY 256

int LEAP_A(int a)
{
	int ret=0;
	if(a%400==0)
	{
		ret=1;
	}
	else
	{
		if(a%100!=0 && a%4==0)
		{
			ret=1;
		}
	}
	return ret;
}

int LEAP_B(int a)
{
	int ret=0;
	if(a%4==0)
	{
		ret=1;
	}
	return ret;
}

int main()
{
	int arr[]={31,28,31,30,31,30,31,31,30,31,30,31};
	int year,date,month,i,j,leap=0,flag=0,temp=0;
	scanf("%d",&year);
	if(year>=1919)
	{
//		printf("ONE\n");
		leap = LEAP_A(year);
		if(leap==1)
		{
			arr[1]=29;
//			printf("%d",leap);
		}
		for(i=0;i<12 && flag==0;i++)
		{
			if(temp<DAY)
			{
				temp = temp+arr[i];
			}
			if(temp>DAY)
			{
				flag=1;
			}
		}
		if(flag==1)
		{
			temp = temp-arr[i];
//			printf("%d\n",temp);
			month=i;
			date=DAY-temp-1;
		}
	}
	else if(year<=1917)
	{
		leap = LEAP_B(year);
		if(leap==1)
		{
			arr[1]=29;
//			printf("%d",leap);
		}
		for(i=0;i<12 && flag==0;i++)
		{
			if(temp<DAY)
			{
				temp = temp+arr[i];
			}
			if(temp>DAY)
			{
				flag=1;
			}
		}
		if(flag==1)
		{
			temp = temp-arr[i];
//			printf("%d\n",temp);
			month=i;
			date=DAY-temp-1;
		}
		
	}
	else
	{
		date=26;
		month=9;
	}
	if(month<10)
	{
		printf("%d.0%d.%d\n",date,month,year);
	}
	else
	{
		printf("%d.%d.%d\n",date,month,year);
	}
	
}

Day of the Programmer C++ Solution

#include <bits/stdc++.h>

using namespace std;

void julian(int y)
{
    if(y % 4 == 0)
        cout << "12.09." << y << endl;
    else
        cout << "13.09." << y << endl;
}

void gregorian(int y)
{
    if(y % 400 == 0 || y % 4 == 0 && y % 100 != 0)
        cout << "12.09." << y << endl;
    else
        cout << "13.09." << y << endl;
}

void transition(int y)
{
    cout << "26.09." << y << endl;
}

signed main()
{
    //freopen("input.txt", "r", stdin);
    ios::sync_with_stdio(0);
    cin.tie(0);
    int y;
    cin >> y;
    if(y < 1918)
        julian(y);
    if(y > 1918)
        gregorian(y);
    if(y == 1918)
        transition(y);
}

Day of the Programmer C Sharp Solution

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {

    static string solve(int year){
        string date;
        int days = 215;
        
        if (year >= 1919) {
            if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
                days += 29;
            else
                days += 28;
        } else if (year <= 1917) {
            if (year % 4 == 0)
                days += 29;
            else
                days += 28;
        } else
            days += 15;
        
        date = string.Concat (256 - days, ".09.", year);
        return date;
    }

    static void Main(String[] args) {
        int year = Convert.ToInt32(Console.ReadLine());
        string result = solve(year);
        Console.WriteLine(result);
    }
}

Day of the Programmer Java Solution

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

class Result {

    /*
     * Complete the 'dayOfProgrammer' function below.
     *
     * The function is expected to return a STRING.
     * The function accepts INTEGER year as parameter.
     */

    public static String dayOfProgrammer(int year) {
    // Write your code here
        int feb;
        if (year < 1918) {
            if (year % 4 == 0) {
                feb = 29;
            } else {
                feb = 28;
            }
        } else {
            if (year % 400 == 0) {
                feb = 29;

            } else if (year % 4 == 0 && year % 100 != 0) {
                feb = 29;
            } else {
                feb = 28;
            }
        }
        int daysInYear;
        if (year != 1918) {
            daysInYear = 215 + feb;
        } else {
            daysInYear = 230;
        }

        int remaininDdays = 256 - daysInYear;
        System.out.println(remaininDdays + ".09." + year);
        return remaininDdays + ".09." + year;
    }

}

public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        int year = Integer.parseInt(bufferedReader.readLine().trim());

        String result = Result.dayOfProgrammer(year);

        bufferedWriter.write(result);
        bufferedWriter.newLine();

        bufferedReader.close();
        bufferedWriter.close();
    }
}

Day of the Programmer JavaScript Solution

'use strict'

process.stdin.resume();
process.stdin.setEncoding('ascii');

var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;

process.stdin.on('data', function (data) {
    input_stdin += data;
});

process.stdin.on('end', function () {
    input_stdin_array = input_stdin.split("\n");
    main();    
});

function readLine() {
    return input_stdin_array[input_currentline++];
}

/////////////// ignore above this line ////////////////////

// Divisible by 4
function isLeapYearJulian(year){
    if (year % 4 === 0) {
        return true
    } else {
        return false
    }
}

function getJulianDOH(year){
    // get Julian day of hacker
    if (isLeapYearJulian(year)){
        return '' + '12.09.' + year 
    } else {
        return '' + '13.09.' + year 
    }
}

// Divisible by 400 OR
// Divisible by 4 and not divisible by 100.
function isLeapYearGregorian(year){
    if (year % 400 === 0 || (year % 4 === 0) && (year % 100 !== 0)) {
        return true
    } else {
        return false
    }
}

function getGregorianDOH(year){
    // get gregorian day of hacker
    if (isLeapYearGregorian(year)){
        return '' + '12.09.' + year 
    } else {
        return '' + '13.09.' + year 
    }
}

function getTransitionDOH(year){
    //This means that in 1918, February 14 was the 32  
        return '' + '26.09.' + year 
}

function solve(year){
    
    // 3 cases:
    //  1700 - 1917: Julian
    //  1918 : Transition year
    //  1919 - 2700: Gregorian
    if (year >= 1700 && year <= 1917){
        return(getJulianDOH(year))
    } else if (year >= 1919 && year <= 2700){
        return(getGregorianDOH(year))
    } else if (year === 1918){
        return(getTransitionDOH(year))
    }

    

}

function main() {
    var year = parseInt(readLine());
    var result = solve(year);
    process.stdout.write(""+result+"\n");

}

Day of the Programmer Python Solution

#!/bin/python3

import sys


y = int(input().strip())
# your code goes here

if y <= 1917:
    if y%4==0:   # IS a leap year
        print("12.09." + str(y))
    else:   # IS NOT a leap year
        print("13.09." + str(y))
elif y == 1918:
    print("26.09.1918")
else:
    if y%400==0 or (y%100!=0 and y%4==0):   # IS a leap year
        print("12.09." + str(y))
    else:   # IS NOT a leap year
        print("13.09." + str(y))
    # Check if leap year

Other Solutions

  • HackerRank Bill Division Problem Solution
  • HackerRank Sales by Match Problem Solution
c C# C++ HackerRank Solutions java javascript python CcppCSharpHackerrank Solutionsjavajavascriptpython

Post navigation

Previous post
Next post

Leave a Reply

You must be logged in to post a comment.

  • HackerRank Dynamic Array Problem Solution
  • HackerRank 2D Array – DS Problem Solution
  • Hackerrank Array – DS Problem Solution
  • Von Neumann and Harvard Machine Architecture
  • Development of Computers
©2025 THECSICENCE | WordPress Theme by SuperbThemes