Race Car Instruction Count Problem

Race Car Instruction Count Problem — ExecCode Hard DSA Practice

Solve the Race Car Instruction Count problem on ExecCode. Free online hard DSA practice in Graph. Write and run code in Java, C++, Python — no signup required to run.

Problem description

A race car starts at position 0 with speed +1. Instruction A: position += speed; speed *= 2. Instruction R: speed becomes -1 if it was positive, else +1 (position unchanged). Reach a target position with the fewest instructions. Return the length of a shortest instruction string that stops exactly on target (speed may be nonzero). You are given: target — positive integer position Return integer instruction count. You do not need to output the string, only its length.

Examples

Input {'target': 3}; Output 2. Input {'target': 6}; Output 5. Input {'target': 1}; Output 1

Constraints

target >= 1 You may overshoot and reverse

Practice Race Car Instruction Count free on ExecCode. Browse DSA problems, topic map, and placement guides.