0

(Apologies in advance for the descriptive question) I have to take integers as values for two variables and keep taking the integer values for as long as I like- from the console. At any point of time , I might requisition what is the sum of the integers entered corresponding to the variables. For example, For Variable A , I have entered 5,5,4 and for Variable B I have entered 2,3,6 [These inputs are taken from the console at different times, not at the same time]. At any point of time I would need to display the sum of A( which is 14) and sum of B(which is 11).

What I have thought is to declare two arraylists : one each for A and B. As I get the values for for A & B, I would keep adding them to their respective arraylists.
Whenever there is a demand for displaying the sum of A , I would add the values in the index of Arraylist for A and display. Same for B.
However, I have a doubt that this approach may be inefficient. Because if I enter one thousand integers for variable A, then the arrayList would become 1000 indexes long and would consume too much memory and summing would be too much of work. Can I achieve what I want without using an arraylist ? some other elegant solution?

4
  • 5
    If you don't need a history of the values entered, just their sum, then you don't need to keep all values in an array. Simply keep two variables and sum their values as they are entered. Commented Dec 20, 2015 at 8:40
  • If you don't want to store them, just create two variables and keeping adding to it as you scan the numbers Commented Dec 20, 2015 at 8:41
  • Agree with Ori above.You can add the new variable to the array AND to the sum once you entered it. If you feel you may have a large amount of variables, store the sums in a hash map. Commented Dec 20, 2015 at 8:46
  • 3
    collections are well designed and optimized. Dont think about problems for 1000 indexes. I think the poor man who inputs the datas will be exhausted before the JVM. Just keep them all, or just the sum. Commented Dec 20, 2015 at 8:55

2 Answers 2

1

You have doubt about 1000 integers in a list, and the time it takes to sum them? Let me remove all your doubts: You don't have a problem.

Try this code:

long start = System.currentTimeMillis(); ArrayList<Integer> list = new ArrayList<>(); for (int i = 0; i < 1000000; i++) list.add(i); int sum = 0; for (int num : list) sum += num; long end = System.currentTimeMillis(); System.out.printf("Sum of %d values is %d%n", list.size(), sum); System.out.printf("Built and calculated in %.3f seconds%n", (end - start) / 1000.0); 

It built an ArrayList<Integer> with one million values, not a measly 1000, then sums them. Output is:

Sum of 1000000 values is 1783293664 Built and calculated in 0.141 seconds 

So, a million in way less than one second. 1000? Not an issue!

Sign up to request clarification or add additional context in comments.

2 Comments

Awesome. So while using the 'collections' I should leave doubts like processing time and efficiency to be taken care of by Java. This helps
@DeepJVM Yeah. In general, and until you learn a lot more, don't think too much about performance, because you'll probably waste time optimizing the wrong code. Most performance tuning should be done by measuring (called "profiling") where the bottlenecks truly are, not where you think they are.
1

It sounds like you are trying to do something like: (in Java)

private int a = 0; public void add_to_A(int input) { a += input; return; } public int a_sum() { return a; } 

As noted, an arrayList would require O(n) space, and would run at O(n) time for any given point in the input. If you received a pathological input, asking for the sum at every step, this would require O(n^2) time, which wouldnt be good for this problem.

As other people have noted, memory isnt going to be an issue for 1000 integers, BUT for the sake of knowledge, it is important to note that if we increase the size of our input a few orders of magnitude and give it a bad input (ask the size of A at every step) run time will blow up quickly. Going by the statistic Andreas submitted, if the array algorithm runs in a few milliseconds for 1000 inputs, the same algorithm running for a million inputs would be closer to a few days/weeks for an unlucky run.

4 Comments

Thanks. This looks very close to what I want. Only one problem: when the second value of the variable comes in from the console( which might happen after 5 minutes): the int(a) should stay the value of the first variable entered. This way, when I sum it, I would get the sum of the first value and the second value(lets call it sum1). Similarly, when the 3rd value comes in , the initial value should be sum1. Then it would add the val3 to this sum1 and give the final sum. This code will not remember what was the sum value before this input came in.
No problem! If I understand you correctly, you want to be able to retrieve old answers from your data? (e.g. add input 52, display sum 41) If so, then yes, this code definitely does not reflect that, and you would need to maintain an array.
What is the example of a typical bad input in this case ? The only thing I can do is that I can requisition the sum at any stage. What would be an unlucky run ?
A bad input would be a request for the sum at every stage. Technically speaking, the list of numbers you want to sum is only a part of the input in the problem we are discussing, the other part is the requests the user sends for the sum. Since any number of inputs may have very few requests for the sum, most heuristics wont offer much difference, but since other algs describe a O(n) runtime for any given request, they are polynomial algs for bad inputs (lots of requests). However, if you want to be able to retrieve old sums, as I gathered from your first comment, this is may be unavoidable

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.