Jumping Numbers | Practice | GeeksforGeeks

Given a positive number X. Find the largest Jumping Number smaller than or equal to X. 
Jumping Number: A number is called Jumping Number if all adjacent digits in it differ by only 1. All single digit numbers are considered as Jumping Numbers. For example 7, 8987 and 4343456 are Jumping numbers but 796 and 89098 are not.
 
Example 1:
Input:
X = 10
Output:
10
Explanation:
10 is the largest Jumping Number
possible for X = 10.
Example 2:
Input:
X = 50
Output:
45
Explanation:
45 is the largest Jumping Number
possible for X = 50.
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function jumpingNums() which takes an Integer X as input and returns the largest Jumping Number less than or equal to X.
 
Expected Time Complexity:  O(k), where k is no of jumping numbers
Expected Auxiliary Space: O(k), where k is no of jumping numbers 
 
Constraints:
1 <= X <= 109
0
mehbulislam812 days ago
Can someone optimize this code ??
class Solution:   def bfs(self,n,i,result):       queue=[]       queue.append(i)       while (queue!=None):           i=queue[0]           if i <= n:               result.append(i)               last_dig=i%10               if last_dig==0:                   queue.append(((i*10)+(last_dig+1)))               elif last_dig==9:                   queue.append(((i*10)+(last_dig-1)))               else:                   queue.append(((i*10)+(last_dig+1)))                   queue.append(((i*10)+(last_dig-1)))   def jumpingNums(self, X):       # code here        if X<=10:           return X       result=[]       for i in range(10):           if i<=X:               self.bfs(X,i,result)           else:               break       maximum=-1       for number in result:           maximum=max(number,maximum)       return maximum
0
karthikappana4842 weeks ago
can someone optimize this python code
def commondiff(num):   jumpingnumber=None   num=str(num)   d=1   for x in range(len(num)-1):       d=abs(int(num[x])-int(num[x+1]))              if d==1:           jumpingnumber=True       else :           jumpingnumber=False   if jumpingnumber==True:       return 1   else:       return 0                     
class Solution:   def jumpingNums(self, X):       A=[]       for i in range(0,X+1):           check=commondiff(i)           if check==1:               A.append(i)       return(A[-1])
0
u6twqtxu7rv0cxwmqxqnrtghetxkoua4ogrydvxe2 months ago
Can some optimize this C++ code:
 
long long jumpingNums(long long X) {
        for(int long long i =X;i>0;i--){
            int long long n = i;
            int l = n%10;
            int flag  = 1;
            int temp;
            if(l==9){
                 temp = 8;
            }
            else {
                temp =l+1;
                
            }
            while(n>0){
                int r = n%10;
                n/=10;
                if (abs(temp-r)!=1){
                    flag = 0;
                    break;
                }
                temp = r;
            }
            if (flag){
                return i;
            }
        }
    }
0
gsk70514 months ago
 long long n,i,j,count,k;       for(i=X;i>9;i--)       {           vector<long long>v;           n=i;           while(n!=0)           {               k=n%10;               v.push_back(k);               n=n/10;           }           count=0;           for(j=0;j<v.size()-1;j++)           {             if(v[j]-1==v[j+1]||v[j]+1==v[j+1])             {                 count=count+1;             }           }           if(count==v.size()-1)           {               return i;           }           v.clear();                  }
0
krushna65 months ago
More time complexity
 int jump(int n)   {   string s = to_string(n);   bool flag = true;   for(int i=0;i<s.length()-1;i++)   {       if (abs(s[i] - s[i+1]) != 1)       {           flag = false;       }   }   return flag;   }      long long jumpingNums(long long X)   {       while(true)       {           if(jump(X))               return X;           X--;       }   }
0
dreamvivek65 months ago
// this qsn is same as Stepping Number
 
 
 long long maxm=INT_MIN;       for(int i=0; i<=9; i++)       {           
       queue<long long >q;   q.push(i);      while(q.empty()==false)   {       long long stepnum=q.front();       q.pop();             if(stepnum<=x)       {          maxm=max(maxm,stepnum) ;               if(i==0 || stepnum>x)        continue;               int lastdigit=stepnum%10;             int stepnumA=stepnum*10+(lastdigit-1);      int stepnumB=stepnum*10+(lastdigit+1);              if(lastdigit==0)       q.push(stepnumB);       else if(lastdigit==9)       q.push(stepnumA);       else       {            q.push(stepnumA);           q.push(stepnumB);       }          }       }       }       return maxm;
+1
yoursnataraj6 months ago
  long long jd[4001];
        queue<long long> q;
        jd[0]=0;
        long long i;
        if(X <= 10) return X;
        for(i=1;i<10;i++)
        {
            jd[i]=i;
            q.push(i);
        }
        long long num;
        while(q.empty()==false)
        {
            if(q.front() > X) break;
            num=q.front();
            int t1=num%10;
            if(t1==0)
            {
                jd[i++]=num*10+t1+1;
                q.push(jd[i-1]);
            }
            else if(t1==9)
            {
                jd[i++]=num*10+t1-1;
                q.push(jd[i-1]);
            }
            else
            {
                jd[i++]=num*10+t1-1;
                q.push(jd[i-1]);
                jd[i++]=num*10+t1+1;
                q.push(jd[i-1]);
            }
            q.pop();
        }
        return num;
    }
https://youtu.be/CKBGkIDeTYA
0
muhammedrameez141220017 months ago
the idea of using queue is all u need to solve the problem
 
long long maxa=INT_MIN;
   long long jumpingNums(long long x) {
       // code here
      
       for(int i=1;i<=9;i++)
       {
           queue<long long>q;
           q.push(i);
           
           while(!q.empty())
           {
               long long num=q.front();
               q.pop();
               
               if(num<=x)
               {
                   maxa=max(num,maxa);
                   
                   long long lastdig=num%10;
                   
                   if(lastdig==0)
                   q.push((num*10)+(lastdig+1));
                   
                   else if(lastdig==9)
                   q.push((num*10)+(lastdig-1));
                   
                   else{
                       q.push((num*10)+(lastdig-1));
                       q.push((num*10)+(lastdig+1));
                   }
               }
           }
       }
       
       
       return maxa;
     
       
       
   }
0
Ajay Venkatesh Gunturu8 months ago
Ajay Venkatesh Gunturu
long long jumpingNums(long long n) {        // code here        int firstDigit, numOfDigits = 0;    long long ans;     //find the first digit of the given number and the number of digits    long long i = n;    while(i > 0){    firstDigit = i % 10;    i = floor(i/10);    numOfDigits++;    }     if(n < 11){        //if given number is less than 11 then return the number itself as the answer    ans = n;    }else{    ans = jumpingNumber(firstDigit, numOfDigits-1, n);    }    return ans;    }     long long jumpingNumber(long long num, int numOfDigits, long long n){    long long ans, currentNum = num;      for (int i = 0; i < numOfDigits; ++i)    currentNum *= 10;     if(currentNum <= n){    if(numOfDigits == 0) return num;     int currentDigit = num%10;     if(currentDigit == 0){    ans = jumpingNumber(num*10 + (currentDigit + 1), numOfDigits-1, n);    if(ans != -1) return ans;     }    else if(currentDigit == 9) {    ans = jumpingNumber(num*10 + (currentDigit - 1), numOfDigits-1, n);    if(ans != -1) return ans;     }    else{    ans = jumpingNumber(num*10 + (currentDigit + 1), numOfDigits-1, n);    if(ans != -1) return ans;    ans = jumpingNumber(num*10 + (currentDigit - 1), numOfDigits-1, n);    if(ans != -1) return ans;    }     if(num < 10){   //if current number is the first digit and answer is not found yet    if(currentDigit > 1){        //if the first digit is in 2 - 9 then reduce it by one    ans = jumpingNumber(num-1, numOfDigits, n);    if(ans != -1) return ans;    }else{        //if the first digit is 1 find answer for numbers starting with 9 and one less digit    ans = jumpingNumber(9, numOfDigits-1, n);    if(ans != -1) return ans;    }    }     }    return -1;    }
0
wallflower10 months ago
wallflower
Java bfs solutionhttps://ide.geeksforgeeks.o...
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.