Problem solution · C++

Count Smaller Elements with Opposite Parity

Count Smaller Elements with Opposite Parity: a C++ solution using segment tree or range structure. Learn the idea, check the complexity, and read the full code, with credit to Kamyu LeetCode Solutions.

Technique
Segment tree or range structure
Source
Kamyu LeetCode Solutions
Length
54 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Segment tree or range structure

For Count Smaller Elements with Opposite Parity, the implementation stores interval information in a range-query data structure so updates and queries avoid rescanning the full input.

  1. Choose the aggregate stored for each interval or prefix.
  2. Build or initialize the structure from the input.
  3. Apply updates and combine the affected nodes to answer each query.

Code notes

  • 54 lines of C++ from the credited upstream file count-smaller-elements-with-opposite-parity.cpp.
  • The implementation visibly relies on sequence storage, hash lookup.
  • 4 loop blocks detected.

Complexity

Count the build once, then multiply the logarithmic update or query path by the number of operations.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from Kamyu LeetCode Solutions by kamyu104 and is used under the MIT licence.

Full codeCount Smaller Elements with Opposite Parity · C++C++
Use this to learn the idea, then write your own version.
// Time:  O(nlogn)// Space: O(n) // sort, coordinate compression, fenwick treeclass BIT {public:    BIT(int n) : bit_(n + 1) {  // 0-indexed    }        void add(int i, int val) {        ++i;        for (; i < size(bit_); i += lower_bit(i)) {            bit_[i] += val;        }    }     int query(int i) const {        ++i;        int total = 0;        for (; i > 0; i -= lower_bit(i)) {            total += bit_[i];        }        return total;    } private:    inline int lower_bit(int i) const {        return i & -i;    }        vector<int> bit_;}; class Solution {public:    vector<int> countSmallerOppositeParity(vector<int>& nums) {        vector<int> sorted_nums(nums);        ranges::sort(sorted_nums);        sorted_nums.erase(unique(begin(sorted_nums), end(sorted_nums)), end(sorted_nums));        unordered_map<int, int> val_to_idx;        for (int i = 0; i < size(sorted_nums); ++i) {            val_to_idx[sorted_nums[i]] = i;        }        vector<BIT> bit(2, BIT(size(val_to_idx)));        vector<int> result(size(nums));        for (int i = size(nums) - 1; i >= 0; --i) {            const auto& idx = val_to_idx[nums[i]];            result[i] = bit[1 ^ (nums[i] % 2)].query(idx - 1);            bit[nums[i] % 2].add(idx, 1);        }        return result;    }}; 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗