nasim
BAN USERThis prints:
1
11
21
1211
111221
312211
13112221
public static void main(String[] args){
int n = 7;
String str = "1";
while(n-->0){
System.out.println(str);
String tmp ="";
for(int i =0; i<str.length();)
{
int count =0;
char ch = str.charAt(i);
int j = i;
while(j<str.length() && str.charAt(j++)== ch)count++;
tmp = tmp+count+ch;
i= i +count;
}
str=tmp;
}
}
- nasim May 19, 2015This will print :
+1+9+1-2
-1+9-1+2
public class arithPrint {
private static void findArith(int[] nums, int index, int target , String s){
if(index == nums.length && target == 0)
System.out.println(s);
else if(index<nums.length)
{
findArith(nums ,index+1, target-nums[index] , s+"+"+nums[index]);
findArith(nums ,index+1, target+nums[index] , s+"-"+nums[index]);
}
}
public static void main(String[] args){
int[] nums = {1,9,1,2};
int target = 9;
findArith(nums ,0 , target , "");
}
}
Here is my Java Solution:
public class freqArray {
public static void main(String[] args) {
int[] lst = {0,100,2,3,5,0,100,2,6,2};
int count = 3;
HashMap<Integer, Integer> tbl = new HashMap<Integer, Integer>();
for(int i = 0; i< lst.length ; i++)
{
int f = 0;
if(tbl.containsKey(lst[i]))
{
f= tbl.get(lst[i]);
}
f++;
tbl.put(lst[i],f);
}
List<Integer> values = new ArrayList( tbl.values());
Collections.sort(values, Collections.reverseOrder());
Set mySet = new HashSet<Integer>();
for(int i = 0; i<count ;i++)
{
for(int key : tbl.keySet())
{
if(tbl.get(key)== values.get(i) && !mySet.contains(key))
{
mySet.add(key);
System.out.println(key);
break;
}
}
}
}
}
I think we need to read the question again. All nodes in subtree of counting nodes should be in range. Here is my code that returns 3 for this tree with range[2,6] which I believe is the right answer:
____________8____________
| |
__5____ ____10____
| | | |
_ 3_ 6_ 9 12
| | |
2 4 7
Code:
public class SameInorder {
static int count;
static int start;
static int end;
private static boolean inRange(Node root) {
if (root == null)
return true;
if ((int) root.data > end) {
if (root.left == null && root.right == null)
return false;
return inRange(root.left);
}
if ((int) root.data < start) {
if (root.left == null && root.right == null)
return false;
return inRange(root.right);
} else {
boolean r = inRange(root.right);
boolean l = inRange(root.left);
if (r && l) {
count++;
return true;
}
return false;
}
}
public static void main(String[] args) {
count = 0;
start = 2;
end = 6;
Node tree = new Node(8);
{
tree.setLeft(5);
{
tree.left.setLeft(3);
{
tree.left.left.setLeft(2);
tree.left.left.setRight(4);
}
tree.left.setRight(6);
{
tree.left.right.setRight(7);
}
}
tree.setRight(10);
{
tree.right.setRight(12);
tree.right.setLeft(9);
}
}
if (inRange(tree))
count++;
System.out.println(count);
}
}
It means the minimum value in range of min and max of elements.
Here is my solution.it works.
- nasim May 26, 2015