Java Program to Compute the Running Total of a List
A List is an ordered sequence of elements stored together to form a collection. A list is a structure to perform both positional access and index-based retrieval of the elements. Lists can be used to store duplicate as well as null elements. Running Total of a sequence of the elements is the summation of the previous sum of the elements and the current integer value received. Lists can be declared and created using an in-built interface <<java.util>>
Storing the first index element of the list into the running total as it is. The previous sum of the elements is initialized to the first index element of this list. And then, with each new element, we add it to the previous sum. This updated sum value becomes the running total until the corresponding index. It is then added to the running total list. Below the procedure is described:
Procedure: Steps followed to compute sum in list
- Step 1: Declaring list for storing running total.
- Step 2: Copy the first element of the original list to the running total List.
- Step 3: Initially, declaring sum to the 0th element of the List.
- Step 4: Iterating over the list starting with the 1st index of List.
- Step 5: Adding the current element to the previous sum.
- Step 6: Copying the previous sum to the running total sum.
- Step 7: Iterating over the elements of the running total List using hasNext() method which holds true till there is a single element remaining in the List.
- Step 8: Print the List which will be running the total List.
Implementation: The following java program is used to illustrate the computation of the running total of the list of elements.
Example:
Java
// Java Program to Compute the Running Total of a List // Importing all classes of input/output java library // Importing all classes from java.util package // Iterator class included in java.util package import java.io.*; import java.util.*; // Class- computing running total in list public class GFG { // Main driver method public static void main(String[] args) { // Creating an List of integer type List<Integer> list = new ArrayList<Integer>(); // Adding elements to List // Custom inputs list.add( 2 ); list.add( 4 ); list.add( 6 ); list.add( 8 ); list.add( 10 ); list.add( 12 ); // printing the contents of the list System.out.print( "Original List : " ); // Iterator Iterator iterator = list.iterator(); // Iterating over the elements of the List // using hasNext() which holds true till there is // further more element in List while (iterator.hasNext()) { System.out.print(iterator.next() + " " ); } // Running total concept in List // Step 1 : Declaring list for storing running total // of list List<Integer> runningtotal = new ArrayList<Integer>(); // Step 2: Copy first element of original list to // running total list runningtotal.add(list.get( 0 )); // Step 3: Initially, declaring sum to the 0th // element of list int prev_sum = list.get( 0 ); // Declaring and maintaining counter int i = 1 ; // Step 4: Iterating over the list starting with the // 1st index while (i < list.size()) { // Step 5: Adding current element to prev_sum prev_sum += list.get(i); // Step 6: Copying prev_sum to the running total runningtotal.add(prev_sum); // Step 6: Incrementing counter i++; } System.out.println(); // Display Message System.out.print( "Running total List of Original List: " ); iterator = runningtotal.iterator(); // Iterating over the elements of the running total // list using hasNext() method which holds true till // there is single element remaining in List while (iterator.hasNext()) { // printing all elements of List System.out.print(iterator.next() + " " ); } } } |
Original List : 2 4 6 8 10 12 Running total List of Original List: 2 6 12 20 30 42
Time complexity: O(N) where N is the size of the given list.
Auxiliary space: O(N), for storing the running sum of the list.