Problem · Bit Manipulation
IP Address to CIDR Blocks
Learn this problemProblem statement
Given a starting IPv4 address startIp and a positive integer count, return the minimum set of CIDR blocks that exactly covers the count consecutive IPv4 addresses starting at startIp.
Each CIDR block must be written in the form a.b.c.d/prefix. The returned blocks must cover the range exactly and use the minimum possible number of blocks.
You may assume the input is a valid IPv4 address and that the range stays within the IPv4 space.
Function
ipRangeToCidrBlocks(startIp: String, count: long) → String[]Complete the function ipRangeToCidrBlocks in the editor below.
ipRangeToCidrBlocks has the following parameters:
String startIp: the first IPv4 address in the rangelong count: the number of consecutive IPv4 addresses to cover
Returns
String[]: the minimum CIDR cover for the range.
Examples
Example 1
startIp = "0.0.0.0"count = 1return = ["0.0.0.0/32"]A single IPv4 address is represented by a /32 CIDR block.
Example 2
startIp = "0.0.0.0"count = 256return = ["0.0.0.0/24"]The range from 0.0.0.0 through 0.0.0.255 is exactly one /24 block.
Constraints
1 <= count <= 2^32startIpis a valid IPv4 address.- The answer must cover exactly the requested range using the minimum number of blocks.