DeepJVM
DeepJVM

Reputation: 87

ArrayList or any other elegant solution?

(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?

Upvotes: 0

Views: 90

Answers (2)

botch
botch

Reputation: 167

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.

Upvotes: 1

Andreas
Andreas

Reputation: 159086

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!

Upvotes: 1

Related Questions