Search
 
SCRIPT & CODE EXAMPLE
 

CPP

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

const maxSubArray = (arr) => {
    let currSum =  getSum(arr, 0, 4);
    let maxSum = 0
    for(let i = 1; i < arr.length - 4; i++){
       currSum =  currSum - arr[i - 1];
       currSum = currSum + arr[i + 3];
        maxSum = Math.max(currSum, maxSum);
    }
    return maxSum;
}

const getSum = (arr, start, end) => {
    let sum = 0;
    for(let i = start; i < end; i++){
        sum += arr[i]
    }
    
    console.log(sum);
    return sum;
}
Comment

find maximum contiguous Sub arrays

#include <iostream>
using namespace std;
int main(){
    int n;
    cin >> n;
    int arr[n];
    for(int i =0;i< n;i++){
        cin >> arr[i];
    }
    
    for (int i = 0; i < n; i++)
    {
        for (int j = i; j < n; j++)
        {
            for (int k = i; k <= j; k++)
            {
                cout << arr[k] << " ";
            }
            cout << endl;
        }
        
    }
    

    return 0;
}
Comment

maximum subarray

// A Divide and Conquer based Java
// A for maximum subarray sum
// problem
import java.util.*;
 
class GFG {
 
    // Find the maximum possible sum in arr[]
    // such that arr[m] is part of it
    static int maxCrossingSum(int arr[], int l, int m,
                              int h)
    {
        // Include elements on left of mid.
        int sum = 0;
        int left_sum = Integer.MIN_VALUE;
        for (int i = m; i >= l; i--) {
            sum = sum + arr[i];
            if (sum > left_sum)
                left_sum = sum;
        }
 
        // Include elements on right of mid
        sum = 0;
        int right_sum = Integer.MIN_VALUE;
        for (int i = m; i <= h; i++) {
            sum = sum + arr[i];
            if (sum > right_sum)
                right_sum = sum;
        }
 
        // Return sum of elements on left
        // and right of mid
        // returning only left_sum + right_sum will fail for
        // [-2, 1]
        return Math.max(left_sum + right_sum - arr[m],
                        Math.max(left_sum, right_sum));
    }
 
    // Returns sum of maximum sum subarray
    // in aa[l..h]
    static int maxSubArraySum(int arr[], int l, int h)
    {
          //Invalid Range: low is greater than high
          if (l > h)
              return Integer.MIN_VALUE;
        // Base Case: Only one element
        if (l == h)
            return arr[l];
 
        // Find middle point
        int m = (l + h) / 2;
 
        /* Return maximum of following three
        possible cases:
        a) Maximum subarray sum in left half
        b) Maximum subarray sum in right half
        c) Maximum subarray sum such that the
        subarray crosses the midpoint */
        return Math.max(
            Math.max(maxSubArraySum(arr, l, m-1),
                     maxSubArraySum(arr, m + 1, h)),
            maxCrossingSum(arr, l, m, h));
    }
 
    /* Driver program to test maxSubArraySum */
    public static void main(String[] args)
    {
        int arr[] = { 2, 3, 4, 5, 7 };
        int n = arr.length;
        int max_sum = maxSubArraySum(arr, 0, n - 1);
 
        System.out.println("Maximum contiguous sum is "
                           + max_sum);
    }
}
// This code is contributed by Prerna Saini
Comment

PREVIOUS NEXT
Code Example
Cpp :: inside information subtask 2 
Cpp :: c++ map access 
Cpp :: c++ read_ascii 
Cpp :: sinh to hop c++ 
Cpp :: c++ set value to inf 
Cpp :: add integers 
Cpp :: different way to print string in c++ 
Cpp :: How to make an array dynamically using pointers 
Cpp :: qt c++ thread example 
Cpp :: c++ How to not use friend declaration when equipping a class with `operator<<` 
Cpp :: c++ starting syntaz 
Cpp :: convert c++ to python online 
Cpp :: Initialize Vector Iterator with begin() function 
Cpp :: Jython Java Python 
Cpp :: vector übergeben c++ 
Cpp :: 123213 
Cpp :: Chef and IPC Certificates codechef solution in c++ 
Cpp :: namespace c++ 
Cpp :: cast c++ 
Cpp :: _ZNSolsEi 
Cpp :: prime template c++ 
Cpp :: FINAL CODES_MY_OS_LAB 
Cpp :: what is c++ function 
Cpp :: how to make a segment tree in c++ 
Cpp :: convert from hex to decimal c++ 
Cpp :: c++ press any key 
Cpp :: 2927260.eps 2927262.jpg 2927263.ai License free.txt License premium.txt 
C :: sleep in c programming 
C :: c string is int 
C :: sdl draw Rectf 
ADD CONTENT
Topic
Content
Source link
Name
6+8 =