Query

Hi,

Does anyone knows how one single Array elements getting stored in two different stacks? Please explain.

I have one Array elements like this {-4,3,8,-1,1,-6,4,5} , using LinkedList

we need to calculate the maximun count of positive values. In the above case it should return the value 10!! its calculatig the addition subsequent positive number plus next negative number.. hope you clear.

Please provide me the hint how to do this..

[469 byte] By [Pannara] at [2007-11-27 7:47:35]
# 1

> hope you clear.

>

Anything but.

I didn't understand the following statment:

> its calculatig the addition subsequent positive number plus next negative number

So you expected 10, what answer did you actually get?

Edit: And post some code maybe, it may help. And use code tags (the code button above the reply window).

masijade.a at 2007-7-12 19:28:40 > top of Java-index,Java Essentials,New To Java...
# 2
ahh... Just give me the generic logic how to add continious positive numbers and ends with a negative number and store it a variable.. like that calculate remaining part same like above.. Finally, return the maximun value among the resultant positive values...
Pannara at 2007-7-12 19:28:40 > top of Java-index,Java Essentials,New To Java...
# 3
{-4,3,8,-1,1,-6,4,5}> its calculatig the addition subsequent positive number plus next > negative number3 + 8 - 1 = 10 ? is it what you want?
j_shadinataa at 2007-7-12 19:28:40 > top of Java-index,Java Essentials,New To Java...
# 4

Use a loop adding each element to another variable that was initialised with 0. Write something up for that (at least try to), and if you are unable to get it right, then post the code you wrote here (using code tags) and we will give a nudge in the right direction again.

Edit: With an if statement to check the current element being added onto the variable, of course.

masijade.a at 2007-7-12 19:28:40 > top of Java-index,Java Essentials,New To Java...
# 5
Yes, thats what i wanted . it should be adding contnious positive numbers plus immediate next negative number. Finally return the maximum number among the calculated values. I didn't still understand why interviewer asking this kind of questions :-(
Pannara at 2007-7-12 19:28:40 > top of Java-index,Java Essentials,New To Java...
# 6

int data = { -4, 3, 8, -1, 1, -6, 4, 5 };

int total = 0;

for (int i : data) {

if (i > 0) {

total += i;

} else {

if (total > 0) {

total += i;

break;

}

}

}

return total;

(Edit) i don't understand this part :

> Does anyone knows how one single Array elements getting stored in

> two different stacks?

> using LinkedList

> we need to calculate the maximun count of positive

> values.

Message was edited by:

j_shadinata

j_shadinataa at 2007-7-12 19:28:40 > top of Java-index,Java Essentials,New To Java...
# 7

You shoudn't really do it for him, but I believe that "maximum count part" is because of the 4, 5 at the end only being 9, therefore the 3, 8, -1 part is the correct return value, but everything would have to be evaluated as if it ended with 6, 5 then 11 would be the answer (I believe thats what that means).

masijade.a at 2007-7-12 19:28:40 > top of Java-index,Java Essentials,New To Java...
# 8

Thanks a lot Shadinata!! Your code worked out very well!

class ArrayBound

{

public static void main(String[] args)

{

int[] data = { -4, 3, 8, -1, 1, -6, 4, 5 };

int total = 0;

for (int i=0; i<data.length; i++) {

if (data[i] > 0) {

total += data[i];

} else {

if (total > 0) {

total += data[i];

break;

}

}

}

System.out.println(total);

}

}

>(Edit) i don't understand this part :

> Does anyone knows how one single Array elements getting stored in

> two different stacks?

What i asked was I have a single empty array. i know how to to store the values in that single array, but i dont know how to push and pop the those values in two different stacks at the same time.. I know abt stack processes push to insert the value from bottom and retrieved in LIFO order.. but how the memory space utilized? are they going to share the same memory space and different stack space?

Pannara at 2007-7-12 19:28:40 > top of Java-index,Java Essentials,New To Java...
# 9
errr.... actually it's not what you are looking for (? i think you also don't understand the question given to you).
j_shadinataa at 2007-7-12 19:28:40 > top of Java-index,Java Essentials,New To Java...
# 10
Sorry, I got the answer for my first question which had given by shadinata. i mixed up the second qestion which is totally different.. my second question was how to store a single array into two different stacks?
Pannara at 2007-7-12 19:28:40 > top of Java-index,Java Essentials,New To Java...