Open In App

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:

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: 1



Input: N = 100
Output: 10

Approach: The given problem can be solved based on the following observations:

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++ 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;
}




/*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 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




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.




<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>

Output
10

Time Complexity: O(log N)
Auxiliary Space: O(1)


Article Tags :