Check if the door is open or closed
Last Updated :
19 Sep, 2023
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 3
Current 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 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:
Approach to solve this problem is to observe that a door is toggled once for every divisor it has. If the number of divisors is odd, the door will end up open, otherwise it will be closed. Using this observation, we can iterate through each door and count its divisors. If the count is odd, the door is open, otherwise it’s closed.
- Initialize a variable divisors to 0 to count the number of divisors of the door number.
- Loop through the divisors of the door number from 1 to the door number itself and for each divisor.
- Check if the divisor is a factor of the door number by checking if the remainder of dividing the door number by the divisor is zero.
- If the divisor is a factor, increment the divisors variable.
- Check if the number of divisors is even by checking if the remainder of dividing the divisors variable by 2 is zero.
- If the number of divisors is even, print “closed” for the door.
- If the number of divisors is odd, print “open” for the door.
Below is the implication of the above approach:
C++
#include <bits/stdc++.h>
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 Main {
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);
}
}
|
Python3
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 Program {
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
Time Complexity: O(n^2), as there are two nested loops, one iterating over n elements and the other iterating over i elements.
Space Complexity: O(1), as we are not using any extra space.
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++
#include <bits/stdc++.h>
using namespace std;
bool hasEvenNumberOfFactors( int n)
{
int root_n = sqrt (n);
if ((root_n*root_n) == n)
return false ;
return true ;
}
void printStatusOfDoors( int n)
{
for ( int i=1; i<=n; i++)
{
if (hasEvenNumberOfFactors(i))
cout << "closed" << " " ;
else
cout << "open" << " " ;
}
}
int main()
{
int n = 5;
printStatusOfDoors(n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static boolean hasEvenNumberOfFactors( int n)
{
double root_n = Math.sqrt(n);
if ((root_n*root_n) == n)
return false ;
return true ;
}
static void printStatusOfDoors( int n)
{
for ( int i = 1 ; i <= n; i++)
{
if (hasEvenNumberOfFactors(i))
System .out.print( "closed" + " " );
else
System.out.print( "open" + " " );
}
}
public static void main (String[] args) {
int n = 5 ;
printStatusOfDoors(n);
}
}
|
Python3
import math
def hasEvenNumberOfFactors(n):
root_n = math.sqrt(n)
if ((root_n * root_n) = = n):
return False
return True
def printStatusOfDoors(n):
for i in range ( 1 , n + 1 ):
if (hasEvenNumberOfFactors(i) = = True ):
print ( "closed" , end = " " )
else :
print ( "open" , end = " " )
n = 5
printStatusOfDoors(n)
|
C#
using System;
class GFG
{
static bool hasEvenNumberOfFactors( int n)
{
double root_n = Math.Sqrt(n);
if ((root_n * root_n) == n)
return false ;
return true ;
}
static void printStatusOfDoors( int n)
{
for ( int i = 1 ; i <= n; i++)
{
if (hasEvenNumberOfFactors(i))
Console.Write( "closed" + " " );
else
Console.Write( "open" + " " );
}
}
static public void Main ()
{
int n = 5;
printStatusOfDoors(n);
}
}
|
Javascript
<script>
function hasEvenNumberOfFactors(n)
{
let root_n = Math.sqrt(n);
if ((root_n*root_n) == n)
return false ;
return true ;
}
function printStatusOfDoors(n)
{
for (let i = 1 ; i <= n; i++)
{
if (hasEvenNumberOfFactors(i))
document.write( "closed" + " " );
else
document.write( "open" + " " );
}
}
let n = 5;
printStatusOfDoors(n);
</script>
|
PHP
<?php
function hasEvenNumberOfFactors( $n )
{
$root_n = sqrt( $n );
if (( $root_n * $root_n ) == $n )
return false;
return true;
}
function printStatusOfDoors( $n )
{
for ( $i = 1; $i <= $n ; $i ++)
{
if (hasEvenNumberOfFactors( $i ))
echo "closed" , " " ;
else
echo "open" , " " ;
}
}
$n = 5;
printStatusOfDoors( $n );
?>
|
Output
open closed closed open closed
Time complexity : O(nlogn)
Auxiliary Space: O(1)
References: Asked in an interview of TCS
Share your thoughts in the comments
Please Login to comment...