Advertisement
totobac

Untitled

Jan 15th, 2021
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.02 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. bool isThereQuadruplet(double array[], double sum, int i, int n)
  4. {
  5.     if (i == n - 4)
  6.     {
  7.         for (size_t r = i + 3; r < n; r++)
  8.         {
  9.             int m = r;
  10.             int counter = 1;
  11.             while (m < n)
  12.             {
  13.                 if (array[i] + array[i + counter] + array[i + counter + 1] + array[r] == sum)
  14.                     return true;
  15.                 m++;
  16.             }
  17.         }
  18.         return false;
  19.     }
  20.  
  21.     else
  22.     {
  23.             for (size_t r = i+3; r < n; r++)
  24.             {
  25.                 int m = r;
  26.                 int counter = 1;
  27.                 while (m < n)
  28.                 {
  29.                     if (array[i] + array[i + counter] + array[i + counter + 1] + array[r] == sum)
  30.                         return true;
  31.                     m++;
  32.                     counter++;
  33.                 }
  34.             }
  35.     isThereQuadruplet(array, sum, i + 1, n);
  36.     }
  37. }
  38. int main()
  39. {
  40.     int n, sum;
  41.     cout << "Enter n: ";
  42.     cin >> n;
  43.     double* array = new double[n];
  44.     for (size_t i = 0; i < n; i++)
  45.     {
  46.         cin >> array[i];
  47.     }
  48.  
  49.     cout << "Enter the sum: ";
  50.     cin >> sum;
  51.     if (isThereQuadruplet(array, sum, 0, n) == true)
  52.         cout << "Quadruplet exists";
  53.     else cout << "Quadruplet doesn't exist";
  54.     delete[] array;
  55. }
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement