From 5ab621a1223261d960d63c143a64501df0316f82 Mon Sep 17 00:00:00 2001 From: aadil42 <77232799+aadil42@users.noreply.github.com> Date: Fri, 3 Jan 2025 12:42:58 +0530 Subject: [PATCH] Create 2270-number-of-ways-to-split-array.js Solved number-of-ways-to-split-array. --- .../2270-number-of-ways-to-split-array.js | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 javascript/2270-number-of-ways-to-split-array.js diff --git a/javascript/2270-number-of-ways-to-split-array.js b/javascript/2270-number-of-ways-to-split-array.js new file mode 100644 index 000000000..153a38581 --- /dev/null +++ b/javascript/2270-number-of-ways-to-split-array.js @@ -0,0 +1,20 @@ +/** + * Array | Counting + * Time O(n) | Space O(1) + * https://leetcode.com/problems/number-of-ways-to-split-array + * @param {number[]} nums + * @return {number} + */ +var waysToSplitArray = function(nums) { + + let currSum = 0; + let total = nums.reduce((acc, num) => num+acc, 0); + let totalValidSplits = 0; + + for (let i = 0; i < nums.length - 1; i++) { + currSum += nums[i]; + if (currSum >= total-currSum) totalValidSplits++; + } + + return totalValidSplits; +};