Skip to content

Latest commit

 

History

History
67 lines (50 loc) · 1.26 KB

custom-comparator.md

File metadata and controls

67 lines (50 loc) · 1.26 KB

Custom Comparator

Sort Array By Parity

vector<int> sortArrayByParity(vector<int>& nums) {
    sort(nums.begin(), nums.end(), [&](int a, int b) {
        return (a % 2) < (b % 2); 
    });
    
    return nums;
}

Sort The People

vector<string> sortPeople(vector<string>& names, vector<int>& heights) {    
    int N = names.size();
    vector<int> idx(N, 0);
    iota(idx.begin(), idx.end(), 0);
    
    sort(idx.begin(), idx.end(), [&](int i, int j) {
        return heights[i] > heights[j]; 
    });
    
    vector<string> ans;
    
    for (auto &a : idx) {
        ans.push_back(names[a]);
    }
    
    return ans;
}

Bitwise XOR of All Pairings

View Code
class Solution {
public:
    int xorAllNums(vector<int>& nums1, vector<int>& nums2) {
        
        int ans = 0;
        int M = nums1.size(), N = nums2.size();
        
        if (N & 1) {
            ans = accumulate(nums1.begin(), nums1.end(), 0, [](int x, int y) {
                return x ^ y;
            });
        }
        
        if (M & 1) {
            ans ^= accumulate(nums2.begin(), nums2.end(), 0, [](int x, int y) {
                return x ^ y;
            });
        }
        
        return ans;
    }
};