Thursday, April 25, 2013

TechGig Solution: Crossing the river

Crossing the river:
Here is link of the question

I started with score of 10 then i stuck at 80 and 90 and finally i got score of 100. Here is my solution if you have better solution please share with us.

Download the below Code: Click Here

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;



public class CrossingRiver {

private static int previousMax;

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String input[] = {""};
System.out.println(bridge(input));
}

@SuppressWarnings("unchecked")
public static int bridge(String[] input1)
{
System.out.println(input1.length);
if(input1.length==0)
return 0;
int arr[];

arr=new int[2*input1.length];
StringTokenizer st;
for(int i=0,j=0;i {
st = new StringTokenizer(input1[i],"#");
while(st.hasMoreElements())
{
arr[j] = Integer.valueOf((String)st.nextElement());
j++;
}

}
for(int j:arr)
System.out.println(j);

Set set = new HashSet();
for(int j=0;j {
boolean b = set.add(arr[j]);
if(b==false)
return -1;
}
set.clear();
for(int j=1;j {
boolean b = set.add(arr[j]);
if(b==false)
return -1;
}


if(input1.length==1)
{
if(input1[0].equals(""))
return 0;

return 1;
}
else
{
int cityFirst;
int citySecond;
int cityFirstInside;
int citySecondInside;
int start;
int end;
int previous1;
boolean flag=false;
int max=0;

ArrayList al = new ArrayList();

for(int i=0;i {
cityFirst = arr[i];
citySecond = arr[i+1];
previous1 = citySecond;

for(int j=0;j {
cityFirstInside = arr[j];
citySecondInside = arr[j+1];
//1#2,2#3,3#1,4#4,5#7,6#5,7#6

if(cityFirst==cityFirstInside && citySecond==citySecondInside)
{
System.out.println("--------------------"+cityFirst+"----"+cityFirstInside+"------------");
max++;
}

else if(cityFirstInside>cityFirst)
{

if(previous1 {
max++;
System.out.println("happy"+cityFirstInside);
previous1=citySecondInside;

}
}

else if(cityFirstInside {
System.out.println("C1="+cityFirstInside+" C2="+citySecondInside+" max="+max);
if(j==0)
{
max++;
al.add(citySecondInside);
}
else
{


for(int p=0;p {
if(!(citySecondInside>(Integer)al.get(p)))
{
System.out.println("hhh");
flag=true;
break;
}

}
System.out.println(flag);
if(!flag)
{

max++;
al.add(citySecondInside);
// System.out.println("C1="+cityFirstInside+" C2="+citySecondInside+" max="+max);
}
flag=false;
}
}
}

al.clear();

if(max>previousMax)
previousMax = max;
max=0;
}
}
return previousMax;
}



Download the above Code: Click Here

Tuesday, April 2, 2013

Java: Towers Of Hanoi

/*
Towers Of Hanoi.
There are three pegs: A, B and C. There are n disks. All disks are different in size.
The disks are initially stacked on peg A so that they increase in size from the top to the bottom.
The goal is to transfer the entire tower from the A peg to the C peg.
One disk at a time can be moved from the top of a stack either to an empty peg or to
a peg with a larger disk than itself on the top of its stack.

The method should return a sequence of disk moves, each move is a String with two letters (A, B or C)
corresponding to the peg the disk moves from and the peg it moves to.
For example, the move "AC" means that a top disk from peg A should be moved to peg C.
*/

  1. import java.util.ArrayList;

  2. importjava.util.List;

  3.  

  4.  

  5. publicclassTowersOfHanoi {

  6.  

  7.     privatestaticList<String> list= newArrayList<String>();

  8.     staticString a="A";

  9.     staticString c="C";

  10.     staticString b="B";

  11.     staticString s="";

  12.     publicstaticvoidmove(intn, intstartPole, intendPole) {

  13.         if(n== 0){

  14.           return;

  15.         }

  16.         intintermediatePole = 6- startPole - endPole;

  17.         move(n-1, startPole, intermediatePole);

  18.         System.out.println("Move "+n + " from "+ startPole + " to "+endPole);

  19.         if(startPole==1)

  20.             s=s+a;

  21.         if(startPole==2)

  22.             s=s+b;

  23.         if(startPole==3)

  24.             s=s+c;

  25.         if(endPole==1)

  26.             s=s+a;

  27.          

  28.         if(endPole==2)

  29.             s=s+b;

  30.         if(endPole==3)

  31.             s=s+c;

  32.          

  33.         list.add(s);

  34.         s="";

  35.         move(n-1, intermediatePole, endPole);

  36.       }

  37.        

  38.     publicstaticList<String> transferFromAtoC(intn) {

  39.         /*

  40.           Towers Of Hanoi.

  41.           There are three pegs: A, B and C. There are n disks. All disks are different in size.

  42.           The disks are initially stacked on peg A so that they increase in size from the top to the bottom.

  43.           The goal is to transfer the entire tower from the A peg to the C peg.

  44.           One disk at a time can be moved from the top of a stack either to an empty peg or to

  45.           a peg with a larger disk than itself on the top of its stack.

  46.  

  47.           The method should return a sequence of disk moves, each move is a String with two letters (A, B or C)

  48.           corresponding to the peg the disk moves from and the peg it moves to.

  49.           For example, the move "AC" means that a top disk from peg A should be moved to peg C.

  50.          */

  51.          

  52.         move(n, 1, 3);

  53.         returnlist;

  54.     }

  55.       publicstaticvoidmain(String[] args) {

  56.           List l1 = newArrayList();

  57.         transferFromAtoC(1);

  58.         System.out.println(list);

  59.       }

  60. }


Download Link: Click Here

Java: Set Intersection

/*
Please implement this method to
return a Set equal to the intersection of the parameter Sets
The method should not chage the content of the parameters.
*/

  1. import java.util.HashSet;

  2. import java.util.Iterator;

  3. import java.util.NoSuchElementException;

  4. import java.util.Set;


  5. public class SetIntersection {

  6. /**

  7. * @param args

  8. */


  9. private static Set<Object> s1;

  10. private static Set<Object> s2;

  11. public static Set<Object> getIntersection(Set<Object> a, Set<Object> b) {

  12. /*

  13. Please implement this method to

  14. return a Set equal to the intersection of the parameter Sets

  15. The method should not chage the content of the parameters.

  16. */


  17. s1 =a;

  18. s2 = b;

  19. Object ob;

  20. Set<Object> s3 = new HashSet<Object>();



  21. Iterator<Object> it1 = s1.iterator();

  22. Iterator<Object> it2 = s2.iterator();


  23. System.out.println("Set 1");

  24. while(it1.hasNext())

  25. {

  26. System.out.println(it1.next());

  27. }

  28. System.out.println("Set 2");

  29. while(it2.hasNext())

  30. {

  31. System.out.println(it2.next());

  32. }


  33. it1 = s1.iterator();

  34. while(it1.hasNext())

  35. {

  36. System.out.println("hi");

  37. try

  38. {

  39. ob = it1.next();

  40. //System.out.println(ob);

  41. if(s2.contains(ob))

  42. {

  43. // System.out.println("contain element is "+ob);

  44. s3.add(ob);

  45. }

  46. }

  47. catch(ClassCastException cse)

  48. {

  49. cse.printStackTrace();

  50. }

  51. catch(NoSuchElementException ne)

  52. {

  53. ne.printStackTrace();

  54. }



  55. }


  56. return s3;

  57. }



  58. public static void main(String[] args) {

  59. // TODO Auto-generated method stub

  60. Set<Object> s1 = new HashSet<Object>();

  61. Set<Object> s2 = new HashSet<Object>();

  62. s1.add(3);

  63. s1.add(4);

  64. s1.add(5);

  65. s1.add(7);

  66. s1.add("hi");

  67. s1.add(3.4);

  68. s2.add(1);

  69. s2.add(2);

  70. s2.add(3);

  71. s2.add(7);

  72. s2.add("hi");

  73. s2.add(new Object());

  74. Set<Object> s3 = getIntersection(s1,s2);

  75. Iterator<Object> it = s3.iterator();

  76. System.out.println("Set 3");

  77. while(it.hasNext())

  78. {

  79. System.out.println(it.next());

  80. }

  81. }

  82. }


Download Link: Click Here

Java: Prime Number List

/*
Please implement this method to
return a list of all prime numbers in the given range (inclusively).
A prime number is a natural number that has exactly two distinct natural number divisors, which are 1 and the prime number itself.
The first prime numbers are: 2, 3, 5, 7, 11, 13
*/

 

  1. import java.util.List;

  2. import java.util.ArrayList;


  3. public class PrimeNumber {

  4. /**

  5. * @param args

  6. */

  7. public static void main(String[] args) {

  8. // TODO Auto-generated method stub

  9. System.out.println(getPrimeNumbers(481,500));

  10. }

  11. public static List<Integer> getPrimeNumbers(int from, int to) {

  12. /*

  13. Please implement this method to

  14. return a list of all prime numbers in the given range (inclusively).

  15. A prime number is a natural number that has exactly two distinct natural number divisors, which are 1 and the prime number itself.

  16. The first prime numbers are: 2, 3, 5, 7, 11, 13

  17. */


  18. List<Integer> list = new ArrayList<Integer>();

  19. boolean b=false;

  20. for(int j=from;j<=to;j++)

  21. {

  22. b=false;

  23. System.out.println("from "+from);

  24. int i=2;

  25. for(;i<j/2;i++)

  26. {


  27. if(j%i==0)

  28. {

  29. b=true;

  30. break;

  31. }



  32. }

  33. System.out.println("j= "+j);

  34. System.out.println("b= "+b);

  35. if(b==false && j!=1 && j!=0)

  36. list.add(j);


  37. }


  38. System.out.println(list);

  39. return list;

  40. }

  41. }


 

Download Link: Click Here

Java: Positive Number Array

/*
Please implement this method to
return a new array with only positive numbers from the given array.
The elements in the resulting array shall be sorted in the ascending order.

*/

 

  1. public class PositiveNumberArray {

  2. /**

  3. * @param args

  4. */

  5. public static void main(String[] args) {

  6. // TODO Auto-generated method stub

  7. int a[] = {3,5,6,30,-3,-3,-3,5,3,-3};

  8. for(int i : retainPositiveNumbers(a))

  9. System.out.println(i);

  10. }


  11. public static int[] retainPositiveNumbers(int[] a) {

  12. /*

  13. Please implement this method to

  14. return a new array with only positive numbers from the given array.

  15. The elements in the resulting array shall be sorted in the ascending order.


  16. */


  17. int len=0;

  18. int b[];

  19. for(int i=0;i<a.length;i++)

  20. {

  21. if(a[i]>0)

  22. len++;

  23. }



  24. b = new int[len];

  25. for(int i=0,j=0;i<a.length;i++)

  26. {

  27. if(a[i]>0)

  28. {

  29. b[j]=a[i];

  30. j++;

  31. }

  32. }


  33. int temp;

  34. for(int i=0;i<b.length-1;i++)

  35. {

  36. for(int j=i+1;j<b.length;j++)

  37. {

  38. if(b[j]<b[i])

  39. {

  40. temp=b[j];

  41. b[j]=b[i];

  42. b[i]=temp;

  43. }

  44. }


  45. }


  46. return b;

  47. }

  48. }


Download Link: Click Here

Java: String Pailndrome

/*
Definition: A palindrome is a string that reads the same forward and backward.
For example, "abcba" is a palindrome, "abab" is not.
Please implement this method to
return true if the parameter is a palindrome and false otherwise.
*/

 

  1. public class Palindrome {

  2. /**

  3. * @param args

  4. */

  5. public static void main(String[] args) {

  6. // TODO Auto-generated method stub

  7. System.out.println(isPalindrome("cbc"));

  8. }

  9. public static boolean isPalindrome(String s) {

  10. /*

  11. Definition: A palindrome is a string that reads the same forward and backward.

  12. For example, "abcba" is a palindrome, "abab" is not.

  13. Please implement this method to

  14. return true if the parameter is a palindrome and false otherwise.

  15. */


  16. String temp;

  17. int len = s.length();

  18. char []arr = new char[len];

  19. for(int i=len-1,j=0;i>=0;i--,j++)

  20. {


  21. arr[j]=s.charAt(i);

  22. }


  23. temp = new String(arr);

  24. System.out.println("temp "+temp+"\ns "+s);

  25. if(temp.equals(s))

  26. return true;

  27. return false;

  28. }

  29. }


Download Link: Click Here

Java: Get Sum of Numbers in a String

Download Link: http://txtup.net/dQNDr

/*

Source Question: betterprogrammer.com
Please implement this method to
return the sum of all integers found in the parameter String. You can assume that
integers are separated from other parts with one or more spaces (' ' symbol).
For example, s="12 some text 3 7", result: 22 (12+3+7=22)
*/

 

  1. import java.util.regex.Matcher;

  2. import java.util.regex.Pattern;

  3. public class GetSumOfNumbers {

  4. public static void main(String[] args) {


  5. System.out.println(getSumOfNumbers("ss34 gbb 65"));

  6. }


  7. public static int getSumOfNumbers(String s) {

  8. /*

  9. Please implement this method to

  10. return the sum of all integers found in the parameter String. You can assume that

  11. integers are separated from other parts with one or more spaces (' ' symbol).

  12. For example, s="12 some text 3 7", result: 22 (12+3+7=22)

  13. */

  14. //s="kkk 4 jj 9d kj 99 kjdj 9 kj 9";

  15. Pattern p = Pattern.compile("\\d+");

  16. Matcher m = p.matcher(s);

  17. int sum=0;

  18. while(m.find())

  19. {

  20. //System.out.println(m.start()+" "+m.end());

  21. sum = sum + Integer.parseInt(s.substring(m.start(), m.end()));

  22. }


  23. return sum;

  24. }

  25. }


Download Link: http://txtup.net/dQNDr