MohamedAbdel3al

Replace MinMax

Jul 29th, 2021
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std ;
  3. typedef long long ll ;
  4. int main() {
  5. ios::sync_with_stdio(false) ; cout.tie(NULL) ; cin.tie(NULL) ;
  6. int n ; cin >> n ;
  7. int minidx , maxidx ;
  8. int arr[n] ;
  9. for (int i = 0; i < n; i++) cin >> arr[i] ;
  10. int max = arr[0] ;
  11. int min = arr[0] ;
  12. for (int i = 0; i < n; i++) {
  13. if (arr[i] < min) {
  14. min = arr[i] , minidx = i ;
  15. }
  16. if (arr[i] > max) {
  17. max = arr[i] , maxidx = i ;
  18. }
  19. }
  20. swap(arr[minidx] , arr[maxidx]) ;
  21. for (int i = 0; i < n; i++) cout << arr[i] << " " ;
  22.  
  23. return 0;
  24. }
Advertisement
Add Comment
Please, Sign In to add comment