I visited numberous website and algorithms, i am still not understand what is the different between ++i and i++.
And also, I don’t know how to use it, which situation should we use ++i instead of i++. Can someone explain it? Thank you so much!!!
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int kadane(const vector<int>& nums) {
if (nums.empty()) return 0;
int max_current = nums[0], max_global = nums[0];
for (size_t i = 1; i < nums.size(); ++i) {
max_current = max(nums[i], max_current + nums[i]);
if (max_current > max_global) {
max_global = max_current;
}
}
return max_global;
}
int main() {
vector<int> nums = { -2, 1, -3, 4, -1, 2, 1, -5, 4 };
cout << "Max sum of contiguous subarray: " << kadane(nums) << endl;
return 0;
}
This is Kadane’s Algorithm, I saw ChatGPT using ++i instead of i++, I wonder why the bot used it
New contributor
Andrew William is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.