Skip to content

Commit

Permalink
Create 9 February Maximum path sum from any node (#711)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chayandas07 authored Feb 9, 2025
2 parents 02a56e3 + 74275ed commit 30cbd1d
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions 9 February Maximum path sum from any node
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public:
long long countBadPairs(vector<int>& nums) {
long long n = nums.size();
long long totalPair = (n * (n - 1) / 2);
long long cnt = 0;
unordered_map<int, int> mp;
for (int i = 0; i < n; i++) {
int x = nums[i] - i;
if (mp.find(x) != mp.end()) {
cnt += mp[x];
}
mp[x]++;
}
return totalPair - cnt;
}
};

0 comments on commit 30cbd1d

Please sign in to comment.