Skip to main content

Accenture

Accenture OA-24 2024

Problem Description

You are given a program to find the count of magical numbers from 1 to N. A magical number is defined by the following criteria:

  1. Convert to Binary: Convert each number in the range 1 to N (inclusive) to its binary representation.
  2. Replace Digits: Replace '0' with '1' and '1' with '2' in the binary string.
  3. Calculate Sum: Calculate the sum of the digits in the modified binary string.
  4. Check for Odd Sum: If the resultant number is odd, then it is considered a magical number.

Your task is to find and return an integer value representing the count of the magical numbers present within the given range.

Input Format:

  • input1: An integer value N representing the range of numbers.

Output Format:

Return an integer value representing the count of magical numbers present within the range.

Constraints:

  • 1 ≤ N ≤ 10^9

Example

Sample Input:

input1: 2

Sample Output:

1

Explanation:
For 1: Binary representation is 1, after replacing becomes 2, the sum is odd.

  • For 2: Binary representation is 10, after replacing becomes 21, the sum is odd.

So, both 1 and 2 are magical numbers.