largest sum of contiguous subarray No Larger than k

Jerry Z.

For example, we have

{2,2,-1}, 
when k = 0, return -1.
when k = 3, return 3.

This is even tricky because we have negative numbers and an additional variable k. k can be any value, negative, don't make any assumption.

I cannot refer to https://en.wikipedia.org/wiki/Maximum_subarray_problem and https://www.youtube.com/watch?v=yCQN096CwWM to solve this problem.

Can any body help me? Better use Java or JavaScript.

Here is a classic algorithm o(n) for the maximum(no variable k):

public int maxSubArray(int[] nums) {

        int max = nums[0];
        int tsum = nums[0];
        for(int i=1;i<nums.length;i++){
            tsum = Math.max(tsum+nums[i],nums[i]);
            max = Math.max(max,tsum);
        }

        return max;
    }
Jerry Z.

This is an o(nlogn) solution referred to https://www.quora.com/Given-an-array-of-integers-A-and-an-integer-k-find-a-subarray-that-contains-the-largest-sum-subject-to-a-constraint-that-the-sum-is-less-than-k

private int maxSumSubArray(int[] a , int k){

    int max = Integer.MIN_VALUE;
    int sumj = 0;
    TreeSet<Integer> ts = new TreeSet();
    ts.add(0);

    for(int i=0;i<a.length;i++){
        sumj += a[i];
        Integer gap = ts.ceiling(sumj - k);
        if(gap != null) max = Math.max(max, sumj - gap);
        ts.add(sumj);
    }
    return max;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Debugging: Largest Sum Contiguous Subarray

Largest sum contiguous subarray (Interview Question)

JavaScript algorithm question: get the find the contiguous subarray which has the largest sum from an array

contiguous subarray within an array (containing at least one number) which has the largest sum

Algorithms question: Largest contiguous subarray selection

Length of longest subarray of sum less than or equal to k

Maximum sum subarray with the length smaller than k problem . O(n)

Largest subarray with sum equal to 0

Maximum sum of any contiguous subarray of the array

Find the length of largest subarray with 0 sum

Maximum sum in a contiguous subarray of a given 2D array

How to check if sum of some contiguous subarray is equal to N?

Why am I getting the last sum as largest sum of subarray?

Efficient algorithm for finding largest index j such that sum from index i to j is less than k

Minimum size of subarray whose sum is k

What is contiguous subarray

Number of distinct contiguous subarray

How to sum the array if it is larger than a given size

Sum values larger than the current value, by group

List Roles that sum of expenses are larger than average

smallest subsets with sum larger than value in python

Smallest sum of subarray with sum greater than a given value

Largest subset in an array such that the smallest and largest elements are less than K apart

Find the max element in all the contiguous subarray of size K using only queue

Sliding window algorithm to calculate the list of all k-element contiguous subarray products of an array modulo p

Maximum sum of contiguous sub-sequence with length at most k

JavaScript: return all contiguous subarrays whose sum equals K

how to find the length of the longest contiguous subarray whose sum is divisible by a given number

i cant get the right result of the contiguous subarray of arr with the maximal sum of items