medium
1 views

Discount Slabs

Calculate the discounted price based on predefined slabs and their corresponding discount percentages

Understand the Problem

Problem Statement

A shop offers discounts based on the purchase price. There are N discount slabs defined by boundary amounts and corresponding discount percentages. When a purchase price falls between two consecutive slab amounts (i.e., between the i-th and (i+1)-th slab), the discount percentage for the i-th slab is applied. If the price is below the lowest slab, no discount is applied. Calculate and output the final discounted amount with exactly two decimal places.

For example, if slabs are [1000, 2000, 3000, 4000] with discounts [12%, 16%, 18%, 24%], a price of 3400 falls between 3000 and 4000, so the 18% discount is applied.

Constraints

  • 1 ≤ N ≤ 100 (number of discount slabs)
  • Price is a positive floating-point number
  • Slab amounts are positive integers in ascending order
  • Discount percentages are positive integers between 1 and 100
  • Output must be formatted to exactly 2 decimal places

Examples

Example 1
Input
4 3400
1000 2000 3000 4000
12 16 18 24
Output
2788.00
Explanation

The price 3400 falls between slab 3000 and 4000, so the 3rd slab's discount (18%) is applied. Final price = 3400 - (3400 * 18/100) = 3400 - 612 = 2788.00

Example 2
Input
4 400
500 1000 1500 2000
10 20 30 40
Output
400.00
Explanation

The price 400 is below the lowest slab (500), so no discount is applied. The final price remains 400.00

Solution

#include <stdio.h>

int main() {
    int n, slab_amount, discount_percent = 0;
    float price;
    int applicable_slab_index = -1;
    
    // Read number of slabs and price
    scanf("%d %f", &n, &price);
    
    // Find the applicable slab
    for (int i = 0; i < n; i++) {
        scanf("%d", &slab_amount);
        if (price >= slab_amount) {
            applicable_slab_index = i;
        }
    }
    
    // Read discount percentages and get the one for applicable slab
    for (int i = 0; i < n; i++) {
        scanf("%d", &discount_percent);
        if (i == applicable_slab_index) {
            break;
        }
    }
    
    // Calculate and print final price
    float final_price;
    if (applicable_slab_index == -1) {
        // No discount applies
        final_price = price;
    } else {
        final_price = price - (price * discount_percent / 100.0);
    }
    
    printf("%.2f", final_price);
    
    return 0;
}
Time:O(N) - We iterate through slabs twice, but each iteration is linear
Space:O(1) - We only use a constant amount of extra space regardless of input size
Approach:

C Solution Explanation:

  1. Variable Declaration: We declare variables for number of slabs (n), slab amounts (slab_amount), discount percentage (discount_percent), price (price), and tracking the applicable slab index (applicable_slab_index)
  2. Input Reading: First scanf reads n and price from the first line
  3. Slab Processing: The first loop reads slab amounts and updates applicable_slab_index whenever the price is >= current slab amount. This finds the highest applicable slab
  4. Discount Processing: The second loop reads discount percentages. We break once we reach the applicable slab index, so discount_percent contains the correct discount rate
  5. Calculation: If no slab applies (applicable_slab_index == -1), we keep the original price. Otherwise, we apply the discount formula: price - (price * discount / 100)
  6. Output: printf with "%.2f" formats the result to exactly 2 decimal places

Visual Explanation

Loading diagram...