(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?
- 5If 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.Ori Lentz– Ori Lentz2015-12-20 08:40:56 +00:00Commented 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 numbersuser1945693– user19456932015-12-20 08:41:10 +00:00Commented 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.Razvan Manolescu– Razvan Manolescu2015-12-20 08:46:18 +00:00Commented Dec 20, 2015 at 8:46
- 3collections 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.guillaume girod-vitouchkina– guillaume girod-vitouchkina2015-12-20 08:55:20 +00:00Commented Dec 20, 2015 at 8:55
2 Answers
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!
2 Comments
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.