Is the Array Circular?

Published by Helen Yu in

Write a function that determines if an array is circular. An array is circular if its subarrays can be reordered such that each subarray's last element is equal to the next subarray's first element.

For example, the array [[1, 1, 1], [9, 2, 3, 4], [4, 1], [1, 2, 5, 7, 9]] is circular because we can re-arrange the elements to create the following array:

[[9, 2, 3, 4], [4, 1], [1, 1, 1], [1, 2, 5, 7, 9]]

Examples

is_circular([[9, 8], [6, 9, 1], [8, 4, 2], [1, 9], [2, 1, 6]]) ➞ true
# Can be reordered: [[9, 8], [8, 4, 2], [2, 1, 6], [6, 9, 1], [1, 9]]

is_circular([[1, 1], [1, 2]]) ➞ false

is_circular([[2, 1], [1, 2]]) ➞ true

is_circular([[2, 1], [1, 2], [2, 1], [1, 3, 1], [1, 4, 4]]) ➞ false

Notes

  • In a circular re-ordering, the last subarray's last element must be identical to the first subarray's first element.
  • Subarrays will contain at least one element.
Watch a quick demo on how Edabit works.