PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, August 17, 2022

[FIXED] Why this code is returning wrong value? inputs= 5 ,{ -1,4,-6,7,-4}

 August 17, 2022     c++, kadanes-algorithm, output     No comments   

Issue

#include<iostream>
#include<climits>
using namespace std;
int main(){
    int n;
    cin>>n;
    int a[n];
    for(int i=0;i<n;++i){
        cin>>a[n];
    }                                       //array instillisation
    int cursum=0;
    int maxsum=INT_MIN;
    for(int i=0;i<n;++i){
             cursum+=a[i];
             if(cursum<0){
                cursum=0;
             }
             maxsum=max(cursum,maxsum);
                }
      cout<<maxsum<<endl;               
      return 0; 
}

//this code is for maximum subarray problem using kadane's algo.My compiler is retrurning wrong output


Solution

In cin you are doing incorrect operation it should be

 cin>> a[i];

what you are doing is taking the value of a[n]



Answered By - Hrishikesh
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing