Calculate extra cost to be paid for luggage based on weight for Air travel
Last Updated :
11 Jan, 2022
Given an array weight[] of size N containing weights of luggage. If the weights are within a threshold of W then it does not require any extra cost. But after the weights cross the threshold they need to pay extra cost according to the following table. The task is to calculate the extra cost of the luggage required.
Range of exceeding weight |
Cost for the excess weight |
0 – 50 |
100 |
51 – 100 |
200 |
101 – 150 |
300 |
151 – 200 |
500 |
> 200 |
1000 |
Examples:
Input: weight[] = {5, 4, 3, 6}, W = 8
Output: 0
Explanation: No weight crosses the threshold. So no extra cost is incurred.
Input: weight[] = {120, 135, 280, 60, 300}, W = 90
Output: 1700
Explanation: The weight 120 is 30 more than the threshold. So, it requires 100 extra cost.
The weight 135 is 45 more than the threshold. So, it requires 100 extra cost.
The weight 280 is 190 more than the threshold. So, it requires 500 extra cost.
The weight 300 is 210 more than the threshold. So, it requires 1000 extra cost.
And the weight 60 is within threshold. So, it requires no cost.
The total extra cost is (100 + 100 + 500 + 1000) = 1700
Approach: The approach is based on following observation. If a luggage has weight above threshold W then it incurs extra cost according to the given table. Follow the steps mentioned below to solve the problem:
- Iterate the array from the start.
- Check if the current luggage has weight more than W.
- If it has, then add extra cost according the excess weight following the table given.
- Else continue iteration.
- Return the total extra cost
Below is the implementation of the above approach,
C++
#include <bits/stdc++.h>
using namespace std;
int weighingMachine( int N, int weight[], int W)
{
int amount = 0;
for ( int i = 0; i < N; i++) {
if (weight[i] - W > 0 && weight[i] - W <= 50)
amount += 100;
else if (weight[i] - W > 50 && weight[i] - W <= 100)
amount += 200;
else if (weight[i] - W > 100
&& weight[i] - W <= 150)
amount += 300;
else if (weight[i] - W > 150
&& weight[i] - W <= 200)
amount += 500;
else if (weight[i] - W > 200)
amount += 1000;
}
return amount;
}
int main()
{
int weight[] = { 120, 135, 280, 60, 300 };
int N = 5;
int W = 90;
cout << weighingMachine(N, weight, W);
}
|
Java
import java.util.*;
class GFG {
static int weighingMachine( int N, int weight[], int W)
{
int amount = 0 ;
for ( int i = 0 ; i < N; i++) {
if (weight[i] - W > 0
&& weight[i] - W <= 50 )
amount += 100 ;
else if (weight[i] - W > 50
&& weight[i] - W <= 100 )
amount += 200 ;
else if (weight[i] - W > 100
&& weight[i] - W <= 150 )
amount += 300 ;
else if (weight[i] - W > 150
&& weight[i] - W <= 200 )
amount += 500 ;
else if (weight[i] - W > 200 )
amount += 1000 ;
}
return amount;
}
public static void main(String[] args)
{
int weight[] =
{ 120 , 135 , 280 , 60 , 300 };
int N = 5 ;
int W = 90 ;
System.out.println(
weighingMachine(N, weight, W));
}
}
|
Python
def weighingMachine(N, weight, W):
amount = 0 ;
for i in range ( 0 , N):
if (weight[i] - W > 0 and weight[i] - W < = 50 ):
amount = amount + 100
elif (weight[i] - W > 50 and weight[i] - W < = 100 ):
amount = amount + 200
elif (weight[i] - W > 100
and weight[i] - W < = 150 ):
amount = amount + 300
elif (weight[i] - W > 150
and weight[i] - W < = 200 ):
amount = amount + 500 ;
elif (weight[i] - W > 200 ):
amount = amount + 1000
return amount
weight = [ 120 , 135 , 280 , 60 , 300 ]
N = 5
W = 90
print (weighingMachine(N, weight, W))
|
C#
using System;
public class GFG {
static int weighingMachine( int N, int []weight, int W)
{
int amount = 0;
for ( int i = 0; i < N; i++) {
if (weight[i] - W > 0
&& weight[i] - W <= 50)
amount += 100;
else if (weight[i] - W > 50
&& weight[i] - W <= 100)
amount += 200;
else if (weight[i] - W > 100
&& weight[i] - W <= 150)
amount += 300;
else if (weight[i] - W > 150
&& weight[i] - W <= 200)
amount += 500;
else if (weight[i] - W > 200)
amount += 1000;
}
return amount;
}
public static void Main(String[] args)
{
int []weight =
{ 120, 135, 280, 60, 300 };
int N = 5;
int W = 90;
Console.WriteLine(
weighingMachine(N, weight, W));
}
}
|
Javascript
<script>
function weighingMachine(N, weight, W) {
let amount = 0;
for (let i = 0; i < N; i++) {
if (weight[i] - W > 0
&& weight[i] - W <= 50)
amount += 100;
else if (weight[i] - W > 50
&& weight[i] - W <= 100)
amount += 200;
else if (weight[i] - W > 100
&& weight[i] - W <= 150)
amount += 300;
else if (weight[i] - W > 150
&& weight[i] - W <= 200)
amount += 500;
else if (weight[i] - W > 200)
amount += 1000;
}
return amount;
}
let weight =
[120, 135, 280, 60, 300];
let N = 5;
let W = 90;
document.write(
weighingMachine(N, weight, W));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...