ohadr.developer
BAN USERpublic static int findPivot(int[] input)
{
if( input == null || input.length <= 1)
return -1;
int sum_left = 0;
int sum_right = 0;
for(int i = 1; i < input.length; ++i)
{
sum_right += input[i];
}
for( int i = 0; i < input.length -1 ; ++i)
{
if(sum_left == sum_right)
return i;
sum_left += input[i];
sum_right -= input[i+1];
}
return -1;
}
The idea is to iterate only the input (sorted) array...
- ohadr.developer November 20, 2016you solve it using a binary tree?
=)
and: wow, this is 1-hour question?!
this is the best solution.
- ohadr.developer August 13, 2015public static class TreeNode<T>
{
TreeNode(T value, TreeNode left, TreeNode right)
{
this.value = value;
this.left = left;
this.right = right;
}
T value;
TreeNode left;
TreeNode right;
@Override
public String toString()
{
return value.toString();
}
}
public static void printZigZagTree( TreeNode head )
{
List<TreeNode> buffer = new ArrayList<TreeNode>();
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.add(head);
//add the 'dummy' node:
queue.add( new TreeNode(null, null, null) );
boolean isLeftToRight =
//true;
false;
while( ! queue.isEmpty() )
{
TreeNode node = queue.poll();
if(node.value == null) //if this is dummy, we know that it is a new "level" in the tree. so handle the previous level (in the buffer)...
{
if(isLeftToRight)
{
for(TreeNode tn : buffer)
{
System.out.print(tn);
}
}
else
{
for(int i = buffer.size()-1; i >= 0 ; --i)
{
System.out.print(buffer.get(i));
}
}
System.out.println();
buffer.clear();
isLeftToRight = !isLeftToRight;
//add the dummy only if the Q is not empty - o/w we run into endless loop:
if(!queue.isEmpty())
queue.add(node);
}
else
{
buffer.add(node);
//add its child to queue:
if(node.left != null)
queue.add(node.left);
if(node.right != null)
queue.add(node.right);
}
}
}
public static void findReps(int array[])
{
int prev = -1;
int counter = 1;
for( int i : array)
{
if( i != prev )
{
if(counter > 1)
{
System.out.println("bla bla " + prev + " bla " + counter);
counter = 1;
}
}
else
{
++counter;
}
prev = i;
}
//check last item:
if(counter > 1)
{
System.out.println("bla bla " + prev + " bla " + counter);
}
}
I have the code? i would debug the alarm app. see what happens upon invocation. see where the error is.
- ohadr.developer July 26, 2015
Replillycrobinson, Program Manager at Marvell
Hey, I am Lilly. I was born in Missouri on September 6, 1992. I studied medicine at Queen’s University ...
RepHi, I am a Graphic designer from Madison. I have been working in cretor for the last 2 years. Before ...
- ohadr.developer November 20, 2016