Advertisement
informaticage

Simple recursion on arrays

Dec 3rd, 2023 (edited)
971
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.13 KB | None | 0 0
  1. #include <cstring>
  2. #include <iostream>
  3.  
  4. void solve_rec_h(int V[], int T[], int length, int i, int even, int odd);
  5. void solve_rec(int V[], int T[], int length);
  6. int max_occurrence_re(int V[], int length);
  7. int max_occurrence_rec_h(int V[], int length, int i, int j, int max_occ, int count);
  8. int count_couples_h(int V[], int length, int i, int j, int max_occ, int count);
  9. int count_character(char V[], char c, int size, int i);
  10. int count_couples(char V[], int size);
  11. int count_couples_h(char V[], int size, int i);
  12.  
  13. void solve_rec(int V[], int T[], int length) {
  14.     return solve_rec_h(V, T, length, 0, 0, length - 1);
  15. }
  16.  
  17. void solve_rec_h(int V[], int T[], int length, int i, int even, int odd) {
  18.     if (i == length) return;
  19.  
  20.     if (V[i] % 2 == 0) {
  21.         T[even] = V[i];
  22.         even++;
  23.     } else {
  24.         T[odd] = V[i];
  25.         odd--;
  26.     }
  27.  
  28.     return solve_rec_h(V, T, length, i + 1, even, odd);
  29. }
  30.  
  31. int max_occurrence(int V[], int length) {
  32.     int max_occ = 0;
  33.  
  34.     for (int i = 0; i < length; i++) {
  35.         int count = 0;
  36.  
  37.         for (int j = i; j < length; j++) {
  38.             if (V[i] == V[j]) {
  39.                 count++;
  40.             }
  41.         }
  42.  
  43.         if (count > max_occ) {
  44.             max_occ = count;
  45.         }
  46.     }
  47.  
  48.     return max_occ;
  49. }
  50.  
  51. int max_occurrence_rec(int V[], int length) {
  52.     return max_occurrence_rec_h(V, length, 0, 0, 0, 0);
  53. }
  54.  
  55. int max_occurrence_rec_h(int V[], int length, int i, int j, int max_occ,
  56.                          int count) {
  57.     std::cout << i << " " << j << std::endl;
  58.     if (i == length) return max_occ;
  59.  
  60.     if (j == length) {
  61.         if (count > max_occ) max_occ = count;
  62.  
  63.         return max_occurrence_rec_h(V, length, i + 1, i + 1, max_occ, 0);
  64.     }
  65.  
  66.     if (V[i] == V[j]) {
  67.         count++;
  68.     }
  69.  
  70.     return max_occurrence_rec_h(V, length, i, j + 1, max_occ, count);
  71. }
  72.  
  73. void solve(int V[], int T[], int length) {
  74.     int even = 0, odd = length - 1;
  75.  
  76.     for (int i = 0; i < length; i++) {
  77.         // For each item in V
  78.         if (V[i] % 2 == 0) {
  79.             T[even] = V[i];
  80.             even++;
  81.         } else {
  82.             T[odd] = V[i];
  83.             odd--;
  84.         }
  85.     }
  86. }
  87.  
  88. int count_character(char V[], char c, int size, int i) {
  89.     if (i == size) return 0;
  90.  
  91.     if (V[i] == c) {
  92.         return count_character(V, c, size, i + 1) + 1;
  93.     }
  94.  
  95.     return count_character(V, c, size, i + 1);
  96. }
  97.  
  98. int count_couples(char V[], int size) {
  99.     return count_couples_h(
  100.         V, size, 0
  101.     ) / 2;
  102. }
  103.  
  104. int count_couples_h(char V[], int size, int i) {
  105.     if (i == size) return 0;
  106.     int result = count_character(V, V[i], size, 0);
  107.  
  108.     if (result == 2) {
  109.         return count_couples_h(V, size, i + 1) + 1;
  110.     }
  111.  
  112.     return count_couples_h(V, size, i + 1);
  113. }
  114.  
  115. int main() {
  116.     int ARR[] = {1, 4, 2, 4, 7, 2, 2, 2, 0};
  117.     int SORTED[9];
  118.  
  119.     char T[] = "tonno_papera";
  120.  
  121.     std::cout << count_couples(T, strlen(T));
  122.  
  123.     // solve_rec(ARR, SORTED, 9);
  124.  
  125.     // std::cout << "Max occ: " << max_occurrence_rec(ARR, 9);
  126.  
  127.     // for (int i = 0; i < 9; i++) {
  128.     //     std::cout << SORTED[i] << " ";
  129.     // }
  130.  
  131.     return 0;
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement