fork download
  1. #include <iostream>
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4. using ll = long long;
  5. #define fo(i,start,end) for(int i=start;i<end;i++)
  6.  
  7. vector<pair<int,int>> FindOptimalPairs(vector<int>&arr,int k){
  8. vector<pair<int,int>>result;
  9. fo(i,0,5){
  10. fo(j,i+1,5){
  11. if(abs(arr[i] - arr[j]) == k){
  12. result.push_back({i,j});
  13. }
  14. }
  15. }
  16. //for loop ends here
  17. return result;
  18. }
  19.  
  20.  
  21. int main() {
  22. // your code goes here
  23. vector<int>arr = {1,5,3,4,2};
  24. ll k = 2;
  25. vector<pair<int,int>>ans = FindOptimalPairs(arr,k);
  26. cout<<"Pairs are:"<<endl;
  27. for(pair<int,int>& it:ans){
  28. int fp = it.first;
  29. int sp = it.second;
  30. cout<<"{"<<fp+1<<","<<sp+1<<"},"<<endl; //one based indexing
  31. }
  32. return 0;
  33. }
Success #stdin #stdout 0s 5324KB
stdin
Standard input is empty
stdout
Pairs are:
{1,3},
{2,3},
{4,5},