Skip to main content

Algorithm to find Max. sub sequence of length atleast l in linear time


Below is the code attached to calculate the max. subsequence of length atleast l in O(n) time.This algorithm works as follows:-
1.) calculate sum of first array L elements. and set running sum & maxsum equal to sum.
2.) for each element after L elements
  a) calculate sum=sum+array[i]-array[i-L];
  b) calculate runningsum=runningsum+array[i];
  c) update runningsum if sum is greater than running sum.
  d) update maxsubsequence if runningsum is greater than max subsequence.
Please find the below code,you can try and run the below code on http://www.compileonline.com/compile_c_online.php
Happy Coding

_________________________________________________________________________________

#include <stdio.h>
#include <string.h>
int findMaxSubsequence(int array[],int arrLength,int lenOfSubSequence){
    int i,maxSubsquence,sum=0,runningsum=0;
    for(i=0;i<arrLength;i++){
        if((i+1)<=lenOfSubSequence){
            sum=sum+array[i];
            maxSubsquence=sum;
            runningsum=sum;
        }else{
            sum=sum+array[i]-array[i-lenOfSubSequence];
             runningsum=runningsum+array[i];
        }
        if(sum>runningsum){
            runningsum=sum;
        }
        if(runningsum>maxSubsquence){
            maxSubsquence=runningsum;
        }
    }
    return maxSubsquence;
}

main()
{
  int arrLength,lenOfSubSequence,i;
  printf("Enter Number of elements\n");
  scanf("%d",&arrLength);
  printf("Enter Length of subsequence\n");
  scanf("%d",&lenOfSubSequence);
  int array[arrLength];
  for(i=0;i<arrLength;i++){
      scanf("%d",&array[i]);
  }
  if(lenOfSubSequence>arrLength){
      printf("No subsequence exist");
  }else{
      printf("Max Sum subsequence of length %d is %d",lenOfSubSequence,findMaxSubsequence(array,arrLength,lenOfSubSequence));
  }

}
_________________________________________________________________________________

Comments

Popular posts from this blog

MonoLithic Vs Microservice Architecture | which Architecture should i choose ?

From last few years ,microservices are an accelerating trend . Indeed, microservices approach offers tangible benefits including an increase in scalability, flexibility, agility, and other significant advantages. Netflix, Google, Amazon, and other tech leaders have successfully switched from monolithic architecture to microservices. Meanwhile, many companies consider following this example as the most efficient way for business growth. On the contrary, the monolithic approach is a default model for creating a software application. Still, its trend is going down because building a monolithic application poses a number of challenges associated with handling a huge code base, adopting a new technology, scaling, deployment, implementing new changes and others. So is the monolithic approach outdated and should be left in the past? And is it worth shifting the whole application from a monolith to microservices ? Will developing a microservices application help you reach you...

Long-Polling vs WebSockets vs Server-Sent Events

Long-Polling vs WebSockets vs Server-Sent Events  Long-Polling, WebSockets, and Server-Sent Events are popular communication protocols between a client like a web browser and a web server. First, let’s start with understanding what a standard HTTP web request looks like. Following are a sequence of events for regular HTTP request: Client opens a connection and requests data from the server. The server calculates the response. The server sends the response back to the client on the opened request. HTTP Protocol Ajax Polling Polling is a standard technique used by the vast majority of AJAX applications. The basic idea is that the client repeatedly polls (or requests) a server for data. The client makes a request and waits for the server to respond with data. If no data is available, an empty response is returned. Client opens a connection and requests data from the server using regular HTTP. The requested webpage sends requests to...

Installation of OpenCV2 / OpenCV3 with Python and Anaconda

This is first tutorial of the series beginning with installation instruction of opencv2 / opencv3 in python anaconda virtual Environment . We will come with lot of exciting blog like face detection and recognition in video/image/livestream , object or people tracking etc , So stay tuned and subscribe for more updates . We are installing it on MAC OS , you need below tools to setup OpenCV: Xcode Homebrew Anaconda OS X Step 1: Install Xcode Go to App Store , Search for Xcode Install it . Step 2: Install HomeBrew Open Terminal(Application->Utilites->terminal) Write this following in terminal: ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install )" brew install python Step 3: Install Anaconda Python Package Follow the Installation instructions, should be pretty standard, however Continuum has a guide here . Type Conda Info and check Installation . click here for Anaconda cheat sheet . Step 4: Create Conda Virtual Env and setup OpenCV You...