Asked inAmazon,SDE-2

Ninja and the Bulbs Challenge

Ninja owns an electronic shop and possesses 'N' bulbs. To verify the quality of the bulbs, Ninja performs a unique technique. After 'N' rounds of this process, bulbs that remain on are considered good quality. Your task is to determine the number of good quality bulbs for Ninja.

Explanation:

The technique is described as follows:

  • In the first round, all bulbs are turned on.
  • In the second round, every second bulb is turned off.
  • In the third round, every third bulb is toggled (turned off if it's on, turned on if it's off).
  • This process continues for 'N' rounds.

At the end of 'N' rounds, the bulbs that remain on are considered of good quality.

Example:
Input:
N = 4
Output:
2
Explanation:

Here's the status of the bulbs after each round:

  • Round 1: All bulbs are on.
  • Round 2: Bulbs 2 and 4 are turned off.
  • Round 3: Bulb 3 is turned off.
  • Round 4: Bulb 4 is turned on.

At the end, bulbs 1 and 4 are on, resulting in 2 good bulbs.

Constraints:

  • 1 <= T <= 10
  • 1 <= N <= 10^9
  • Time limit: 1 sec
AnswerBot
1mo

Determine the number of good quality bulbs remaining after 'N' rounds of a unique technique.

  • Start with all bulbs on in the first round

  • Toggle every 'i' bulb in 'i' round

  • Count the bulbs that remain on a...read more

Help your peers!
Add answer anonymously...
Amazon SDE-2 Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter