Number of open doors | TCS Coding Question
Consider a long alley with N doors on one side. All the doors are closed initially. You move to and fro in the alley changing the states of the doors as follows:
- You open a door that is already closed, and you close a door that is already opened.
- You start at one end go on altering the state of the doors till you reach the other end, and then you come back and start altering the states of the doors again.
- In the first go, you alter the states of doors numbered 1, 2, 3, …, N.
- In the second go, you alter the states of doors numbered 2, 4, 6, ….
- In the third go, you alter the states of doors numbered 3, 6, 9 ….
- and so on…
The above procedure will continue till the Nth turn in which you alter the state of the door numbered N. The task is to find the number of open doors at the end of the procedure.
Examples:
Input: N = 3
Output: 1Input: N = 100
Output: 10
Approach: The given problem can be solved based on the following observations:
- A door can only be open in the end if and only if it has an odd number of factors because each door is visited once by each of its factors.
- Initially, all doors are closed so it will remain closed if it has an even number of factors and will remain open if it has an odd number of factors.
- Therefore, the total number of doors that remain open from 1 to N will be the number of doors that have an odd number of factors.
From the above observations, only the number which is perfect squares having an odd number of factors. Therefore, the total number of doors that remain open from 1 to N will be the number of perfect squares between 1 to N, and the number of perfect squares between 1 to N is sqrt(N).
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function that counts the number of // doors opens after the Nth turn int countOpenDoors( int N) { // Find the number of open doors int doorsOpen = sqrt (N); // Return the resultant count of // open doors return doorsOpen; } // Driver Code int main() { int N = 100; cout << countOpenDoors(N); return 0; } |
Java
/*package whatever //do not write package name here */ import java.io.*; class GFG { // Function that counts the number of // doors opens after the Nth turn static int countOpenDoors( int N) { // Find the number of open doors int doorsOpen = ( int ) Math.sqrt(N); // Return the resultant count of // open doors return doorsOpen; } // Driver Code public static void main (String[] args) { int N = 100 ; System.out.println(countOpenDoors(N)); } } // This code is contributed by Potta Lokesh |
Python3
# Python3 code for the above approach import math # Function that counts the number of # doors opens after the Nth turn def countOpenDoors(N): # Find the number of open doors doorsOpen = int (math.sqrt(N)) # Return the resultant count of # open doors return doorsOpen # Driver Code if __name__ = = '__main__' : N = 100 print (countOpenDoors(N)) # This code is contributed by MuskanKalra1 |
C#
using System; class GFG { // Function that counts the number of // doors opens after the Nth turn static int countOpenDoors( int N) { // Find the number of open doors int doorsOpen = ( int )Math.Sqrt(N); // Return the resultant count of // open doors return doorsOpen; } // Driver Code public static void Main() { int N = 100; Console.Write(countOpenDoors(N)); } } // This code is contributed by subhammahato348. |
Javascript
<script> // JavaScript program for the above approach // Function that counts the number of // doors opens after the Nth turn function countOpenDoors(N) { // Find the number of open doors let doorsOpen = parseInt(Math.sqrt(N)); // Return the resultant count of // open doors return doorsOpen; } // Driver Code let N = 100; document.write(countOpenDoors(N)); // This code is contributed by Potta Lokesh </script> |
10
Time Complexity: O(log N)
Auxiliary Space: O(1)
Please Login to comment...