Skip to content

Instantly share code, notes, and snippets.

@joechai93
Created May 28, 2021 23:44
Show Gist options
  • Save joechai93/cb033c7dc698189855ae5c93b7d0b69b to your computer and use it in GitHub Desktop.
Save joechai93/cb033c7dc698189855ae5c93b7d0b69b to your computer and use it in GitHub Desktop.
Missing value in ordered vector problem
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <cmath>
int main()
{
std::vector<int> A = {1,3, 2,4,7,6};
std::sort(A.begin(), A.end());
std::vector<int> B(A.size());
std::iota(B.begin(), B.end(), 1);
std::vector<int> C;
std::transform(A.begin(), A.end(), B.begin(), std::back_inserter(C),
[](double a, double b) { return fabs(a-b); });
std::cout << (std::find(C.begin(), C.end(), 1))-C.begin() << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment