Imagine you're a network administrator who needs to efficiently organize IP address ranges using CIDR blocks. This problem challenges you to find the minimum number of CIDR blocks needed to cover a specific range of IP addresses.
Background:
โข An IP address is a 32-bit number displayed as four decimal groups separated by dots (e.g., 192.168.1.1)
โข A CIDR block like 192.168.1.0/24 represents a range of IP addresses where the first 24 bits are fixed
Your Task:
Given a starting IP address and a count n, find the minimum number of CIDR blocks that cover exactly the range [ip, ip + n - 1]. No other IP addresses should be included.
Example: If you start at 255.0.0.7 and need to cover 10 addresses, you might use blocks like 255.0.0.7/32 (1 address), 255.0.0.8/30 (4 addresses), etc.
Input & Output
Constraints
- 1 โค n โค 1000
- The given IP address is valid
- ip + n - 1 will not overflow (stays within valid IP range)
- IP address format: Each octet is between 0 and 255