Skip to content

Commit

Permalink
maxSubSumKConcat
Browse files Browse the repository at this point in the history
  • Loading branch information
Abid Khan committed Jan 30, 2023
1 parent 9104a36 commit 948466b
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions arrays/kandanes/maxSumSubKConcat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function maxSubSumKConcat(arr, k){
let newArr = [];
let maxSum = -Infinity;
let currentSum = 0;
for(let i = 0; i < k; i++){
newArr = [ ...newArr, ...arr]
}

for(let i= 0; i < newArr.length; i++){
currentSum += newArr[i];
maxSum = Math.max(maxSum, currentSum);
if(currentSum < 0){
currentSum = 0;
}
}

return maxSum

}

console.log(maxSubSumKConcat([1,3],3) == 12)
console.log(maxSubSumKConcat([1, -2, 1],2) == 2)
console.log(maxSubSumKConcat([1,-1, 2],3) == 6)
console.log(maxSubSumKConcat([0,-1, 2],3) == 4)

0 comments on commit 948466b

Please sign in to comment.