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.
- Check if the current luggage has weight more than W.
- Return the total extra cost
Below is the implementation of the above approach,
C++
// C++ code to implement the above approach#include <bits/stdc++.h>using namespace std;Â
// Function to calculate the extra costint weighingMachine(int N, int weight[], int W){Â Â Â Â int amount = 0;Â
    // Loop to calculate the excess cost    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;}Â
// Driver codeint main(){Â Â Â Â int weight[] = { 120, 135, 280, 60, 300 };Â Â Â Â int N = 5;Â Â Â Â int W = 90;Â
    cout << weighingMachine(N, weight, W);}Â
// This code is contributed by Samim Hossain Mondal. |
Java
// Java code to implement the above approachimport java.util.*;Â
class GFG {Â
    // Function to calculate the extra cost    static int weighingMachine(int N, int weight[], int W)    {        int amount = 0;Â
        // Loop to calculate the excess cost        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;    }Â
    // Driver code    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
# Python code to implement the above approachÂ
# Function to calculate the extra costdef weighingMachine(N, weight, W):Â Â Â Â Â Â Â Â Â amount = 0;Â
    # Loop to calculate the excess cost    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Â
# Driver codeweight = [ 120, 135, 280, 60, 300 ]N = 5W = 90Â
print(weighingMachine(N, weight, W))Â
# This code is contributed by Samim Hossain Mondal. |
C#
// C# code to implement the above approachusing System;Â
public class GFG {Â
  // Function to calculate the extra cost  static int weighingMachine(int N, int []weight, int W)  {    int amount = 0;Â
    // Loop to calculate the excess cost    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;  }Â
  // Driver code  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));  }}Â
// This code is contributed by 29AjayKumar |
Javascript
<script>Â Â Â Â Â Â Â Â // JavaScript code for the above approachÂ
Â
        // Function to calculate the extra cost        function weighingMachine(N, weight, W) {            let amount = 0;Â
            // Loop to calculate the excess cost            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;        }Â
        // Driver codeÂ
        let weight =            [120, 135, 280, 60, 300];        let N = 5;        let W = 90;Â
        document.write(            weighingMachine(N, weight, W));Â
Â
  // This code is contributed by Potta Lokesh    </script> |
Â
Â
1700
Time Complexity: O(N)
Auxiliary Space: O(1)
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

… [Trackback]
[…] Find More on on that Topic: geeksforgeeks.org/calculate-extra-cost-to-be-paid-for-luggage-based-on-weight-for-air-travel/ […]