Number Of Open Doors | Practice | GeeksforGeeks

Consider a long alley with a N number of 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: you open a door that is already closed and you close a door that is already opened. You start at one end go on altering the state of the doors till you reach the other end and then you come back and start altering the states of the doors again.
In the first go, you alter the states of doors numbered 1, 2, 3, ... , n.
In the second go, you alter the states of doors numbered 2, 4, 6...
In the third go, you alter the states of doors numbered 3, 6, 9 ...
You continue this till the Nth go in which you alter the state of the door numbered N.
You have to find the number of open doors at the end of the procedure.
 
Example 1:
Input:
N = 2
Output:
1
Explanation:
Initially all doors are closed.
After 1st go, all doors will be opened.
After 2nd go second door will be closed.
So, Only 1st door will remain Open.
Example 2:
Input:
N = 4
Output:
2
Explanation:
Following the sequence 4 times, we can
see that only 1st and 4th doors will
remain open.
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function noOfOpenDoors() which takes an Integer N as input and returns the answer.
 
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
 
Constraints:
1 <= N <= 1012
0
badgujarsachin831 month ago
int noOfOpenDoors(long long N) {
        // code here
        int res=sqrt(N); 
        return res;
    }
0
aakasshuit2 months ago
//Java Solution

        int ans =(int) Math.sqrt(N);
        return ans;
0
gayathrisrujanareddy2 months ago
class Solution:
   def noOfOpenDoors(self, N):
       # code here
       ans=math.sqrt(N)
       return  int(ans)
0
ramratan700473 months ago
# Python solution
#Divyanshu Kumar(Asansol Engineering college,ECE) Madhubani wala
def noOfOpenDoors(self, N):
        # code here
        t=math.sqrt(N)
        return int(t)
-1
sairish20014 months ago
int noOfOpenDoors(long long N) {
   return pow(N, 0.5);
}
0
lindan1236 months ago
int noOfOpenDoors(long long N) {
        // code here
        int ans= sqrt(N);
        return ans;
    }
0
sangramranshing576 months ago
class Solution {

  public:
    
      int noOfOpenDoors(long long N) {

        
        int a = sqrt(N);
               return a;

    }

};
0
as76636 months ago
return sqrt(N);
A gate will be open in the end if and only if it has an odd number of factors.
Only perfect squares have the odd number of factors.
0
euhidaman6 months ago
My JAVA solution -->
class Solution {
    static int noOfOpenDoors(Long N) {
        // code here
        return (int)Math.sqrt(N);
    }
};
0
suryabhanmaurya5717 months ago
Time Complexity - O(1)
class Solution:   def noOfOpenDoors(self, N):       # code here       sq = math.sqrt(N)       return int(sq)
 
we find sqrt for solution because if door lies between 4- 8 then ans will be 2 and 9 - 15 ans will be 3
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.