Advertisement
Sanlover

Done

Nov 12th, 2021
589
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.79 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5.   int N, uniqueCount = 0, *initialArray, *tempArray;
  6.  
  7.   cout << "Enter N: ";
  8.   cin >> N;
  9.   if (N <= 0) {
  10.     cout << "Array length must be positive.";
  11.     return 0;
  12.   }
  13.   initialArray = new int[N];
  14.   tempArray = new int[N];
  15.  
  16.   cout << endl << "Fill the array:" << endl;
  17.   for (int i = 0; i < N; i++) {
  18.     cout << " " << i << ") ";
  19.     cin >> initialArray[i];
  20.   }
  21.   cout << endl << "Your array:" << endl;
  22.   for (int i = 0; i < N; i++) {
  23.     cout << initialArray[i];
  24.     if (i + 1 < N) {
  25.       cout << ", ";
  26.     }
  27.   }
  28.  
  29.   for (int i = 0; i < N; i++) {
  30.     bool isFound = false;
  31.     for (int j = 0; j < uniqueCount && !isFound; j++) {
  32.       if (initialArray[i] == tempArray[j]) {
  33.         isFound = true;
  34.       }
  35.     }
  36.     if (!isFound) {
  37.       tempArray[uniqueCount++] = initialArray[i];
  38.     }
  39.   }
  40.  
  41.   delete[] initialArray;
  42.   initialArray = new int[uniqueCount];
  43.   for (int i = 0; i < uniqueCount; i++) {
  44.     initialArray[i] = tempArray[i];
  45.   }
  46.   delete[] tempArray;
  47.  
  48.   cout << endl << endl << "Array after deleting duplicates:" << endl;
  49.   for (int i = 0; i < uniqueCount; i++) {
  50.     cout << initialArray[i];
  51.     if (i + 1 < uniqueCount) {
  52.       cout << ", ";
  53.     }
  54.   }
  55.  
  56.   for (int i = uniqueCount; i >= 0; i--) {
  57.     bool isAnySwaps = false;
  58.     for (int j = 0; j < i - 1; j++) {
  59.       if (initialArray[j] % 10 > initialArray[j + 1] % 10) {
  60.         swap(initialArray[j], initialArray[j + 1]);
  61.         isAnySwaps = true;
  62.       }
  63.     }
  64.     if (!isAnySwaps) {
  65.       break;
  66.     }
  67.   }
  68.  
  69.   cout << endl << endl << "Array after sorting by smallest digit:" << endl;
  70.   for (int i = 0; i < uniqueCount; i++) {
  71.     cout << initialArray[i];
  72.     if (i + 1 < uniqueCount) {
  73.       cout << ", ";
  74.     }
  75.   }
  76.   return 0;
  77. }
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement