Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

find maximum sum in array of contiguous subarrays

#include <iostream>

using namespace std;

int main(){
    //Input Array
    int n;
    cin >> n;
    int arr[n];
    for(int i =0;i< n;i++){
    cin >> arr[i];
    }

    int currentSum = 0;
    int maxSum = INT_MIN;
    //algo
    for (int i = 0; i < n; i++)
    {
        currentSum += arr[i];
        if (currentSum <0)
        {
            currentSum = 0;
        }
        maxSum = max(maxSum, currentSum);
    }
    cout << maxSum << endl;

    return 0;
}
Comment

Maximum Subarray sum

 def maxSubArray(self, nums: List[int]) -> int:
        curr_best = overall_best =  nums[0]
        for i in range(1,len(nums)):
            if curr_best>=0:
                curr_best = curr_best + nums[i]
            else:
                curr_best = nums[i]
            if curr_best > overall_best:
                overall_best = curr_best
        return overall_best
Comment

maximum sum subarray

public static int SumArray()
{
    var arr = new int[]{ -2, -4, -5, -6, -7, -89, -56 };
    var sum = 0;
    var max = arr[0];
    foreach (var item in arr)
    {
        sum += item;
      // sum = Math.Max(sum,0); resting here will not give  expected output
        max = Math.Max(sum,max);
        sum = Math.Max(sum,0);
    }
    return max;
}
Comment

How to find the maximum subarray sum in python?

"""
Implementation of "kadane's algorithm".
It returns the maximum subarray sum for
a given bigger array.
Time complexity: O(n)
Space complexity: O(1)
"""

def kadane_algorithm(array):
    running_sum = array[0]
    max_sum = array[0]
    for idx in range(1, len(array)):
        current_val = array[idx]
        running_sum = max(current_val, running_sum+current_val)
        max_sum = max(max_sum, running_sum)
    return max_sum

print(kadane_algorithm([1, 2, 3, 5, -1]))  # 11 = 1 + 2 + 3 + 5
Comment

Maximum subarray sum

var maxSequence = function(arr){
  var min = 0, ans = 0, i, sum = 0;
  for (i = 0; i < arr.length; ++i) {
    sum += arr[i];
    min = Math.min(sum, min);
    ans = Math.max(ans, sum - min);
  }
  return ans;
}
Comment

print maximum sum subarray

#include<bits/stdc++.h>
using namespace std;
int main(){
	int n;
	cin>>n;
	int a[n];
	for(int  i = 0;i<n;i++){
		cin>>a[i];
	}
	int s = 0;
	int mx = INT_MIN;
	for(int  i =0;i<n;i++){
		s+=a[i];
		if(s<0){
			s=0;
		}
		mx = max(s,mx);
	}
	cout<<mx;
	return 0;
}
Comment

Maximum sum subarray of size ‘K’

l=[4,6,10,8,2,1]
ans=0
m=3
for i in range(len(l)+1):
   for j in range(i):
       if len(l[j:i])==m:
          ans=max(ans,sum(l[j:i]))
print(ans) 

Comment

PREVIOUS NEXT
Code Example
Python :: how to import data in python 
Python :: round python print 
Python :: upgrade python version windows 
Python :: django password hashing 
Python :: plt.hist bins 
Python :: gui button in tkinter color 
Python :: import gpio raspberry pi 
Python :: two pointer function in python 
Python :: add header info in django response 
Python :: .squeeze function in numpy 
Python :: python - input: integer 
Python :: python declare variable 
Python :: swarm plot 
Python :: append element to list py 
Python :: python map 
Python :: simple bmi calculator using python 
Python :: how to standardize the image data to have values between 0 and 1 
Python :: Python RegEx Subn – re.subn() Syntax 
Python :: imshow of matplotlib 
Python :: index in python 
Python :: python looping through a list 
Python :: how to remove some indexes from a dataframe in python 
Python :: how to write something in python 
Python :: dictionary get all values 
Python :: doing some math in python 
Python :: prompt python 
Python :: how to make a variable in python 
Python :: /n in python 
Python :: python list remove 
Python :: range(n,n) python 
ADD CONTENT
Topic
Content
Source link
Name
6+8 =