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!!

ehm ... I'm not really sure what your question is.
is that your working code you need/want to improve? do you need to write additional methods/functionalities? ... ?

commented: actually, this my code is taking too much time at execution you can try so I wantto make a logic which can execute faster than it. +0

For the particular years you provided, this holds:

    public int[] getYears(){
        int numbers[] = new int[3], dif=0;
        for (int i=0;i<years.length;i++) if (years[i]<0) years[i]*=-1; //normalising the array to positive integers
        for (int i=0;i<years.length;i+=2){
            if (years[i]-years[i+1]>dif){
                dif = years[i]-years[i+1];
                numbers[0]=years[i];
                numbers[1]=years[i+1];
                numbers[2]=dif;
            }
        }
        return numbers;
    }

now try to apply this to your situation.

@Lucaci: I think you haven't read my question throughly, so please read it again, and give me a solution which is compatible to my problem. as there is no need to calculate the age of each dinosaur.
but Thanks for your response.

So, you want to know the maximum number of overleaps?, e.g.:
20,70,22,60,58,60,58,65
Four dinosaurs lived at the same time...

@Lucaci: exactly,I want this.

I would sort the events by date and then iterate through the list adding one to the population for each birth and subtracting one for each death.

Here, this is for making up for my previous erroneous post.

    public boolean inRange(int min, int max, int nr){
        if (min<=nr && nr<=max) return true;
        return false;
    }

    public int ovr(){
        int years[]={-80000,-79960,20,70,22,60,58,60,58,65,1950,2005}, overl = 0;
        for (int i=0;i<years.length;i+=2){
            int min = years[i], max = years[i+1], ovre = 0;
            for (int j=0;j<years.length;j+=2){
                if (inRange(min, max, years[j]) && inRange(min, max, years[j+1])) ovre++; 
            }
            if (ovre>overl) overl=ovre;
        }
        return overl;
    }

public int findMaxdinosaur (int[] years) {

    int[] birthDino = new int[years.length];
    int[] deathDino=new int[years.length];

      //Now  birth & death list is made in which death contains even place.

       for(int i=0;i<=years.length-2;i=i+2){  
           birthDino[i]=years[i];
           deathDino[i+1]=years[i+1];
          }
     Arrays.sort(birthDino);
     Arrays.sort(deathDino);
   List<Integer> al=new ArrayList<Integer>();//List is need because i need to remove null values from here
       for(int aa: birthDino){
            al.add(aa);
       }
      al.removeAll(Collections.singleton(0));// Removing all positions which contain null values
    // System.out.println(al);
     List<Integer> all=new ArrayList<Integer>();
       for(int aa: deathDino){
         all.add(aa);                             //adding in to the List
        }
     all.removeAll(Collections.singleton(0));

 int  Alive=0,maxDinos=0,LastBornMax=0;
 int b = 0,d=0;
 // S N Prasad Rao
    while( b!=birthDino.length && d!=deathDino.length) {
            if (birthDino[b] < deathDino[d])  {
                  Alive =  Alive + 1;
                  b = b + 1;
                } 
                 else {
                         Alive =  Alive - 1;
                        d = d + 1  ;    
                       }

            if(maxDinos <  Alive){  //checking the values
                maxDinos =  Alive;
                LastBornMax=birthDino[b-1];
                }
       }//while end


      return maxDinos;
}//end of method
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.