Calculate extra cost to be paid for luggage based on weight for Air travel - GeeksforGeeks

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.
Examples: 
Input: weight[] = {5, 4, 3, 6}, W = 8Output: 0Explanation: No weight crosses the threshold. So no extra cost is incurred.
Input: weight[] = {120, 135, 280, 60, 300}, W = 90Output: 1700Explanation: 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.If it has, then add extra cost according the excess weight following the table given.Else continue iteration.
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++
Java
Python
C#
Javascript
// 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 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 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# 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
<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)
 
lokeshpotta20
shikhasingrajput
samim2000
Algo-Geek 2021
Algo Geek
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Lexicographically smallest string formed by concatenating any prefix and its mirrored form
Check if the given string is valid English word or not
Check if an edge is a part of any Minimum Spanning Tree
Find One’s Complement of an Integer | Set 2
Sorting given character Array using Linked List
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Program to find GCD or HCF of two numbers