Running Sum of 1D Array - LeetCode - 1480 - Java Solution

Given an array nums. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]).


Given an array nums. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]).

Return the running sum of nums.


Solution:


[

class Solution {

    public int[] runningSum(int[] nums) {

        int[] returnNumbers = new int[nums.length];

        int counter = 0;

        int num = 0;

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

            num = 0;

            for (int j = 0; j <= counter ; j++){

                num = num + nums[j];          

            }

            returnNumbers[i] = num;

            counter++;

        }

        return returnNumbers;

    }

}

]

Steps :

Here we have solved this problem using the brute force approach. In the first for loop, we iterate through the given array. 

We save the sum of elements in an array in the variable num. Next, we run a second for loop through the elements of the array till i th element and add it and save it into the variable num. 

The second loops run only till the variable counter. Once the second loop is finished running, we take the sum of i elements and save it in a new array and increment the counter once. Once the code has finished running we have an array that contains the sum till i th element. 

Let me know your thoughts in the comments below. 

COMMENTS

BLOGGER
Name

Affiliate marketing tutorial,2,Android,60,Android Development,22,Android tutorials,41,Blogging tactics,8,Deals,1,DSA,4,Elite Youtube Tutorial,4,Firebase,1,Flutter,4,Fuchsia,1,Hosting Guides,3,Leetcode,4,Miscellaneous,10,Mobile Gaming,17,NTN,94,Problogging,7,PUBG,16,Reviews,1,SEO Tutorial,5,Start a blog,9,Start a channel,6,Top 5,4,Travel Blogging,4,Windows,12,Windows Security,4,Windows tutorials,7,Wordpress Tutorials,4,Youtube Tutorials,11,
ltr
item
A Passionate Developers Creation: Running Sum of 1D Array - LeetCode - 1480 - Java Solution
Running Sum of 1D Array - LeetCode - 1480 - Java Solution
Given an array nums. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]).
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhY_qgpqPvevNnhV3g207ysNbm1oiyQVk7y7HEZ-G44c4gcYet5jcHeUKLNECGgVt2f0FcV2LDtLMnSD89EF5eSXutgpSp5xFP_7VAG3vExrMTJ95PCflS_lnlqWSvbVc-AolDCgTzYEBlQ91tS-MSKDDvJrJlEUC-wWkS7hNRmF71WKUQ68nd7rJyDnME/w200-h133/luca-bravo-XJXWbfSo2f0-unsplash.jpg
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhY_qgpqPvevNnhV3g207ysNbm1oiyQVk7y7HEZ-G44c4gcYet5jcHeUKLNECGgVt2f0FcV2LDtLMnSD89EF5eSXutgpSp5xFP_7VAG3vExrMTJ95PCflS_lnlqWSvbVc-AolDCgTzYEBlQ91tS-MSKDDvJrJlEUC-wWkS7hNRmF71WKUQ68nd7rJyDnME/s72-w200-c-h133/luca-bravo-XJXWbfSo2f0-unsplash.jpg
A Passionate Developers Creation
https://www.apdevc.com/2024/10/running-sum-of-1d-array-leetcode-1480.html
https://www.apdevc.com/
https://www.apdevc.com/
https://www.apdevc.com/2024/10/running-sum-of-1d-array-leetcode-1480.html
true
3690156770086819421
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content