🎉 One-stop destination for all your technical interview Preparation 🎉
int findNumberOfLIS(vector<int>& arr)
{
int n = arr.size();
vector<int> dp(n, 1);
vector<int> cnt(n, 1); // Stores the count of LIS ending at i
int maxLen = 1;
for (int i = 0; i < n; i++) {
for (int prev = 0; prev < i; prev++) {
if (arr[prev] < arr[i]) {
if (dp[prev] + 1 > dp[i]) {
dp[i] = dp[prev] + 1;
cnt[i] = cnt[prev]; // Give count
}
else if (dp[prev] + 1 == dp[i]) { // Added this condition
cnt[i] += cnt[prev];
}
}
}
maxLen = max(maxLen, dp[i]);
}
int ans = 0;
for (int i = 0; i < n; i++) {
if (dp[i] == maxLen) {
ans += cnt[i]; // Add all LIS ending at i
}
}
return ans;
}