marcelovox2
BAN USERYou have to use related subtraction... that is the way to get the 5 ... I wrote a simple code below showing it ..
public void test() {
int i = 123456789;
int r = reverse(i);
System.out.println(r);
}
private int reverse(int i) {
int r = 0;
int d = 10;
int s = 0;
while(i > 0){
int remainder = i % 10;
r = (r * 10) + remainder;
s = 0;
while(i > 10){
i = d;
s++;
}
i = s;
}
return r;
}

marcelovox2
January 25, 2013 Don't need to convert to String .. do it using %, * and + operators :)
Keep in mind > 756 %10 = 6 .. (6 * 10) + 5 = 65 ... so you can come up with the algorithm :)
How about using a suffix array?
 marcelovox2 January 11, 2013public static void main(String[] args) {
char[] l = "dog world car life".toCharArray();
System.out.println(l);
for(int i =0; i < l.length/2; i++){
char t = l[i];
l[i] = l[l.length  i  1];
l[l.length  i1] = t;
}
for(int j = 0, s = 0, e = 0; j <= l.length; j++){
if(j == l.length  l[j] == ' '){
e = j  1;
for(; s < e;){
char t = l[s];
l[s] = l[e];
l[e] = t;
s++;
e;
}
s = j + 1;
}
}
System.out.println(l);
}

marcelovox2
January 10, 2013 import java.util.Arrays;
public class Permutation {
public static void main(String args[]) {
char sorted[] = { 'a', 'a', 'l', 'b' };
Arrays.sort(sorted);
permute(sorted, 0);
}
public static void permute(char[] word, int k) {
if (word.length == k)
System.out.println(word);
else {
for (int i = k; i < word.length; i++) {
if (i + 1 < word.length && word[i] == word[i + 1])
continue;
swap(word, i, k);
for (int l = k + 1; l <= i; l++) {
if (word[l] > word[i]) {
swap(word, l, i);
}
}
permute(word, k + 1);
for (int l = i; l >= k + 1; l) {
if (word[l] < word[i]) {
swap(word, l, i);
}
}
swap(word, i, k);
}
}
}
private static void swap(char[] s, int index, int i) {
char temp = s[index];
s[index] = s[i];
s[i] = temp;
}
}

marcelovox2
November 07, 2012 Question > "You should process it as soon as you are taking an input."
Probably the arraylen doesn't work..
Using a Sweep Line algorithm approach I think is the way... can be done in N logN
 marcelovox2 October 14, 2012import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
public class LongestSequence {
static Seq current = new Seq();
static Seq lastLongSequence = new Seq();
public static void main(String[] args) {
int a[] = { 1, 5, 3, 4, 6, 4 };
int[] expectedResult = { 3, 4, 6 };
// coming from an input
for (int i = 0; i < a.length; i++) {
addSequence(a[i]);
}
Seq result = current.size() > lastLongSequence.size()?current:lastLongSequence;
if (Arrays.equals(expectedResult, result.getAsArray())) {
System.out.println("Goal");
} else {
System.out.println("fail");
}
}
private static void addSequence(int i) {
if (i > current.getLastElement()) {
current.add(i);
} else {
if(lastLongSequence.size() < current.size()){
lastLongSequence = current;
}
current = new Seq();
current.add(i);
}
}
static class Seq {
int last = 0;
// using a dynamic array
ArrayList<Integer> s = new ArrayList<Integer>();
public int[] getAsArray() {
return convert(s);
}
public int size() {
return s.size();
}
public void add(int i) {
last = i;
s.add(i);
}
public int getLastElement() {
return last;
}
int[] convert(ArrayList<Integer> integers) {
int[] r = new int[integers.size()];
Iterator<Integer> iterator = integers.iterator();
for (int i = 0; i < r.length; i++) {
r[i] = iterator.next().intValue();
}
return r;
}
}
}

marcelovox2
October 14, 2012 A solution using DFS or BFS has the run time O(V+E) for worst case.
 marcelovox2 October 13, 2012I am assuming you type in your search the contact(in this case freeninza) and it returns a phone linked to this contact. Using a Trie can help you on that and the time will be O(L) where L is the string lenght, in this case freeninza.
 marcelovox2 October 13, 2012BFS or DFS in this situation work pretty much in the same way for timing.
 marcelovox2 October 13, 2012public void permutation(char[] a, int n) {
if(n == 1){
System.out.println(a);
return;
}
for(int i = 0; i < n; i++){
swap(a, i, n1);
permutation(a, n1);
swap(a, i, n1);
}
}
private void swap(char[] a, int i, int j) {
char c;
c = a[i]; a[i] = a[j]; a[j] = c;
}

marcelovox2
October 13, 2012 Use DFS algorithm to get the height of each path from the root to the leaf.
 marcelovox2 October 13, 2012Binary tree we cant assume it is sort(BST), in that case I'd use a DFS(depthfirst search), visiting all paths from the root. You can keep in a separeted structure the max sum found and the nodes visited.... You can even considering using a max priority queue to support it.
 marcelovox2 October 13, 2012How about using permutation?
package algos.pq;
public class Permutation {
private static int greatestNumber;
public static void main(String[] args) {
int a[] = { 4, 94, 1, 14, 9 };
int[] expectedResult = { 9, 94, 4, 14, 1 };
permutation(a, a.length);
if (greatestNumber == getAsNumber(expectedResult)) {
System.out.println("Goal");
System.out.println(greatestNumber);
} else {
System.out.println("fail");
System.out.println(greatestNumber);
}
}
private static void permutation(int[] a, int n) {
if (n == 1) {
int maybeTheNumber = getAsNumber(a);
if (greatestNumber < maybeTheNumber)
greatestNumber = maybeTheNumber;
return;
}
for (int i = 0; i < n; i++) {
swap(a, i, n  1);
permutation(a, n  1);
swap(a, i, n  1);
}
}
private static void swap(int[] a, int i, int j) {
int c;
c = a[i];
a[i] = a[j];
a[j] = c;
}
//quick and dirty
private static int getAsNumber(int[] expectedResult) {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < expectedResult.length; i++) {
builder.append(expectedResult[i]);
}
return Integer.parseInt(builder.toString());
}
}

marcelovox2
October 12, 2012
 marcelovox2 January 25, 2013