Absolute Difference of 1 | Practice | GeeksforGeeks

Given an array arr of size n. Print all the numbers less than kandshould be such that the difference between every adjacent digit should be 1in the array.
Note: Return empty list if no such number present, driver code will automatically print -1.
Example 1:
Input:n = 8, k = 54arr[] = {7, 98, 56, 43, 45, 23, 12, 8}Output: 43 45 23 12Explanation: 43 45 23 12all these numbers have adjacent digits diff as 1 and they areless than 54.
Example 2:
Input:n = 10, k = 1000arr[] = {87, 89, 45, 235, 465, 765, 123, 987, 499, 655}Output: 87 89 45 765 123 987
Your Task:You don't need to read input or print anything. Your task is to complete the functiongetDigitDiff1AndLessK()which takes the array of integersarr, nandkas parameters and returns a list of integerdenoting the answer.
Expected Time Complexity:O(n)Expected Auxiliary Space:O(1)
Constraints:1 <= n <= 1071 <= k, arr[i] <= 1018
0
codewithshoaib193 weeks ago
long[] getDigitDiff1AndLessK(long[] arr, int n, long k) {       // code here       ArrayList<Long>list=new ArrayList<>();       for(int i=0;i<n;i++){           if(arr[i]<k){               if(check(arr[i])){                   list.add(arr[i]);               }           }       }       long[]ans=new long[list.size()];       for(int i=0;i<list.size();i++){           ans[i]=list.get(i);       }       return ans;   }   public static boolean check(long x){       String s=Long.toString(x);       if(s.length()==1){           return false;       }       else if(s.length()==2){           int y=Integer.parseInt(s.substring(0,1));           int z=Integer.parseInt(s.substring(1,2));           if(Math.abs(y-z)!=1){               return false;           }       }        for(int i=1;i<s.length()-1;i++){             int y = Integer.parseInt(s.substring(i - 1, i));           int z = Integer.parseInt(s.substring(i, i + 1));           int zz = Integer.parseInt(s.substring(i + 1, i + 2));           if(Math.abs(z-y)!=1 || Math.abs(z-zz)!=1 ){               return false;           }        }        return true;   }
0
hasnainraza1998hr1 month ago
C++, 0.4
vector<long long> getDigitDiff1AndLessK(long long *arr, int n, long long k) {       vector<long long> v;       for(int i=0;i<n;i++){           if(arr[i]<k && arr[i]>9){               int num=arr[i];               bool flag=true;               while(num>9){                   int d1=num%10;                   num=num/10;                   int d2=num%10;                   if(abs(d1-d2)!=1){                       flag=false;                       break;                   }               }               if(flag==true)                   v.push_back(arr[i]);           }       }       return v;   }
0
dangrio2 months ago
class Solution{ public:   bool checkDiffAs1(int n){       int prevDigit=n%10, digit;       n /= 10;       if(!(n>0)) return false;       while(n>0){           digit = n%10;           if(abs(digit-prevDigit)==1){               n /= 10;               prevDigit = digit;           }else return false;       }       return true;   }   vector<long long> getDigitDiff1AndLessK(long long *arr, int n, long long k) {       // code here       vector<long long> ans;       for(int i=0;i<n;i++){           if(arr[i]<k){               if(checkDiffAs1(arr[i])) ans.push_back(arr[i]);           }       }       return ans;   }}; 
0
arjunaryagupta2033 months ago
       #Python Code
       li=[]       for i in range(len(arr)):           if arr[i]<k:               n=arr[i]               prev=n%10               n=n//10               while n>0:                   curr=n%10                   if abs(prev-curr)!=1:                       flag=1                       break                   n=n//10                   if n==0:                       li.append(arr[i])                   prev=curr           return li     
0
codersheaven693 months ago
Python solution 
 
returnArray=[]       for element in arr:           if(element<k and element>9):               itis = True               changed=element               while(changed>9):                   last = changed%10                   changed=changed//10                   first = changed%10                   if(abs(first-last)!=1):                       itis=False                       break               if(itis == True):                   returnArray.append(element)              return returnArray
0
abhishekpundir5263 months ago
  bool check(long long var){          string s = to_string(var);          if(s.length() == 1){         return false;     }          if(abs(s[1] - s[0]) != 1){         return false;     }          if(abs(s[s.length() - 1] - s[s.length() - 2]) != 1){         return false;     }               for(int i = 1;i < s.length()-1; i++){         if(abs(s[i] - s[i-1]) != 1 || abs(s[i] - s[i+1]) != 1){             return false;         }     }          return true; }      vector<long long> getDigitDiff1AndLessK(long long *arr, int n, long long k) {       vector<long long> ans;              for(int i = 0;i < n; i++){           if(arr[i] < k){               if(check(arr[i])){                   ans.push_back(arr[i]);               }           }       }              return ans;   }
+1
badgujarsachin835 months ago
vector<long long> getDigitDiff1AndLessK(long long *arr, int n, long long k) {
        vector<long long > v;
        for(int i=0;i<n;i++){
            if(arr[i]>9 && arr[i]<k){
                int temp=arr[i];
                bool ans=true;
                while(temp>9){
                    int a=temp%10;
                    temp/=10;
                    int b=temp%10;
                    if(abs(a-b)!=1){
                        ans=false;
                        break;
                    }
                }
                if(ans==true){
                    v.push_back(arr[i]);
                }
            }
        }
        if(v.empty()){
            v.push_back(-1);
        }
        return v;
    }
-1
mrshubhamkr20007 months ago
I don't know why my first for loop is not iterating. Only first element gets checked for the rest of the code after that loop. Here is my code: 
int sum=0,c=0,z=0;       Arrays.sort(arr);       for(int i=0; i<n; i+=1){           if(arr[i]<k && arr[i]>9){               String s=String.valueOf(arr[i]);               for(int j=0;j<s.length()-1;j++){                   sum+=Math.abs(s.charAt(j)-s.charAt(j+1));               }               if(sum==((s.length()-1)*1)){                   arr[z]=arr[i];                   z++;                   c++;               }           }else{               continue;           }       }       long a[]=new long[c];       for(int i=0;i<c;i++){           a[i]=arr[i];       }       return a;
0
Subh Arora8 months ago
Subh Arora
@mod please correct the python driver's code....
-2
Mohd Usman Barkaati8 months ago
Mohd Usman Barkaati
Correct Answer C++Execution Time : 0.4
 https://uploads.disquscdn.c...
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.