class Temp
{
static int []years={-80000,-79950,20,70,22,60,58,60,58,65,1950,2005};
public static int max_year(int x,int y)
{
int i=0;
int j=0;
int m=0;
int n=0;
int z=0;
while(x<y)
{
z=(x+y)/2;
for(i=0;i<=11;i=i+2)
{
if((years[i]<z) && (years[i+1]>x))
{
m=m+1;
}
if((years[i]<y) && (years[i+1]>z))
{
n=n+1;
}
}
if(m>n)
{
y=z;
}
else
{
x=z;
}
}
return z;
}
public static void main(String...s)
{
int k=0;
int t;
int d=0;
t=max_year(-100000,2005);
for(k=0;k<11;k=k+2)
{
if((years[k]<t) && (years[k+1]>t))
{
d=d+1;
}
}
System.out.println(d);
}
}
the above code is for the following problem:
Question: We are given an array of 2n integers wherein each pair in this array of integers represents the year of birth and the year of death of a dinosaurs respectively. The range of valid years we want to consider is [-100000 to 2005]. For example, if the input was:
-80000 -79950 20 70 22 60 58 65 1950 2004
it would mean that the first dinosaur had a birth year of –80000 and an year of death of –79950 respectively. Similarly the second dinosaur lived from 20 to 70 and so on and so forth.
We would like to know the largest number of dinosaurs that were ever alive at one time. Write a method to compute this, given the above array of 2n integers.
Write one of the methods.
Please guide me if I can optimize my code for this problem.
Thanks!!