Given an array, find all the subsets

int arr[] = {1,2,3,4,5};
int level= 0;

for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
subset(arr[i]+””,i+1,arr);

}
}

private void subset(String i, int level,int[] arr) {

for (int j = level; j < arr.length; j++) {
System.out.println(i+””+arr[j]);
subset(i+””+arr[j],j+1,arr);

}

Find the all the lexical pair from the given array of strings.

lexical pairs

 

Vector<String> v = new Vector<String>();
Set<String> s = new HashSet<String>();
v.add(“Resistance”);
v.add(“Ancestries”);
v.add(“Gainly”);
v.add(“Laying”);
v.add(“test”);
v.add(“troop”);
v.add(“Amit”);
v.add(“Mait”);
v.add(“Self”);
v.add(“Taim”);
Boolean lexical = false,found = false;
System.out.println(“case 2”);

int lenght = v.size(),count = 0;

for (int i = 0; i < v.size(); i++) {
String source = v.get(i);

source = source.toLowerCase();
char[] sourceWord = source.toCharArray();
Arrays.sort(sourceWord);
source = new String(sourceWord);
//System.out.println(“source “+source);
count =0;
for (int j = i+1; j < v.size(); j++) {

String target = v.get(j);
target = target.toLowerCase();
char[] targetWord = target.toCharArray();
Arrays.sort(targetWord);
target = new String(targetWord);

if(source.equals(target)){
lexical = true;

System.out.print(v.get(i)+” “);
System.out.println(v.get(j));
continue;
// v.remove(v.get(j));

}

}
if(count>0)
System.out.println(“”);
}

if(!lexical){
System.out.println(“No such pairs”);
}

Given a matrix with each cell containing each number of candies, and a constraint that you can move only right or down, from the top left corner to the bottom right corner, find the path that gets you maximum candies.

Note: This result will print the all the possible path cost , you can fetch the path with maximum candies.

 

int matrix[][] = {{0,5,7},{7,9,1},{2,7,11}};
int rows = matrix.length,column = matrix[0].length;
System.out.println(“rows : “+rows );
System.out.println(“columns : “+column);
int TotalSum = 0,sum=0;

FindTheTotalSum(0,0,sum,rows,column,matrix);

}

private void FindTheTotalSum(int i, int j, int sum,int rows,int column,int[][] arr) {

if((i==column-1)&&(j==rows-1)){
System.out.println(sum+” sum of the path”);
}
sum = sum + arr[j][i];
if(i<column-1){
FindTheTotalSum(i+1, j, sum, rows, column,arr);
}
if(j<rows-1){
FindTheTotalSum(i, j+1, sum, rows, column,arr);
}

}

Given an array of characters, find the longest continuous non-repeating sequence of characters

Ex: aabcdefdghiajk – efdghiajk

 

char[] str = {‘a’,’a’,’b’,’c’,’d’,’e’,’f’,’d’,’g’,’h’,’i’,’a’,’j’,’k’};
int len = str.length;
System.out.println(“len “+len);
Map m = new LinkedHashMap();

for (int i = 0; i < len; i++) {
System.out.println(str[i]+” “+i);
if(m.containsKey(str[i])){
System.out.println(“found”);
i = (int) m.get(str[i]);
System.out.println(“new i”+i);

m.clear();
}else{

m.put(str[i], i);
}

}