Hello,
As I was trying to solve ZigZag problem on topcoder the weirdest thing happend.
#include <bits/stdc++.h>
using namespace std;
class ZigZag {
public:
int longestZigZag(vector<int> arr) {
int dp[55], state[55];
memset(dp, 1, sizeof dp);
cout << dp[0] << endl;
memset(state, 0, sizeof state);
for (int i = 0; i < arr.size(); ++i) {
for (int j = 0; j < i; ++j) {
if ((j == 0 || (arr[i] - arr[j] < 0 && state[j] > 0)
|| (arr[i] - arr[j] > 0 && state[j] < 0))
&& dp[j] + 1 > dp[i])
dp[i] = dp[j] + 1, state[i] = arr[i] - arr[j];
}
}
return dp[arr.size() - 1];
}
};
as I ran the given code, the cout statement gives 16843009
.
I removed it and initialized every element int the first loop and it worked.