fork download
  1. // Online C++ compiler to run C++ program online
  2. #include <iostream>
  3. using namespace std;
  4. int main() {
  5. int arr[5]={1,7,3,10,6};
  6. int sum = 17;
  7. int i=0,j=4;
  8. for (i=0;i<5;i++)
  9. {
  10. cout<<"i="<<i<<","<<"j="<<j<<endl;
  11.  
  12. if (i==j)
  13. {
  14. i=0;
  15. j--;
  16. }
  17. if (i!=j && arr[i]+arr[j]==sum)
  18. {
  19. cout<<i+1<<","<<j+1<<endl;
  20. break;
  21. }
  22. else if (i>j)
  23. break;
  24.  
  25. }
  26. if (j==0)
  27. cout<<"Not found"<<endl;
  28. return 0;
  29.  
  30. }
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
i=0,j=4
i=1,j=4
i=2,j=4
i=3,j=4
i=4,j=4
i=1,j=3
2,4