Solving below problem and getting error:
Problem statement:
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.
Error :
error: no match for 'operator=' (operand types are 'std::vector<int>::iterator {aka __gnu_cxx::__normal_iterator<int*, std::vector<int> >}' and '__gnu_cxx::__normal_iterator<const int*, std::vector<int> >')
it = A.begin() + a;
int GetMaxSum(vector<int> V)
{
vector<int>::iterator it;
int n = V.size();
int a = 0;
int s = 0;
while (a < n)
{
int sum = 0;
int temp = 0;
it = V.begin() + a;
for (int i = 0; i < (n-a); i++)
{
temp = temp + (*it);
sum = ((sum > temp) ? sum : temp);
it++;
}
s = ((s < sum) ? sum : s);
a++;
}
return s;
}
NOTE : I know this is not a best solution as time complexity is n*n.
I am trying to solve it in better time complexity.
Error gone when define const iterator instead normal iterator.
i.e.
vector<int>::const_iterator it;
I am still not sure why it needed const iterator.
Would any one like reply here...? :)