Check if the door is open or closed | Practice | GeeksforGeeks

Given N doors and N persons. The doors are numbered from 1 to N and persons are given id’s numbered from 1 to N. Each door can have only two statuses i.e. open (1) or closed (0) . Initially all the doors have status closed. Find the final status of all the doors, when all the persons have changed the status of the doors of which they are authorized. i.e. if status open then change the status to closed and vice versa. 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.
 
Example 1: 
Input:
N = 3
Output:
1 0 0 
Explanation:
Initiall status of rooms - 0 0 0. 
Person with id 2 changes room 2 to open,
i.e. (0 1 0). Person with id 1 changes
room 1, 2, 3 status (1 0 1).
Person with id 3 changes room 3
status i.e. (1 0 0).
Example 2: 
Input:
N = 2
Output:
1 0
Explanation:
Initiall status of rooms - 0 0. 
Person with id 2 changes room 2 to open,
i.e. (0 1). Person with id 1 changes
room 1, 2 status (1 0).

 
Your Task:
You don't need to read input or print anything. Your task is to complete the function checkDoorStatus() which takes an Integer N as input and returns an array as the answer.
 
Expected Time Complexity: O(N*sqrt(N))
Expected Auxiliary Space: O(N)
 
Constraints:
1 <= N <= 104
0
mayank20212 weeks ago
C++ : 0.03/1.4int *checkDoorStatus(int N) {      // int arr[N];
         int *arr = new int[N];       for(int i=1; i<= N ; i++)        {            int sqrti=sqrt(i);            if(sqrti*sqrti==i) arr[i-1]=1;            else arr[i-1]=0;             }     return arr;   }
+2
ncln538jsgdizpte5utrdkgdmv73rde23vovyyh52 months ago
int numberoffactor(int n){       int count = 0;       for(int i=1;i*i<=n;i++){           if(n%i==0){               count++;               if(n/i!=i){                   count++;               }           }       }       return count;   }   int *checkDoorStatus(int N) {       // code here       int *arr = new int[N];       for(int i=0;i<N;i++){           arr[i] = 0;       }       for(int i=4;i<=N;i++){           if(numberoffactor(i)%2 !=0){               arr[i-1] = 1;            }       }       arr[0] = 1;       return arr;   }
0
gayathrisrujanareddy2 months ago
class Solution:
   def checkDoorStatus(self, N):
       # code here
       l=[]
       for i in range(N):
           l.append(0)
       for i in range(1,N+1):
           ans=math.sqrt(i)
           if ans==int(ans):
               l[i-1]=1
       return l
0
abhishekpundir5264 months ago
    int *checkDoorStatus(int N) {       int *arr = new int [N];              int i = 1;              while(i*i <= N){           arr[(i*i) - 1] = 1;           i++;       }              return arr;   }
+1
badgujarsachin835 months ago
int *checkDoorStatus(int N) {
       // code here
       int *a=(int*)malloc(sizeof(int)*N);
       for(int i=0;i<=N;i++){
           int no=sqrt(i);
           if(no*no==i){
               a[i-1]=1;
           }
       }
       return a;
   }
-1
s87647 months ago
import mathclass Solution:   def checkDoorStatus(self, N):       # code here       status=[0]*N       for i in range(1,N+1):           sq=math.sqrt(i)           if sq==int(sq):               status[i-1]=1       return status               '''here if the no of factors are odd then the door will be        opened to check if the no of factors are odd just check         whether the sq of number == int(sq of number) if yes then        it will have odd factors''' 
0
s8764
This comment was deleted.
-1
chaithrarhsn7 months ago
 def checkDoorStatus(self, N):       self.N=N       c=[]       for i in range(1,self.N+1):           num=int(math.sqrt(i))           if num*num==i:               c.append(1)                          else:               c.append(0)       return c
0
Laksh Chanan8 months ago
Laksh Chanan
JAVA SOLUTIONstatic int[] checkDoorStatus(int n)     {        int[] res = new int[n];        for(int i = 1;i*i <= n;i++)            res[i*i-1] = 1;        return res;    }
-1
Pritam Majumder9 months ago
Pritam Majumder
class Solution {  public:    int *checkDoorStatus(int N) {        int *a = (int *)malloc(sizeof(int) * N);                for(int i = 1; i <= N; i++)        {            int num = (int)sqrt(i);                        /*if num is a perfect square then door is open*/            if((num*num) == i) {                /*i-1 because here loop starts from 1 but array starts from index 0 (i-1)*/                a[i-1] = 1;             }        }        return a;    }};
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.