79647589

Date: 2025-06-01 13:58:12
Score: 1
Natty:
Report link

Sort in difference. This data is very useful for the students Mini

sort(machines.begin(), machines.end(), [](const pair<int, int>& a, const pair<int, int>& b) {
    return (a.second - a.first) > (b.second - b.first);});

Input = {(6,7), (3,9), (8,6)}

Output = {(3,9),(6,7),(8,6)}

Sort vector of pairs by second element ascending

vector<pair<int,int>> v = {{1, 3}, {2, 2}, {3, 1}};
sort(v.begin(), v.end(), [](const pair<int,int>& a, const pair<int,int>& b) {
    return a.second < b.second;
});

Input:
[(1,3), (2,2), (3,1)]

Output:
[(3,1), (2,2), (1,3)]

Sort vector of pairs by first ascending, then second descending

vector<pair<int,int>> v = {{1, 2}, {1, 3}, {2, 1}};
sort(v.begin(), v.end(), [](const pair<int,int>& a, const pair<int,int>& b) {
    if (a.first != b.first)
        return a.first < b.first;
    return a.second > b.second;
});

Input:
[(1,2), (1,3), (2,1)]

Output:
[(1,3), (1,2), (2,1)]

Sort vector of integers by absolute value descending

vector<int> v = {-10, 5, -3, 8};
sort(v.begin(), v.end(), [](int a, int b) {
    return abs(a) > abs(b);
});

Input:
[-10, 5, -3, 8]

Output:
[-10, 8, 5, -3]

Filter a vector to remove even numbers

vector<int> v = {1, 2, 3, 4, 5};
v.erase(remove_if(v.begin(), v.end(), [](int x) {
    return x % 2 == 0;
}), v.end());

Input:
[1, 2, 3, 4, 5]

Output:
[1, 3, 5]

Square each element in vector

vector<int> v = {1, 2, 3};
transform(v.begin(), v.end(), v.begin(), [](int x) {
    return x * x;
});

Input:
[1, 2, 3]

Output:
[1, 4, 9]

Sort strings by length ascending

vector<string> v = {"apple", "dog", "banana"};
sort(v.begin(), v.end(), [](const string& a, const string& b) {
    return a.size() < b.size();
});

Input:
["apple", "dog", "banana"]

Output:
["dog", "apple", "banana"]

Min heap of pairs by second element

auto cmp = [](const pair<int,int>& a, const pair<int,int>& b) {
    return a.second > b.second;
};
priority_queue<pair<int,int>, vector<pair<int,int>>, decltype(cmp)> pq(cmp);

pq.push({1, 20});
pq.push({2, 10});
pq.push({3, 30});

while (!pq.empty()) {
    cout << "(" << pq.top().first << "," << pq.top().second << ") ";
    pq.pop();
}

Input:
Pairs pushed: (1,20), (2,10), (3,30)

Output:
(2,10) (1,20) (3,30)

Sort points by distance from origin ascending

vector<pair<int,int>> points = {{1,2}, {3,4}, {0,1}};
sort(points.begin(), points.end(), [](const pair<int,int>& a, const pair<int,int>& b) {
    return (a.first*a.first + a.second*a.second) < (b.first*b.first + b.second*b.second);
});

Input:
[(1,2), (3,4), (0,1)]

Output:
[(0,1), (1,2), (3,4)]

Sort strings ignoring case

vector<string> v = {"Apple", "banana", "apricot"};
sort(v.begin(), v.end(), [](const string& a, const string& b) {
    string la = a, lb = b;
    transform(la.begin(), la.end(), la.begin(), ::tolower);
    transform(lb.begin(), lb.end(), lb.begin(), ::tolower);
    return la < lb;
});

Input:
["Apple", "banana", "apricot"]

Output:
["Apple", "apricot", "banana"]

Reasons:
  • Contains signature (1):
  • Long answer (-1):
  • Has code block (-0.5):
  • Unregistered user (0.5):
  • Low reputation (1):
Posted by: Student