March 17, 2018

Simple Solution 2 Scenario --(Odd or Even length ) 
Base condition &  Algo recursive(str, i, j)
  1. i==j //even len
  2. if i< j  recurve call (str, i +1,j-1)
  3. else ch[i] ==ch[j] // Extra base condition odd len

public class HelloWorld {

 static boolean ispalindrome(char ch[], int i, int j) {
  if (i == j) return true;
  if (i < j) {
   if (ch[i] != ch[j])
    return false;
   else
    return ispalindrome(ch, i + 1, j - 1);
  }
  if (ch[i] != ch[j])
   return false;
  else
   return true;
 }
 public static void main(String[] args) {
  System.out.println(ispalindrome("jatin".toCharArray(), 0, 4));
  System.out.println(ispalindrome("nitin".toCharArray(), 0, 4));
  System.out.println(ispalindrome("jatinn".toCharArray(), 0, 5));
  System.out.println(ispalindrome("nittin".toCharArray(), 0, 5));
 }
}

March 16, 2018

Josh : playtime with array

Question:Sorted Array have given to you.now a hacker swap odd index value with other odd index value.WAP that serach the element and return it's index in logn complexity.
Example: original sorted  array :{1,2,3,4,5,6,7,8}
              swapped given array:{1,4,3,2,5,8,7,6},key,5(element for searching)
              Output 4(it's index).

March 14, 2018

Print Start Pattern Using Recursion

Output :
X 
X X 
X X X 
X X X X 
---------------------
X X X X 
X X X 
X X 
X 
JAVA Code:
public class HelloWorld {

 static int count = 0;

 public static void print(int i, int j) {

  if (i <= 0 || j < 0 ) return;

  System.out.print("X ");

  print(i, j - 1);

  if (j == 0) {

   count++;

   System.out.println();

   print(i - 1, count);





  }

 }

   public static void print2(int i, int j) {

  if (i <= 0 || j < 0 ) return;

System.out.print("X ");

print2(i, j - 1);





  if (j == 0) {



   System.out.println();

   print2(i - 1, i-2);





  }

}





 public static void main(String[] args) {

  //System.out.println("Hello World2");

  print(4, 0);

  System.out.println("---------------------");



  print2(4, 3);

 }

}