Age Calculator | codehef solution

Chef's planet is called Javad. A year on Javad has � months numbered 1 through �. For each valid �, the �-th month has �� days numbered 1 through ��.

On Javad, years that are divisible by 4 are leap years - the last month of each leap year is one day longer (it has ��+1 days).

You are given Chef's birth date and the current date. Find the age of Chef in days, i.e. the number of days between Chef's birth date and the current date inclusive.

Input

  • The first line of the input contains a single integer � denoting the number of test cases. The description of � test cases follows.
  • The first line of each test case contains a single integer �.
  • The second line contains � space-separated integers �1,�2,…,��.
  • The third line contains three space-separated positive integers ��, �� and �� denoting the year, month and day of Chef's birth respectively.
  • The fourth line contains three space-separated positive integers ��, �� and �� denoting the current year, month and day respectively.

Output

For each test case, print a single line containing one integer — Chef's age in days.

Constraints

  • 1≤�≤100
  • 1≤�≤10,000
  • 1≤��≤10,000 for each valid �
  • 1≤��,��≤100,000
  • 1≤��,��≤�
  • �� will be less than or equal to the days of the �� month of the �� year.
  • �� will be less than or equal to the days of the �� month of the �� year.
  • the current date is equal to or greater than Chef's birth date

Subtasks

Subtask #1 (10 points):

  • 1≤��,��,�≤1,000
  • 1≤��≤100 for each valid �

Subtask #2 (90 points): original constraints

Sample 1:

Input
Output
4
5
1 2 3 4 5
2 1 1
3 5 1
2
3 3
1 1 1
2 2 2
2
1 1
3 1 1
3 2 1
5
1 4 3 4 6
3 5 6
10 1 1
26
11
2
112
Did you like the problem statement?

 Code(C++):-

#include <bits/stdc++.h>

using namespace std;


int main()
{
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
long long int a[n];
for (int i = 0; i < n; i++)
cin >> a[i];
long long int yb, mb, db, yc, mc, dc, s1 = 0, s2 = 0, s = 0;
cin >> yb >> mb >> db >> yc >> mc >> dc;
for (int i = 0; i < n; i++)
{
if (i < (mc - 1))
s2 += a[i];
if (i > (mb - 1))
s1 += a[i];
s += a[i];
}
long long int ans = (a[mb - 1] - db + 1) + s1 + (yb % 4 == 0 ? 1 : 0) + (yc - yb - 1) * s + ((yc / 4) - (yb / 4)) - (yc % 4 == 0 ? 1 : 0) + s2 + dc;
cout << ans << "\n";
}
return 0;
}

Code(JAVA):-

import java.util.*;
class AgeC
{

public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0)
{
int n=sc.nextInt();
long a[]=new long[n+1];
a[0]=0;
long sum=0;
HashMap<Integer,Long> hm=new HashMap<Integer,Long>();
hm.put(0,0l);
for(int i=1;i<=n;i++)
{
a[i]=sc.nextLong();
sum=sum+a[i];
hm.put(i,sum);
}

int yb=sc.nextInt();
int mb=sc.nextInt();
int db=sc.nextInt();

int yc=sc.nextInt();
int mc=sc.nextInt();
int dc=sc.nextInt();
long ans=0;

if(yb==yc)
{
long var1= hm.get(mc-1);
var1=var1+dc;

long var2= hm.get(mb-1);
var2=var2+db;
ans=var1-var2;  
    
}else{

for(int i=yb;i<=yc;i++)
{
if(i==yb)
{
long var= hm.get(mb-1);

var=var+db;
if(yb%4==0)
ans=ans+sum+1-var;
else
ans=ans+sum-var;
}else if(i==yc)
{
long var= hm.get(mc-1);
var=var+dc;
ans=ans+var;
}else{
if(i%4==0)
ans=ans+sum+1;
else
ans=ans+sum;
}
}
}
System.out.println(ans+1);
}

}


}

Recommended Post :-

HCL Coding Questions:-

Capgemini Coding Questions:-

Companies interview:-

Full C course:-    

Key points:-

Cracking the coding interview:-

 Array and string:-

Tree and graph:-

Hackerearth Problems:-

Hackerrank Problems:-

Data structure:-

 MCQs:-