RS
BAN USERimport java.util.ArrayList;
public class Subset_of_set {
public static ArrayList<ArrayList<Integer>> getSubset(ArrayList<Integer> set)
{
ArrayList<ArrayList<Integer>> allsubset = new ArrayList<ArrayList<Integer>>();
int max = 1<<set.size();
for(int i = 0;i<max;i++)
{
ArrayList<Integer> subset = converInttoSet(i,set);
allsubset.add(subset);
}
return allsubset;
}
private static ArrayList<Integer> converInttoSet(int i,ArrayList<Integer> set)
{
ArrayList<Integer> subset= new ArrayList<Integer>();
int index =0;
for(int k = i;k>0; k>>=1)
{
if((k&1)>0)
subset.add(set.get(index));
index++;
}
return subset;
}
public static void main(String[] args) {
ArrayList<Integer> a = new ArrayList<>();
for (int i = 0; i < 5; i++)
{
a.add(i+1);
}
ArrayList<ArrayList<Integer>> result = getSubset(a);
System.out.println(result);
}
}
import java.util.ArrayList;
public class Subset_of_set {
public static ArrayList<ArrayList<Integer>> getSubset(ArrayList<Integer> set)
{
ArrayList<ArrayList<Integer>> allsubset = new ArrayList<ArrayList<Integer>>();
int max = 1<<set.size();
for(int i = 0;i<max;i++)
{
ArrayList<Integer> subset = converInttoSet(i,set);
allsubset.add(subset);
}
return allsubset;
}
private static ArrayList<Integer> converInttoSet(int i,ArrayList<Integer> set)
{
ArrayList<Integer> subset= new ArrayList<Integer>();
int index =0;
for(int k = i;k>0; k>>=1)
{
if((k&1)>0)
subset.add(set.get(index));
index++;
}
return subset;
}
public static void main(String[] args) {
ArrayList<Integer> a = new ArrayList<>();
for (int i = 0; i < 5; i++)
{
a.add(i+1);
}
ArrayList<ArrayList<Integer>> result = getSubset(a);
System.out.println(result);
}
}
import java.util.LinkedList;
import java.util.Queue;
/*Given a binary tree. Write a function that takes only root node as arguement and
returns (sum of values at odd height)-(sum of values at even height).*/
public class Odd_Even_Sum {
public static void main(String[] args) {
TreeNode root = new TreeNode(1);
root.leftchild = new TreeNode(2);
root.rightchild = new TreeNode(3);
root.leftchild.leftchild = new TreeNode(4);
root.leftchild.rightchild = new TreeNode(5);
root.rightchild.leftchild = new TreeNode(6);
root.rightchild.rightchild = new TreeNode(7);
root.rightchild.leftchild.leftchild = new TreeNode(8);
root.rightchild.leftchild.rightchild = new TreeNode(10);
root.rightchild.rightchild.rightchild = new TreeNode(9);
System.out.println(utilFuction(root));
}
public static int utilFuction(TreeNode root)
{
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.add(root);
TreeNode current ;
boolean flag = true;
int sum =0;
Queue<TreeNode> temp_queue = new LinkedList<TreeNode>();
while(!queue.isEmpty())
{
current = queue.poll();
if(flag)
sum =sum + current.data;
else
sum = sum - current.data;
if(current.leftchild!=null)
temp_queue.add(current.leftchild);
if(current.rightchild!=null)
temp_queue.add(current.rightchild);
if(queue.isEmpty())
{
queue = temp_queue;
temp_queue = new LinkedList<TreeNode>();
flag = !flag;
}
}
return sum;
}
}
public class Longest_Palindrome_subsequence {//Using Dynamic Programming and filling //upper half of the diagonal table at the end the table[0][n-1] will give the result
int lps_DP(char [] str)
{ int n = str.length;
int [][] table = new int[n][n] ;
for(int i = 0;i<n;i++)
table[i][i] = 1;
for(int len = 2;len <=n;len++)
{
for(int i = 0;i <n-len+1;i++)
{
int j = i + len - 1;
if(str[i]==str[j] && len ==2)
table[i][j] = 2;
else if(str[i]==str[j])
table[i][j] = table[i+1][j-1]+2;
else
table[i][j] =Math.max(table[i][j-1],table[i+1][j]);
}
}
return table[0][n-1];
}
}
public TreeNode convert(TreeNode node)
{
if(node== null)
return null;
if(node.leftchild==null && node.rightchild== null)
return node;
else
{ TreeNode temp = null;
temp = convert(node.leftchild);
if(temp!=null)
{
while(temp.rightchild!=null)
temp= temp.rightchild;
temp.rightchild = node;
node.leftchild = temp;
}
temp = convert(node.rightchild);
if(temp!=null)
{
while(temp.leftchild!=null)
temp = temp.leftchild;
temp.leftchild = node;
node.rightchild = temp;
}
}
return node;
}
boolean isBST(TreeNode node,int min,int max)
{
if(node==null)
return true;
if(node.leftchild==null && node.rightchild==null)
return true;
else if(node.value>min&&node.value<max)
return(isBST(node.leftchild, min, node.value)&&isBST(node.rightchild,node.value,max));
else
return false;
}
public class BST_greater_sum {
int max_sum;
public BST_greater_sum() {
max_sum =0;// TODO Auto-generated constructor stub
}
TreeNode modify_tree(TreeNode node,int sum){
if(node == null)
return null;
else
{
modify_tree(node.rightchild, max_sum);
max_sum = max_sum + node.value;
node.value = max_sum;
modify_tree(node.leftchild, max_sum);
}
return node;
}
}
public class Mul_remai_nos {
public static void multipy(int[] a)
{
int multi_result = 1;
for(int i : a)
{
multi_result = multi_result*i;
}
for(int j=0;j< a.length;j++)
{
a[j] = multi_result/a[j];
}
}
public static void main(String[] args) {
int[] a = {1,2,3,4};
for(int i : a)
System.out.print(i+"\t");
multipy(a);
System.out.println();
for(int i : a)
System.out.print(i+"\t");
}
}
public class Max_height_tree {
int height1,height2;
public Max_height_tree() {
height1 = 0;
height2 =0;
}
public int height(TreeNode node)
{
if(node==null)
return -1;//This decides if a
else{
height1=height(node.leftchild)+1;
height2 =height(node.rightchild)+1;
}
return (Math.max(height1,height2));
}
}
import java.util.HashMap;
public class Anagram {
static HashMap<Character,Integer> Anagram_table = new HashMap<Character,Integer>();
public Anagram() {
}
public static boolean checkIfAnagram(String s1,String s2)
{
if(s1.length()!=s2.length())
return false;
for(int i =0;i < s1.length();i++)
{ char c = s1.charAt(i);
int count =0;
if(!Anagram_table.containsKey(c))
Anagram_table.put(c, ++count);
else
Anagram_table.put(c,(Anagram_table.get(c))+1);
}
for(int j =0;j<s2.length();j++)
{
char c = s2.charAt(j);
if(Anagram_table.containsKey(c))
{
Anagram_table.put(c,Anagram_table.get(c)-1);
if(Anagram_table.get(c)==0)
Anagram_table.remove(c);
}
else
return false;
}
return(Anagram_table.isEmpty());
}
}
Repdeloresrdaniels, Cloud Support Associate at JDA
Crossed the country marketing wool in Nigeria. Was quite successful How to Get Girlfriend Back By Vashikaran Mantra . Spent a ...
Repsos337837, Personnel assistant at Bountiful Harvest Health Food Store
I am a Risten Personnel Assistant . I am responsible for maintaining human resource records of Department employees . I have a ...
- RS January 20, 2014