fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4. using namespace std;
  5.  
  6. int main() {
  7. vector<int> nums = {1, 2, 2, 1};
  8. int k = 1;
  9.  
  10. int count = 0;
  11. int n = nums.size();
  12.  
  13. for(int i = 0; i < n; i++) {
  14. for(int j = i + 1; j < n; j++) {
  15. if(abs(nums[i] - nums[j]) == k) {
  16. count++;
  17. }
  18. }
  19. }
  20.  
  21. cout << count << endl;
  22. return 0;
  23. }
  24.  
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
4