Tap here to switch tabs
Problem
Submissions

Problem: IP to CIDR

IP to CIDR converts a starting IPv4 address and a count into the minimum list of CIDR blocks covering exactly those consecutive addresses, with no extras.

Statement

An IPv44 address is a 3232 bit number written in the form "a.b.c.d", where a, b, c, and d are integers in the range [0,255][0, 255] representing four 88 bit groups.

A CIDR block is written in the form "a.b.c.d/len", where "a.b.c.d" is the starting IP address and len is an integer representing the prefix length in bits. A CIDR block with length len covers 232−len2^{32 - {len}} consecutive IP addresses that share the same first len bits.

Given a starting IPv44 address ip and an integer n, return a list of CIDR blocks that covers the range of n consecutive IP addresses starting at ip. The resulting list must cover every IP address in the range exactly once, contain no IP addresses outside the range, and use the minimum possible number of CIDR blocks.

Constraints:

  • 7≤7 \leq ip.length ≤15\leq 15

  • ip is a valid IPv44 address in the form "a.b.c.d" where a, b, c, and d are integers in the range [0,255][0, 255].

  • 1≤1 \leq n ≤1000\leq 1000

  • Every implied address ip + x (for x<nx < n) will be a valid IPv44 address.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: IP to CIDR

IP to CIDR converts a starting IPv4 address and a count into the minimum list of CIDR blocks covering exactly those consecutive addresses, with no extras.

Statement

An IPv44 address is a 3232 bit number written in the form "a.b.c.d", where a, b, c, and d are integers in the range [0,255][0, 255] representing four 88 bit groups.

A CIDR block is written in the form "a.b.c.d/len", where "a.b.c.d" is the starting IP address and len is an integer representing the prefix length in bits. A CIDR block with length len covers 232−len2^{32 - {len}} consecutive IP addresses that share the same first len bits.

Given a starting IPv44 address ip and an integer n, return a list of CIDR blocks that covers the range of n consecutive IP addresses starting at ip. The resulting list must cover every IP address in the range exactly once, contain no IP addresses outside the range, and use the minimum possible number of CIDR blocks.

Constraints:

  • 7≤7 \leq ip.length ≤15\leq 15

  • ip is a valid IPv44 address in the form "a.b.c.d" where a, b, c, and d are integers in the range [0,255][0, 255].

  • 1≤1 \leq n ≤1000\leq 1000

  • Every implied address ip + x (for x<nx < n) will be a valid IPv44 address.