Check if the door is open or closed - GeeksforGeeks

23 Mar, 2021
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: – 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 : As n = 3, therefore there are 3 doors {1, 2, 3} and 3 persons with ids {1, 2, 3}person with id = 1 can change the status of door 1, 2, 3 person with id = 2 can change the status of door 2 person with id = 3 can change the status of door 3Current status of all doors: closed closed closed Consider a sequence of events,  
Person with id = 1 changes status of door 2 Current status of all doors: closed open closedPerson with id = 3 changes status of door 3 Current status of all doors: closed open openPerson with id = 1 changes status of door 1, 3 Current status of all doors: open open closedPerson with id = 2 changes status of door 2 Current status of all doors: open closed closed
Person with id = 1 changes status of door 2 Current status of all doors: closed open closed
Person with id = 3 changes status of door 3 Current status of all doors: closed open open
Person with id = 1 changes status of door 1, 3 Current status of all doors: open open closed
Person with id = 2 changes status of door 2 Current status of all doors: open closed closed
Another Example :
Input : 5
Output : open closed closed open closed

Note: Sequence of open/closed is displayed in
increasing door number 
 
Approach: It is mathematical and logical approach. If we observe it properly, then we find that the final status of a door numbered i is open if ‘i’ has odd number of factors and status is closed if ‘i’ has even number of factors. It does not depend in which sequence the status of doors are changed. To find whether count of divisors of number is even or odd, we can see Check if count of divisors is even or odd post.  
C++
Java
Python3
C#
PHP
Javascript
// C++ implementation of// doors open or closed#include <bits/stdc++.h>using namespace std; // Function to check whether 'n'// has even number of factors or notbool 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 doorvoid 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 programint main(){    int n = 5;    printStatusOfDoors(n);    return 0;}
// java implementation of// doors open or closedimport 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);             }} // This article is contributed by vt_m
# Python 3 implementation of# doors open or closedimport math # Function to check whether# 'n' has even number of# factors or notdef 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 doordef 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 programn = 5 printStatusOfDoors(n) # This code is contributed by Smitha Dinesh Semwal
// C# implementation of// doors open or closedusing System; class GFG{ // Function to check whether// 'n' has even number of// factors or notstatic 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 doorstatic 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 Codestatic public void Main (){    int n = 5;    printStatusOfDoors(n);}} // This Code is contributed by ajit
<?php// PHP implementation of// doors open or closed // Function to check whether// 'n' has even number of// factors or not function hasEvenNumberOfFactors($n){    $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 doorfunction printStatusOfDoors($n){    for ($i = 1; $i <= $n; $i++)    {        // If even number of factors        // final status is closed        if (hasEvenNumberOfFactors($i))            echo "closed" ," ";         // else odd number of factors        // final status is open        else            echo "open" ," ";    }} // Driver Code$n = 5;printStatusOfDoors($n); // This code is contributed by ajit@?>
<script> // JavaScript program for the above approach     // Function to check whether 'n'    // has even number of factors or not    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))                document.write( "closed" + " ");                 // else odd number of factors            // final status is open            else                document.write( "open" + " ");        }    }     // Driver Code         let n = 5;    printStatusOfDoors(n); // This code is contributed by susmitakundugoaldanga.</script>
Output :  
open closed closed open closed
Time complexity : O(n)References: Asked in an interview of TCS This article is contributed by Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
jit_t
AryanMehrotra
susmitakundugoaldanga
prime-factor
TCS
Mathematical
TCS
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Modulo Operator (%) in C/C++ with Examples
Merge two sorted arrays
Prime Numbers
Program to find sum of elements in a given array
Operators in C / C++
Program for factorial of a number
Program for Decimal to Binary Conversion
Algorithm to solve Rubik's Cube
Print all possible combinations of r elements in a given array of size n
The Knight's tour problem | Backtracking-1