| |
GeekInterview.com > Interview Questions > J2EE > Core Java
| Print | |
Question: Jumble Array Values
Answer: An array A is of size N. Write a java method that jumbles all values in the array. All possible permutations must be possible and chances for each possible permutation must be equal. For example, size of array A is 3. A[0]=x, A[1]=y, A[2] =z. Possible permutations are 3! After jumbling elements, there would be equal chances for each of xyz,yxz,... to be in the array elements.
|
| June 06, 2009 06:42:05 |
#6 |
| vijusatya |
Member Since: May 2009 Total Comments: 3 |
RE: Jumble Array Values |
public void jumble() { String a=new String{"A","B","C"}; ArrayList ar=new ArrayList(); for (i=0;i<=arr.length;i++) { String str=new String(a[i]); for(j=0;j<=arr.length;j++) { if(!(a[i].equals(a[j]))) str=str+a[j];
} if(!ar.contains(str) { System.out.println(str); ar.add(str); } } |
| |
Back To Question | |