CodeChef Flight Luggage Problem
Link to the problem : codechef.com/problems/AIRLINE
Chef has 3 bags that she wants to take on a flight. They weigh A, B, and C kgs respectively. She wants to check-in exactly two of these bags and carry the remaining one bag with her.
The airline restrictions says that the total sum of the weights of the bags that are checked-in cannot exceed D kgs and the weight of the bag which is carried cannot exceed E kgs. Find if Chef can take all the three bags on the flight.
Constraints
1≤T≤36000
1≤A,B,C≤10
15≤D≤20
5≤E≤10
// Online C++ compiler to run C++ program online
#include <iostream>
#include <stdbool.h>
using namespace std;
int main() {
int checkIn=15,selfWeight=6,temp,temp2,count=0;
cout<<"Enter the weight's \n";
int arr[5];
for(int i=0;i<5;i++){
if(i<3){
cout<<"Enter the bag "<<i+1<<" weight"<<endl;
cin>>arr[i];
}
if(i==3){
cout<<"Enter the check-in weight: ";
cin>>checkIn;
}
if(i==4){
cout<<"Enter the self weight: ";
cin>>selfWeight;
}
}
// cout<<"The check-in value is : "<<checkIn<<endl;
// cout<<"The self-weight value is : "<<selfWeight<<endl;
cout<<"\n The array is : [ ";
for (int i=0;i<3;i++){
cout<<arr[i]<<" ";
}
cout<<"]"<<endl;
cout<<"sorting the Array"<<endl;
for(int i=0;i<2;i++){
if(arr[i]>arr[i+1]){
count++;
temp2=arr[i];
arr[i]=arr[i+1];
arr[i+1]=temp2;
}
}
if(count>=1){
for(int i=0;i<2;i++){
if(arr[i]>arr[i+1]){
temp2=arr[i];
arr[i]=arr[i+1];
arr[i+1]=temp2;
}
}
}
cout<<"\nThe array is : [ ";
for (int i=0;i<3;i++){
cout<<arr[i]<<" ";
}
cout<<"]"<<endl;
if(arr[0]<=selfWeight){
if(arr[1]+arr[2]>checkIn){
temp=arr[0];
arr[0]=arr[1];
arr[1]=temp;
if(arr[1]+arr[2]<=checkIn){
cout<<"Yes";
}
else{
cout<<"\nNo\n";
}
}
else{
cout<<"Yes";
}
}
else {
cout<<"\nNo";
}
return 0;
}