Advertisement
Sanlover

Untitled

Nov 18th, 2021
843
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.76 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5.   int N, min, max;
  6.   cout << "Enter N: ";
  7.   cin >> N;
  8.   if (N <= 0 && N % 2 != 0) {
  9.     cout << "N must be positive and even";
  10.     return 0;
  11.   }
  12.   int* array = new int[N];
  13.   cout << "Fill the array:" << endl;
  14.  
  15.   min = max = N / 2;
  16.   for (int i = 0; i < N; i++) {
  17.     cout << i + 1 << ") ";
  18.     cin >> array[i];
  19.     if (i < N / 2) {
  20.       if (array[min] > array[i]) {
  21.         min = i;
  22.       }
  23.     } else {
  24.       if (array[i] > array[max]) {
  25.         max = i;
  26.       }
  27.     }
  28.     swap(array[max], array[min]);
  29.   }
  30.  
  31.   cout << endl
  32.        << "Your array afer swapping min from first half to max from second:";
  33.   for (int i = 0; i < N; i++) {
  34.     cout << array[i] << ' ';
  35.   }
  36.   return 0;
  37. }
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement