Pattern Jumping | Practice | GeeksforGeeks

A frog starts at the point 0. In his first turn, he can make a jump of 1 unit. Now for all consequent turns, if the frog is currently at a distance x (from the start), his jump will take him x units forward. Given a leaf at a distance N, you have to find if the frog can reach that leaf or not.
 
Example 1:
Input:
N = 3
Output:
False
Explanation:
The frog can't reach the position 3.
Example 2:
Input:
N = 2
Output:
True
Explanation:
The frog will jump to position 1 in
the first jump. Now, he is at a distance
of 1 from the start, so he cam jump 1m.
So, he reaches the point 2.
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function canJump() which takes an Integer N as input and return "True" if the frog can reach N else return "False".
 
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
 
Constraints:
1 <= N <= 1018
0
mayank20213 months ago
C++ : 0.0/18.4 string canJump(long long N) {              long long c_jump=1, jump=0;       while(jump<N)        {            jump=jump+c_jump;            c_jump=jump;                    }                if(jump==N) return "True";        else return "False";          }
+1
ruchitchudasama1235 months ago
 public:
    string canJump(long long N) {
        long long int a=log2(N);
        // cout<<a<<endl;
        a=pow(2,a);
        if(a==N)return "True";
        return "False";
    }
0
kashyapratiksha095 months ago
class Solution {
    static String canJump(Long N) {
        
         // code here
         
        if((N & (N-1) )== 0)
        return "True";
        
        else return "False";
    }
};
-2
adityans1232410 months ago
A frog initially placed at origin can jump along the horizontal axis.
The frog will make N jumps.
Let's define a function in the order in which the frog makes his jump. il.com
Initially from FO=0, and the i-th Jump (1s i SN) at coordinate Fi-Fi-1+XI-1.
How many times will the frog be at the coordinate that is at most ?
Input Format
The first line contains two integers N and K.
The next line contains N Integers XI.
• 1sNs100@gma
1sxis 100
1s Ks10000
Output Format
Print the result.
0
Sumanyu2 years agoCan someone explain what is being asked?Reply Open ExternallyShow 1 RepliesLoading...
Sumanyu
Can someone explain what is being asked?
0
Md Hayatuddin2 years agoCorrect Answer.Correct AnswerExecution Time:0.01        string s1="True";        string s2="False";        if (N && !(N &(N-1)))        return s1;return s2;Reply Open ExternallyShow 0 RepliesLoading...
Md Hayatuddin
Correct Answer.Correct AnswerExecution Time:0.01        string s1="True";        string s2="False";        if (N && !(N &(N-1)))        return s1;return s2;
0
Ambikesh Sarangi2 years agoPython SolutionExecution time -  0.02st = int(input())for _ in range(t):    x = int(input())    if x == 1 or (x and (not(x & (x - 1)))) is True:        print('True')    else:        print('False')Reply Open ExternallyShow 0 RepliesLoading...
Ambikesh Sarangi
Python SolutionExecution time -  0.02s
t = int(input())for _ in range(t):    x = int(input())    if x == 1 or (x and (not(x & (x - 1)))) is True:        print('True')    else:        print('False')
0
Sagar Singh2 years agofor i in range(int(input())):    n = int(input())    b = False    if n == 1 or n == 0:        print("True")    else:        while n>1:            if n%2 == 0:                b = True                n = n//2            else:                b = False                break        if b:            print("True")        else:            print("False")Reply Open ExternallyShow 0 RepliesLoading...
Sagar Singh
for i in range(int(input())):    n = int(input())    b = False    if n == 1 or n == 0:        print("True")    else:        while n>1:            if n%2 == 0:                b = True                n = n//2            else:                b = False                break        if b:            print("True")        else:            print("False")
0
Vivek pandey2 years agosimple cppsolution :#include <iostream>using namespace std;int main() {//codeint t; cin>>t;while(t--){    int n; cin>>n;    if((n & (n-1))==0){        cout<<"True\n";    }    else{        cout<<"False\n";    }}return 0;}Reply Open ExternallyShow 0 RepliesLoading...
Vivek pandey
simple cppsolution :
#include <iostream>using namespace std;
int main() {//codeint t; cin>>t;while(t--){    int n; cin>>n;    if((n & (n-1))==0){        cout<<"True\n";    }    else{        cout<<"False\n";    }}return 0;}
0
Alokit2 years agoWe have to check n is power of 2 or not . if it is then ans is yes else ans is no.#include <bits stdc++.h="">using namespace std;int main() {//codeint t,n;cin>>t;while(t--){    cin>>n;    if((n&(n-1))==0) cout<<"True\n";    else cout<<"False\n";}return 0;}Reply Open ExternallyShow 0 RepliesLoading...
Alokit
We have to check n is power of 2 or not . if it is then ans is yes else ans is no.#include <bits stdc++.h="">using namespace std;
int main() {//codeint t,n;cin>>t;while(t--){    cin>>n;    if((n&(n-1))==0) cout<<"True\n";    else cout<<"False\n";}return 0;}
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.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems 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.