Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8778705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:35:44+00:00 2026-06-13T19:35:44+00:00

The subarray contains both positive and negative numbers. You have to find a maximum

  • 0

The subarray contains both positive and negative numbers. You have to find a maximum sum subarray such that the length of the sub-array is greater than or equal to k.

Here is my code in c++ using Kadane’s algorithm.

#include <iostream>

using namespace std;

int main(){

    int n,k;
    cin >> n >> k;
    int array[n];
    int sum = 0;
    int maxsum = 0;
    int beststarts[n];

    for(int i = 0;i < n; i++){
            cin >> array[i];
    }

    for(int i = 0;i < k-1;i ++){
            sum = sum+array[i];
            beststarts[i] = 0;
    }


    for(int i =  k-1;i < n; i++){ //best end search with min length;
            sum = sum+array[i];
            int testsum = sum;
            if(i > 0){
            beststarts[i] = beststarts[i-1];
            }
            for(int j = beststarts[i] ;i-j > k-1;j ++){
                    testsum = testsum - array[j];
                    if(testsum > sum){
                            beststarts[i] = j+1;
                            sum = testsum;
                    }
            }
            if(sum > maxsum){
                    maxsum = sum;
            }
    }

    cout << maxsum;

    return 0;
}

My code is working fine but it is very slow, and i cant think of any ways to improve my code. I have also read this question Find longest subarray whose sum divisible by K but that is not what i want, the length can be greater than k also.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T19:35:46+00:00Added an answer on June 13, 2026 at 7:35 pm

    Solution based on this answer

    Live demo

    #include <algorithm>
    #include <iterator>
    #include <iostream>
    #include <numeric>
    #include <ostream>
    #include <utility>
    #include <vector>
    
    // __________________________________________________
    
    template<typename RandomAccessIterator> typename std::iterator_traits<RandomAccessIterator>::value_type
    max_subarr_k(RandomAccessIterator first,RandomAccessIterator last,int k)
    {
        using namespace std;
        typedef typename iterator_traits<RandomAccessIterator>::value_type value_type;
        if(distance(first,last) < k)
            return value_type(0);
        RandomAccessIterator tail=first;
        first+=k;
        value_type window=accumulate(tail,first,value_type(0));
        value_type max_sum=window, current_sum=window;
        while(first!=last)
        {
            window += (*first)-(*tail) ;
            current_sum = max( current_sum+(*first), window );
            max_sum = max(max_sum,current_sum);
            ++first;
            ++tail;
        }
        return max_sum;
    }
    
    // __________________________________________________
    
    template<typename E,int N>
    E *end(E (&arr)[N])
    {
        return arr+N;
    }
    
    int main()
    {
        using namespace std;
        int arr[]={1,2,4,-5,-4,-3,2,1,5,6,-20,1,1,1,1,1};
        cout << max_subarr_k(arr,end(arr),4) << endl;
        cout << max_subarr_k(arr,end(arr),5) << endl;
    }
    

    Output is:

    14
    11
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a multidimensional array that contains a variable number of sub arrays. Each
I have a array called $A which contains only non zero positive numbers. Now
Given an array of real-valued numbers, A[1..n,1..n] , I wish to find the sub-array
Input: An array of n positive and negative numbers and a number k. Output:
There are algorithms for detecting the maximum subarray within an array (both contiguous and
I wrote the following code to find the contiguous subarray with maximum sum, which
Possible Duplicate: Find the maximum interval sum in a list of real numbers. I
I am making a program which finds the maximum-length subarray whose sum is closest
Ruby has a select method that takes an array and returns a subarray consisting
i have a multi dimension array with sub array having repeated values of '

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.