Disk Space Calculator

Medium2s max execution

You're working on a file system implementation in C++ and need to calculate how much disk space a file actually occupies when stored on disk.

📀 Understanding File Storage:

  1. Files have two sizes:
    • The actual file size in bytes
    • The disk space used, which is always greater than or equal to the file size
  2. Storage is divided into fixed-size clusters.
    • A file may occupy multiple clusters
    • You must allocate whole clusters (no partial clusters allowed)
    • If a file spills over the end of a cluster, you must allocate the next cluster entirely

💡 Quick Example:

For a file of 1500 bytes on a disk with 512-byte clusters:

  • 1500 ÷ 512 ≈ 2.93 clusters needed
  • Round up to 3 clusters
  • Total space used = 3 × 512 = 1536 bytes

🎯 Your Task:

Implement the compute_size_on_disk function in C++ that calculates the total disk space used by a file given the cluster size and file size (in bytes). Do not include a main() function; your code will be wrapped automatically.

Examples:

Input: [512, 1500]
Output: 1536

1500 bytes requires 3 clusters of 512 bytes each, totaling 1536 bytes.

Input: [1024, 2048]
Output: 2048

2048 bytes fits exactly in 2 clusters of 1024 bytes.

Input: [512, 513]
Output: 1024

513 bytes requires 2 clusters because one cluster (512 bytes) is insufficient.

Constraints:

  • •1 ≤ cluster_size ≤ 10000
  • •1 ≤ file_size ≤ 100000
  • •All inputs are positive integers
  • •All values are in bytes
Standard includes available:iostream, string, algorithm, vector, etc.
Loading...

Custom Test Input

Output

Run your code to see output...