Advertisement
Guest User

Untitled

a guest
Jul 17th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. import java.util.*;
  2. public class HackerRank {
  3.  
  4. public static int steppingNumber(int input1)
  5. {
  6. //Write code here
  7. int result=0;
  8. int i=0,remainder=0;
  9. String num=String.valueOf(input1);
  10. int [] arr=new int [num.length()];
  11. while(input1>0){ //converting integer to integer array
  12. remainder=input1%10;
  13. arr[i]=remainder;
  14. input1=input1/10;
  15. i++;
  16. }
  17.  
  18. int f=0,g=1,x,y;
  19. do{
  20. x=arr[f];
  21. y=arr[g];
  22. if((x-y)==1||(y-x)==1){ //to find whether is stepping number or not
  23. result=1;
  24. f++;
  25. g++;
  26. }
  27. else {
  28. result=-1;
  29. break;
  30. }
  31.  
  32. }while(g<arr.length);
  33.  
  34. return result;
  35. }
  36.  
  37.  
  38.  
  39.  
  40. public static void main(String[] args) {
  41. Scanner in = new Scanner(System.in);
  42. int output = 0;
  43. int ip1 = Integer.parseInt(in.nextLine().trim());
  44. output = steppingNumber(ip1);
  45. System.out.println(String.valueOf(output));
  46.  
  47. in.close();
  48.  
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement