Advertisement
tosip

Untitled

Jun 1st, 2021
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. long double hasElement(int array[], int size)
  6. {
  7.  
  8.     int l = 0;
  9.     int r = size - 1;
  10.     sort(array, array + size);
  11.     while (l < r)
  12.     {
  13.         if (array[l] + array[r] == 2020)
  14.         {
  15.             return array[l] * array[r];
  16.         }
  17.         else if (array[l] + array[r] < 2020)
  18.         {
  19.             l++;
  20.         }
  21.         else if (array[l] + array[r] > 2020)
  22.         {
  23.             r--;
  24.         }
  25.     }
  26.     return 0;
  27. }
  28.  
  29. int main()
  30. {
  31.     int A[] = {1721,
  32.                979,
  33.                366,
  34.                299,
  35.                675,
  36.                1456};
  37.     int arr_size = sizeof(A) / sizeof(A[0]);
  38.  
  39.     // Function calling
  40.     cout << hasElement(A, arr_size) << endl;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement