#include <iostream>
#include <limits>
int MIN = std::numeric_limits<int>::min()
using namespace std ;
void findMaxSubArray(int inputArray[] , int n )
{
int maxStartIndex=0;
int maxEndIndex=0;
int maxSum = MIN ;
int cumulativeSum= 0;
int maxStartIndexUntilNow=0;
for (int currentIndex = 0; currentIndex < n ; currentIndex++)
{
int eachArrayItem = inputArray[currentIndex];
cumulativeSum+=eachArrayItem;
if(cumulativeSum>maxSum)
{
maxSum = cumulativeSum;
maxStartIndex=maxStartIndexUntilNow;
maxEndIndex = currentIndex;
}
else if (cumulativeSum<0)
{
maxStartIndexUntilNow=currentIndex+1;
cumulativeSum=0;
}
}
cout << "Max sum : "<< maxSum << "\n" ;
cout << "Max start index : "<< maxStartIndex << "\n" ;
cout << "Max end index : "<< maxEndIndex << "\n" ;
}
int main()
{
int intArr[] = {-1,3,-1,-1,-1,-1,-1,-1 } ;
//int intArr[] = {-1, 3, -5, 4, 6, -1, 2, -7, 13, -3};
//int intArr[]={-6,-2,-3,-4,-1,-5,-5};
findMaxSubArray(intArr,8);
return 0 ;
}
ここで与えられた実装が正しいかどうか懐疑的だったので、正確に C++ で実装しましたが、上記のテスト ケースでは機能しません。アルゴリズムのどこが間違っているのかわかりませんか?