Header Ad

HackerEarth An Easy Problem solution

In this HackerEarth An Easy problem solution Mr X loves his clock. His clock displays time in the HH : MM Format using the 24 hour system, the first 2 characters in the string are used to display the current hour, with possible leading zeros. Also, the last 2 characters are used to display the current minutes, also with possible leading zeroes.

Now, Mr X came up with a new thing, where he calls a particular time displayed by the clock good if the sum of all digits written on the clock during that time is divisible by a given number x.

You have been given the current time displayed by the clock and an integer x. You need to find the minimum number of minutes Mr X needs to wait, to see a good time being displayed by the clock. If the clock is already displaying a good time, Mr X does not need to wait at all.

If the clock will never ever display a good time, then print -1 instead as the answer.


HackerEarth An Easy Problem solution


HackerEarth An Easy Problem solution.

#include<bits/stdc++.h>

using namespace std;

typedef complex<double> base;
typedef long double ld;
typedef long long ll;

#define pb push_back
#define pii pair<int,int>
#define pll pair< ll , ll >
#define vi vector<int>
#define vvi vector< vi >

const int maxn=(int)(1e5+5);
const ll mod=(ll)(1e9+7);

bool check(int h,int m,int x)
{
string s1=to_string(h),s2=to_string(m);

int ret=0;

for(char ch:s1)
{
ret+=(ch-'0');
}

for(char ch:s2)
{
ret+=(ch-'0');
}

return (ret%x==0);
}

int main()
{
string s;cin>>s;

int x;cin>>x;

int h=((s[0]-'0')*10)+((s[1]-'0'));

int m=((s[3]-'0')*10)+(s[4]-'0');

int ctr=0,ans=-1;

while(ctr<=1440)
{
if(check(h,m,x))
{
ans=ctr;break;
}

m++;

if(m==60)
{
h=(h+1)%24;m=0;
}

ctr++;
}

cout<<ans<<endl;

return 0;
}

Second solution

#include <bits/stdc++.h>
using namespace std;

const int maxt = 24 * 60;
int x;
int main(){
auto getTime = [](){
int h, m, ret = 0;
scanf("%d:%d", &h, &m);
return m + h * 60;
};
int l = getTime();
scanf("%d", &x);
for(int i = l; i < maxt; i++){
string s;
int y = i;
for(int j = 0; j < 2; j++, y /= 60)
s.push_back('0' + y % 60 % 10), s.push_back('0' + y % 60 / 10);
if((accumulate(s.begin(), s.end(), 0) - '0' * 4) % x == 0)
return cout << i - l << '\n', 0;
}
cout << "-1\n";
}

Post a Comment

0 Comments