A classroom has several students, half of whom are boys and half of whom are girls. You need to arrange all of them in a line for the morning assembly such that the following conditions are satisfied:
The students must be in the order of non-decreasing height. Two boys or two girls must not be adjacent to each other. You have been given the heights of the boys in the array and the heights of the girls in the array. Find out whether you can arrange them in an order which satisfies the given conditions. Print "YES" if it is possible, or "NO" if it is not.
Let's say there are boys n=3 and girls n=3, where the boys' heights are b = [5,3,8 ] and the girls' heights area = [2,4,6 ]. These students can be arranged in the order , which is [2, 3, 4, 5, 6, 8]. Because this is in order of non-decreasing height, and no two boys or two girls are adjacent to each other, this satisfies the conditions. Therefore, the answer is "YES".
Function (My code) :
string arrangeStudents(vector<int> a, vector<int> b) {
sort(a.begin(),a.end());
sort(b.begin(),b.end());
int n = a.size();
for(int i{};i<n;i++){
if(a[i] == b[i] ){
return "YES";
}}
return "NO";
for(int i{};i<n;i++){
if(a[i] > b[i]){
return "YES";
}}
return "NO";
for(int i{};i<n;i++){
if(a[i] == b[i] ){
return "YES";
}}
return "NO";
}
function arrangeStudents(a, b) {
RépondreSupprimerconst result = []
let i = 0;
let j = 0;
while (i < a.length && j < b.length) {
if (a[i] < b[j]) {
if (i > 0 && j > 0 && a[i - 1] !== b[j - 1] && result[result.length - 1] == a[i - 1]) {
return 'NO'
}else if (j == 0 && i > 0 && result[result.length - 1] == a[i - 1]){
return 'NO'
}else {
result.push(a[i])
}
i++
}else if(a[i] > b[j]) {
if (i > 0 && j > 0 && a[i - 1] !== b[j - 1] && result[result.length - 1] == b[j - 1]) {
return 'NO'
}else if (i == 0 && j > 0 && result[result.length - 1] == b[j - 1]){
return 'NO'
}else {
result.push(b[j])
}
j++;
}else {
if (i > 0 && result[result.length - 1] == a[i -1]) {
result.push(b[j], a[i])
}else {
result.push(a[i], b[j])
}
i++
j++
}
}
return 'YES'
}