Venkat Ram Reddy
BAN USERAn Algorithm geek !
// output: Stepping of 789 and 987[789, 876, 878, 898, 987]
package interviews.epic;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author venkatramreddykunta
*/
public class SteppingNo {
public static void main(String[] args) {
System.out.println("Stepping of 789 and 987"+steppingNo(789,987));
}
public static List<Integer> steppingNo(int s, int e) {
ArrayList<Integer> steppingNumbers = new ArrayList<Integer>();
int i = 0;
while (s <= e) {
if (isStepping(s))
steppingNumbers.add(s);
s++;
}
return steppingNumbers;
}
// 9
// 9876
private static boolean isStepping(int i) {
if(i<=9 && i>=0)
return true;
if(Math.abs(i%10-(i/10)%10)==1)
return isStepping(i/10);
else
return false;
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package interviews.epic;
/**
*
* @author venkatramreddykunta
*/
public class MatrixLevel {
public static void main(String[] args) {
// TODO Auto-generated method stub
char[][] c = {{'c','i','p','e'},{'r','n','k','u'},{'u','o','w','o'},{'l','e','s','y'}};
int level=0;
printTopLevels(c,level);
}
// c i p e < - start here
// r n k u
// u o w o
// l e s y
private static void printTopLevels(char[][] c, int level) {
printCurrentLevel(c,level);
}
// c i p e < - start here
// r u
// u o
// l e s y
private static void printCurrentLevel(char[][] c, int level) {
int i=0,lastIndex=c.length-level-1,startIndex=level;
if(level==c.length/2)
return;
// print top left <-top right
// i p e < - start here
for(i=lastIndex;i>startIndex;i--){
System.out.print(c[level][i]);
}
// c
// r
// u
// print top left - > bottom left
for(i=startIndex;i<lastIndex;i++){
System.out.print(c[i][level]);
}
// l e s
// print bottom left -> bottom right
for(i=startIndex;i<lastIndex;i++){
System.out.print(c[lastIndex][i]);
}
// u
// o
// y
// print bottom right to top right
for(i=lastIndex;i>startIndex;i--){
System.out.print(c[i][lastIndex]);
}
printTopLevels(c,level+1);
}
}
- Venkat Ram Reddy April 20, 2015