I need help on my homework, I got this question and don't know how to find the answer:
Consider a group of retired people, where any person start to work in a year X and got retired in a year Y. Create a function that recieves an a array following the structure: [[x1,y1],[x2,y2],...[xn,yn]]
and calculate which year(s) has most people working.
Consider:
- The X values as the year that the person starts working
(x>0, x<y)
; - The Y values as the year that the person gets retired
(y>0)
; - The beginning year must be considered as a worked year;
- The retired year must be considered as a worked year.
Example: Input [[1960,2005],[1945,2008],[1938,1999],...]
I tried to get the lowest and highest years to compare and goes year by year counting how many people was working but it didn't work out.
EDIT: I managed to transform the array into one and sorted all the worked years. Now i need to manage how to find what person worked each year. The code that i developed is following:
var variety = [[2000,2008],[2003,2009],[2001,2012],[2004,2020],[2003,2021],[1998,2015]], year = [], anos2 = 0, k=0, minYear=0, maxYear=0
function getyear(people){
for(var lin = 0; lin < people.length; lin++){ //Esse for é para transformar a matriz em um único vetor dinamicamente
for(var col = 0; col < people.length-(people.length-2); col++){
year[k] = people[lin][col]
k++
}
}
year.sort(compareNumbers) //Ordena o vetor dos anos trabalhados
console.log(year)
console.log(people)
}
function compareNumbers(a,b){
return a -b
}
getyear(variety)