Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 5725 |

@blackshadows | 5685 |

@akhayrutdinov | 5111 |

@mb1973 | 4989 |

@Quandray | 4908 |

@saiujwal13083 | 4156 |

@sanjay05 | 3762 |

@marius_valentin_dragoi | 3511 |

@sushant_a | 3453 |

@KshamaGupta | 3318 |

@the_coder95 | 3302 |

Complete Leaderboard | |

Handle | Score |

@avaiesshaik782 | 674 |

@arpitsatnalika | 659 |

@tujhekya | 602 |

@code_it_ | 591 |

@prakharxyz | 583 |

@iamronak | 481 |

@Rahul Jadhav 1 | 466 |

@imnm | 466 |

@sivanandh03 | 464 |

@satyam0460 | 457 |

@18eumc107 | 443 |

Complete Leaderboard |

There are N coins numbered from 1 to N, all turned tails.N persons flip these coins. When the ith person enters, he flips all coins whose number is divisible by i.Calculate the number of coins that will remain heads after N persons enter and perform fliping operation.

**Example 1:**

**Input:**
N=3
**Output:**
1
**Explanation:**
initially the configuration is TTT
Person 1 flips all coins divisible
by 1.Now,Configuration is HHH.
Person 2 flips all coins divisible by
2.Now,Configuration is HTH.
Person 3 flips all coins divisible by
3.Now,finally configuration is HTT.
Thus,Number of heads is 1.

**Example 2:**

**Input:**
N=5
**Output:**
2
**Explanation:**
Initial configuration=TTTTT
The configuration changes as follows:
TTTTT->HHHHH->HTHTH->HTTTH->
HTTHH->HTTHT.
So, finally number of heads is 2.

**Your Task:**

You don't need to read input or print anything.Your task is to complete the function **numberOfHeads()** which takes the number N as input parameter and returns the number of coins that show heads after N persons have performed the flipping peration.

**Expected Time Complexity:**O(LogN)

**Expected Auxillary Space:**O(1)

**Constraints:**

1<=N<=10^{18}

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes
Number of Heads

...

Please choose 'ReadOnlyMode' if you needn't to 'Edit' the problem e.g. viewing OJ's solution, TestCase Files (TCFs), TimeLimit etc.