Back to problems

Distance from Each 1 to the Nearest 2 in an Array

Algorithm · Snowflake · Medium

Problem: Find the Closest 2 for Each 1 in an Array You receive an array arr containing n elements, where every entry is either 0, 1, or 2. For each index holding 1, determine how far it is from the closest index holding 2. For indices i and j, distance is measured as i - j . Print these distances in the order the 1 values appear when scanning from left to right. When the array contains no 2, print -1 once for every occurrence of 1. When no element is 1, print a blank line.…

Checking your access…