Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9225167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:30:40+00:00 2026-06-18T04:30:40+00:00

Question: We are given an array of 2n integers wherein each pair in this

  • 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 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.

Can anyone suggest the way to find out solution?

Edit tried with this->
rough code

#include<stdio.h>
#include<stdlib.h>
#include <stddef.h>
static void insertion_sort(int *a, const size_t n) {
    size_t i, j;
    int value;
    for (i = 1; i < n; i++) {
        value = a[i];
        for (j = i; j > 0 && value < a[j - 1]; j--) {
            a[j] = a[j - 1];
        }
        a[j] = value;
    }
}


int  main(){
    int arr[10]={-80000,-79950,20,70,22,60,58,65,1950,2004};
    int strt[5],end[5];
    int bal[5];
     int i,j,k,l,m,length;
     l=0;
     m=0;
   for (i=0; i<10; i++){
       //printf("i = %2d arr[i] = %2d\n", i, arr[i]);
       if(i%2==0){
       strt[l]=arr[i];
       l++;
       }else{
       end[m]=arr[i];
       m++;
       }
   }
   insertion_sort(strt, sizeof strt / sizeof strt[0]);
   insertion_sort(end, sizeof end / sizeof end[0]);
   k=sizeof(end)/sizeof(int);
   for(j=0;j<5;j++){
       bal[i]=strt[i]-end[k-1];
       k--;

   }
   length=sizeof bal / sizeof bal[0];
   insertion_sort(bal, sizeof bal / sizeof bal[0]);
   printf("answer is = %2d",bal[length-1]);

}

But output is not as expected.
Please tell me what I miss.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T04:30:42+00:00Added an answer on June 18, 2026 at 4:30 am

    Consider each dinosaur birth as an open parenthesis and death as a close one. Then sort the parenthesis by date – this will give you chronological order of every event of birth and death. After that pass over the parenthesis sequence and compute the balance (increment on ‘(‘ and decrement on ‘)’ ). Track the maximal balance and it will be the answer.

    Update:

    Sample source in C++.
    Input: number of dinosaurs n, then 2n dates of birth and death.
    Output: number of maximal quantity of dinos living at the same time

    #include <algorithm>
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    int main()
    {
        int n;
        cin >> n;
        vector<pair<int, int> > dinos(2*n); // <date, died/born>
        int i;
        for( i = 0; i < n; ++i )
        {
            cin >> dinos[ 2 * i ].first >> dinos[ 2 * i + 1 ].first;
            dinos[ 2 * i ].second = 1; // born
            dinos[ 2 * i + 1 ].second = 0; // died
        }
        sort( dinos.begin(), dinos.end()); // sorts by date first
        int ans = 0, balance = 0;
        for( i = 0; i < dinos.size(); ++i )
            if( dinos[ i ].second ) // someone's born
            {
                ++balance;
                ans = max( ans, balance ); // check for max
            }
            else
                --balance;
        cout << ans << endl;
        return 0;
    }
    

    P.S. I assumed that if two dinos born and died at the same time then they didn’t live together. If you want the opposite, just change the values as born=0, died=1.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had this question: Given an unsorted array of positive integers and an integer
This is a interview question: given an array of integers find the max. and
I was asked this question: You are given two lists of integers, each of
This is an interview question I faced recently. Given an array of 1 and
Given the following question, Given an array of integers A of length n, find
The original problem statement is this one: Given an array of 32bit unsigned integers
Question: Given an unsorted array of positive integers, is it possible to find a
You are given an array of integers. You have to output the largest range
QUESTION: Given a cell index (red) compute the array index (black) that surround the
I have a homework question that says: Problem 1: Given the array [ 22

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.