1

this is the code I have written in Java, itr is the iterator for the key, I am facing a problem in iterating the key, the question for the code is :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 following methods:

C/C++: int find_max_dinosaur (int[] years);

Java: public int findMaxdinosaur (int[] years);

package usingarray;


int dinoStrength;
int deathOfDino;
int dinoBirthAge;
int currentDinoBirthAge;
int currentDinoDeathAge;
int count;

TreeMap dino=new TreeMap();
List<Integer> dinos = new ArrayList<Integer>();

Scanner sc=new Scanner(System.in);


public void getDionoAges()
{
    System.out.println("How many Dinosaur");
    dinoStrength=sc.nextInt()+1;
    for(int i=1;i<dinoStrength;i++)
    {
        System.out.println("Please enter "+i+" dino birth age and death age:");
        dino.put(sc.nextInt(),sc.nextInt());
    }
}

public void logic()
{
    Collection value = dino.values();
    Collection key = dino.values();
    Iterator it = value.iterator();
    Iterator itr = key.iterator();

    System.out.println("logic");

    for(int x=0;x<dinoStrength;x++)
    {
        System.out.println("in for");
    while(it.hasNext() && itr.hasNext())
        {
            System.out.println("in while");
            deathOfDino=(int) it.next();
            //currentDinoDeathAge=(int) it.next();
            dinoBirthAge=(int) itr.next();

            while(itr.hasNext())
            {
                System.out.println("In itr while");
                currentDinoBirthAge=(int) itr.next();
                if(dinoBirthAge<=currentDinoBirthAge && currentDinoBirthAge<=deathOfDino)
                {
                    count++;
                    System.out.println(count);
                }
            }
            dinos.add(count);
        }
    }
}

public void display()
{
    Iterator<Integer> it = dinos.iterator();
    while(it.hasNext())
    {
        System.out.println(it.next());
    }
}
public static void main(String[] args) {
    VickyDino vd=new VickyDino();
    vd.getDionoAges();
    vd.logic();
    vd.display();
}

}

Cœur
  • 37,241
  • 25
  • 195
  • 267
Sunny
  • 2,074
  • 4
  • 24
  • 33
  • I'll answer in a bit, but can I just say for now: loving the dinos. – G. Bach Feb 03 '13 at 16:43
  • Could you try iterating like this (http://stackoverflow.com/questions/46898/how-do-i-iterate-over-each-entry-in-a-map)? I think iterating by value and key at the same time is not appropriate. – bhdrkn Feb 03 '13 at 17:50
  • Also, This two collection(values and keys) does not have to be in same order. Keys will be in natural order. – bhdrkn Feb 03 '13 at 17:59

2 Answers2

1

I would assume your problem is this:

Collection value = dino.values();
Collection key = dino.values();

You're using dino.values() twice; the second should probably be dino.keys(). However, since you're putting the birth year into the keys and the year of death into the values, you probably may wanna switch the two. Note: I didn't check whether the logic in your loops is correct.

G. Bach
  • 3,869
  • 2
  • 25
  • 46
0

/* 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 ... death of a dinosaurs respectively. The range of valid years we want to consider is [-100000 to 2005].

*/

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;

    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

    //S N Prasad Rao.
      return maxDinos;
}//end of method