Check if the door is open or closed

Last Updated : 21 Mar, 2025

Given n doors and n persons. The doors are numbered 1 to n and persons are given id's numbered 1 to n. Each door can have only 2 status open and closed. Initially all the doors have status closed.
Find the final status of all the doors if a person changes the current status of all the doors, i.e. if status open then change to status closed and vice versa, for which he is authorized. A person with id 'i' is authorized to change the status of door numbered 'j' if 'j' is a multiple of 'i'. 

Note: 

  • Initially all the doors have status closed.
  • A person has to change the current status of all the doors for which he is authorized exactly once. 
  • There can be a situation that before a person changes the status of the door, another person who is also authorized for the same door changes the status of the door. 

Example : 

Input 3
Output open closed closed
Explanation
The person with id 1 opens all doors : open open open
id 2 closes the second door : open closed open
id 3 closes the third door : open closed closed

Input 4
Output open closed closed openclosed closed closed
Explanation
The person with id 1 opens all doors : open open open open
id 2 closes the 2nd and 4th doors : open closed open closed
id 3 closes the third door : open closed closed closed
id 4 opens the fourth door : open closed closed open

Try It Yourself
redirect icon

[Naive Approach] Using Nested Loop â€“ O(n2) time and O(1) space

Each door is toggled once for every divisor of its number. Doors with an odd number of divisors remain open, and doors with an even number of divisors remain closed. Only perfect square doors (1, 4, 9, 16, 25, ...) have an odd number of divisors, so they remain open. Please note that, for every number, divisors appear in pairs. For example, 12 has divisors (1, 12), (3, 4) and (2, 6). For a perfect square, there is a pair that has same values and this makes the number of different divisors odd. For example for 16, divisors are (1, 16), (2, 8) and (4, 4)

C++
#include <iostream>
using namespace std;

void printStatusOfDoors(int n) {
    for (int i = 1; i <= n; i++) {
        int divisors = 0;
        for (int j = 1; j <= i; j++) {
            if (i % j == 0) {
                divisors++;
            }
        }
        if (divisors % 2 == 0) {
            cout << "closed ";
        } else {
            cout << "open ";
        }
    }
}

int main() {
    int n = 5;
    printStatusOfDoors(n);
    return 0;
}
Java
public class GfG{
    public static void printStatusOfDoors(int n) {
        for (int i = 1; i <= n; i++) {
            int divisors = 0;
            for (int j = 1; j <= i; j++) {
                if (i % j == 0) {
                    divisors++;
                }
            }
            if (divisors % 2 == 0) {
                System.out.print("closed ");
            } else {
                System.out.print("open ");
            }
        }
    }

    public static void main(String[] args) {
        int n = 5;
        printStatusOfDoors(n);
    }
}
Python
def print_status_of_doors(n):
    for i in range(1, n + 1):
        divisors = 0
        for j in range(1, i + 1):
            if i % j == 0:
                divisors += 1
        if divisors % 2 == 0:
            print("closed", end=" ")
        else:
            print("open", end=" ")

n = 5
print_status_of_doors(n)
C#
using System;

class GfG{
    static void PrintStatusOfDoors(int n) {
        for (int i = 1; i <= n; i++) {
            int divisors = 0;
            for (int j = 1; j <= i; j++) {
                if (i % j == 0) {
                    divisors++;
                }
            }
            if (divisors % 2 == 0) {
                Console.Write("closed ");
            } else {
                Console.Write("open ");
            }
        }
    }

    static void Main(string[] args) {
        int n = 5;
        PrintStatusOfDoors(n);
    }
}
JavaScript
function printStatusOfDoors(n) {
    for (let i = 1; i <= n; i++) {
        let divisors = 0;
        for (let j = 1; j <= i; j++) {
            if (i % j === 0) {
                divisors++;
            }
        }
        if (divisors % 2 === 0) {
            process.stdout.write("closed ");
        } else {
            process.stdout.write("open ");
        }
    }
}

const n = 5;
printStatusOfDoors(n);

Output
open closed closed open closed 

[Expected Approach] Finding Square Root – O(n log(n)) time and O(1) space

We can use Check if count of divisors is even or odd logic to solve the problem effectively.

C++
#include <bits/stdc++.h>
using namespace std;

// Function to check whether 'n'
// has even number of factors or not
bool hasEvenNumberOfFactors(int n)
{
    int root_n = sqrt(n);

    // if 'n' is a perfect square
    // it has odd number of factors
    if ((root_n*root_n) == n)
        return false;

    // else 'n' has even
    // number of factors
    return true;
}

// Function to find and print
// status of each door
void printStatusOfDoors(int n)
{
    for (int i=1; i<=n; i++)
    {
        // If even number of factors
        // final status is closed
        if (hasEvenNumberOfFactors(i))
            cout << "closed" << " ";

        // else odd number of factors
        // final status is open
        else
            cout << "open" << " ";
    }
}

// Driver program
int main()
{
    int n = 5;
    printStatusOfDoors(n);
    return 0;
}
Java
import java.io.*;

class GfG {
    
    // Function to check whether 'n'
    // has even number of factors or not
    static boolean hasEvenNumberOfFactors(int n)
    {
        double root_n = Math.sqrt(n);
    
        // if 'n' is a perfect square
        // it has odd number of factors
        if ((root_n*root_n) == n)
            return false;
    
        // else 'n' has even
        // number of factors
        return true;
    }
    
    // Function to find and print
    // status of each door
    static void printStatusOfDoors(int n)
    {
        for (int i = 1 ; i <= n; i++)
        {
            // If even number of factors
            // final status is closed
            if (hasEvenNumberOfFactors(i))
                System .out.print( "closed" + " ");
    
            // else odd number of factors
            // final status is open
            else
                System.out.print( "open" + " ");
        }
    }
    
    // Driver program
    public static void main (String[] args) {
        int n = 5;
        printStatusOfDoors(n);
        
    }
}
Python
import math

# Function to check whether
# 'n' has even number of 
# factors or not
def hasEvenNumberOfFactors(n):

    root_n = math.sqrt(n) 

    # if 'n' is a perfect square
    # it has odd number of factors
    if ((root_n * root_n) == n):
        return False

    # else 'n' has even
    # number of factors
    return True

# Function to find and print
# status of each door
def printStatusOfDoors(n):

    for i in range(1, n + 1):
    
        # If even number of factors
        # final status is closed
        if (hasEvenNumberOfFactors(i) == True):
            print("closed", end =" ") 

        # else odd number of factors
        # final status is open
        else:
            print("open", end =" ") 
    
# Driver program
n = 5

printStatusOfDoors(n) 
C#
using System;

class GfG {

    // Function to check whether
    // 'n' has even number of
    // factors or not
    static bool hasEvenNumberOfFactors(int n)
    {
        double root_n = Math.Sqrt(n);

        // if 'n' is a perfect square
        // it has odd number of factors
        if ((root_n * root_n) == n)
            return false;

        // else 'n' has even
        // number of factors
        return true;
    }

    // Function to find and print
    // status of each door
    static void printStatusOfDoors(int n)
    {
        for (int i = 1; i <= n; i++) {
            // If even number of factors
            // final status is closed
            if (hasEvenNumberOfFactors(i))
                Console.Write("closed"
                              + " ");

            // else odd number of factors
            // final status is open
            else
                Console.Write("open"
                              + " ");
        }
    }

    // Driver Code
    static public void Main()
    {
        int n = 5;
        printStatusOfDoors(n);
    }
}
JavaScript
function hasEvenNumberOfFactors(n)
{
    let root_n = Math.sqrt(n);

    // if 'n' is a perfect square
    // it has odd number of factors
    if ((root_n * root_n) == n)
        return false;

    // else 'n' has even
    // number of factors
    return true;
}

// Function to find and print
// status of each door
function printStatusOfDoors(n)
{
    for (let i = 1; i <= n; i++) {
        // If even number of factors
        // final status is closed
        if (hasEvenNumberOfFactors(i))
            console.log("closed" +
                        " ");

        // else odd number of factors
        // final status is open
        else
            console.log("open" +
                        " ");
    }
}

// Driver Code

let n = 5;
printStatusOfDoors(n);

Output
open closed closed open closed 
Comment