Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Learn everything about programming

HackerEarth Sherlock and Dates problem solution

YASH PAL, 31 July 2024
In this HackerEarth Sherlock and Dates problem solution Watson was observing in calendar dates of form DD:MM: YYYY. He considers a date to be lucky if
  1. DD + 1 = MM and
  2. MM + 1 = YYYY % 100, where a % b denotes the remainder when a is divided by b.
For example, date 02:03:2004 is lucky because 02 + 1 = 03 and 03 + 1 = 2004 % 100.
Now comes Sherlock and Watson gives him two dates, say D1 and D2, and asks him how many lucky dates lie between D1 and D2(both inclusive).
HackerEarth Sherlock and Dates problem solution

HackerEarth Sherlock and Dates problem solution.

#include<bits/stdc++.h>
#define assn(n,a,b) assert(n<=b && n>=a)
using namespace std;
#define pb push_back
#define mp make_pair
#define clr(x) x.clear()
#define sz(x) ((int)(x).size())
#define F first
#define S second
#define REP(i,a,b) for(i=a;i<b;i++)
#define rep(i,b) for(i=0;i<b;i++)
#define rep1(i,b) for(i=1;i<=b;i++)
#define pdn(n) printf("%dn",n)
#define sl(n) scanf("%lld",&n)
#define sd(n) scanf("%d",&n)
#define pn printf("n")
typedef pair<int,int> PII;
typedef vector<PII> VPII;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef long long LL;
#define MOD 1000000007
LL mpow(LL a, LL n)
{LL ret=1;LL b=a;while(n) {if(n&1)
ret=(ret*b)%MOD;b=(b*b)%MOD;n>>=1;}
return (LL)ret;}
int cnt(int d, int m, int y){
if(y==0)return 0;
int x=y/100;
int ret=max(x,0)*11;
ret += max(0,min(13,(y%100)-1)-2);
if(m>y%100-1 and y%100>=3)ret++;
else if(m==y%100-1 and d>=y%100-2 and y%100>2)ret++;
return ret;
}
int ar[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
bool leap(int y){
if(y%4==0){
if(y%100==0){
if(y%400==0)return true;
return false;
}
return false;
}
return false;
}
int main()
{
int t;
sd(t);
getchar();
while(t--){
int d1,m1,y1,d2,m2,y2,ans=0;
scanf("%d:%d:%d %d:%d:%d",&d1,&m1,&y1,&d2,&m2,&y2);
if(d1==1){
if(m1==1){
d1=31;
m1=12;
y1--;
}
else{
m1--;
if(m1==2){
if(leap(y1))d1=29;
else d1=28;
}
else d1=ar[m1];
}
}
else d1--;
cout << cnt(d2,m2,y2) - cnt(d1,m1,y1) << endl;
}
return 0;
}

Second solution

#include <cstdio>
#include <cmath>
#include <iostream>
#include <set>
#include <algorithm>
#include <vector>
#include <map>
#include <cassert>
#include <string>
#include <cstring>
#include <queue>

using namespace std;

#define rep(i,a,b) for(int i = a; i < b; i++)
#define S(x) scanf("%d",&x)
#define S2(x,y) scanf("%d%d",&x,&y)
#define P(x) printf("%dn",x)
#define all(v) v.begin(),v.end()
#define sz size()

typedef long long int LL;
typedef pair<int, int > pii;
typedef vector<int > vi;

int X[4000000];
int days[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
int tot[13];

void pre() {

rep(i,1,13) {
tot[i] += tot[i-1] + days[i];
}

rep(y,1,10000) rep(d,1,12) {
int m = d + 1;

if(m + 1 == (y % 100)) {
int dayNumber = (y - 1) * 365 + tot[m-1] + d;
X[dayNumber] = 1;
}

}

rep(i,2,4000000) X[i] += X[i-1];

}

int main() {
int t;
S(t);
pre();
while(t--) {
int d,m,y;
scanf("%d:%d:%d",&d,&m,&y);
int a = (y - 1) * 365 + tot[m-1] + d;
scanf("%d:%d:%d",&d,&m,&y);
int b = (y - 1) * 365 + tot[m-1] + d;
printf("%dn",X[b] - X[a-1]);
}
return 0;
}
coding problems solutions

Post navigation

Previous post
Next post

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes