SPOJ SBANK problem

0

This spoj problem may seem difficult in the beginning, but all that is required is a good knowledge of STL in C++. It simply required obtaining the account number from the user and storing it in a map. After this, just iterate through the map and print the values.

[gist https://gist.github.com/niviusha/5944486]

MaximumSubArray – Dynamic programming

0

//aim of the program is to find the maximum subarray (contiguous sequence of number) in an array containing both positive and negative numbers

#include<iostream>
using namespace std;
int main(){
    int n;
    cout<<“enter the number of elements”;
    cin>>n;
    int arr[n],max[n],maxsum=0;
    cout<<“enter the elements”;
    for(int i=0;i<n;i++){
        cin>>arr[i];
    }
    if(arr[0]>0)
      max[0]=arr[0];
      else
      max[0]=0;
      for(int i=1;i<n;i++){
          if((max[i-1]+arr[i])>0){
              max[i]=max[i-1]+arr[i];
          }
              else
              max[i]=0;
          
      }
      for(int i=1;i<n;i++){
          if(max[i]>maxsum){
              maxsum=max[i];
          }
      }
      cout<<“\n maximun sum is “<<maxsum;
  }