fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <set>
  4. #include <algorithm>
  5.  
  6. bool cmp_with_sort(std::vector<int> arr1, std::vector<int> arr2)
  7. {
  8. if(arr1.size()!=arr2.size())
  9. return false;
  10. std::sort(arr1.begin(), arr1.end());
  11. std::sort(arr2.begin(), arr2.end());
  12. bool arrs_equal = true;
  13. for(int i=0;arrs_equal && i<arr1.size();++i)
  14. if(arr1[i]!=arr2[i])
  15. arrs_equal=false;
  16. return arrs_equal;
  17. }
  18.  
  19. bool cmp_with_sets(const std::vector<int>& arr1, const std::vector<int>& arr2)
  20. {
  21. if(arr1.size()!=arr2.size())
  22. return false;
  23. std::multiset<int> set1(arr1.begin(),arr1.end());
  24. std::multiset<int> set2(arr2.begin(), arr2.end());
  25. return set1==set2;
  26. }
  27.  
  28. bool cmp_nested_loops(const std::vector<int>& arr1, const std::vector<int>& arr2)
  29. {
  30. if(arr1.size()!=arr2.size())
  31. return false;
  32. for(int i=0;i<arr1.size();i++)
  33. {
  34. bool found = false;
  35. for(int j=0;!found && j<arr2.size();j++)
  36. if(arr1[i]==arr2[j])
  37. found = true;
  38. if(!found)
  39. return false;
  40. }
  41. return true;
  42.  
  43. }
  44.  
  45. int main() {
  46. std::vector<int> arr1 {4,1,2,1};
  47. std::vector<int> arr2 {1,4,1,2};
  48.  
  49. std::cout << (cmp_with_sort(arr1,arr2)?"Equal":"Not equal") << "\n";
  50. std::cout << (cmp_with_sets(arr1,arr2)?"Equal":"Not equal") << "\n";
  51. std::cout << (cmp_nested_loops(arr1,arr2)?"Equal":"Not equal") << "\n";
  52. return 0;
  53. }
Success #stdin #stdout 0s 5656KB
stdin
Standard input is empty
stdout
Equal
Equal
Equal