public static int findMaxNum(int num){
        int base = num;
        int first, next;
        Stack<Integer> st = new Stack<Integer>();

        if( num <= 20 ){
            return num;
        }

        while (base>0){ //
            st.push(base%10);
            base /=10;
        }

        next = st.pop();
        base = 0;

        do{
            first = next;
            next = st.pop();

            if( isValid(first,next) ){
                base = base * 10 + first; //update base!
                if( st.isEmpty()){
                    base = base*10 + next;
                }
            }
           else{//we must find a valid value!
                int v = getClosestNum(first,next);
                first = v/10;
                next = v%10;
                base = base*10 + first;
                base = base*10 + next;
                //if( next>0 ) next = 9; //here no need now!
                if (st.size()>0){
                    for(int i=0;i<st.size();i++) {
                        base = base * 10 + next;
                    }
                    st.clear();
                }
            }

        }while(!st.isEmpty());


        return base;
    }