Advertisement
193030

01. Hackerrank vector

Jun 20th, 2021
1,221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.18 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #include <algorithm>    // std::count
  4.  
  5. #include <vector>       // std::vector
  6.  
  7. using namespace std;
  8.  
  9. string ltrim(const string & );
  10. string rtrim(const string & );
  11.  
  12. /*
  13.  * Complete the 'findNumber' function below.
  14.  *
  15.  * The function is expected to return a STRING.
  16.  * The function accepts following parameters:
  17.  *  1. INTEGER_ARRAY arr
  18.  *  2. INTEGER k
  19.  */
  20.  
  21. string findNumber(vector < int > arr, int k) {
  22.  
  23.   if (std::find(arr.begin(), arr.end(), k) != arr.end()) {
  24.     cout << "YES " << endl;
  25.  
  26.   } else {
  27.     cout << "NO " << endl;
  28.  
  29.   }
  30.  
  31. }
  32.  
  33. int main() {
  34.   vector < int > inputArr;
  35.   int n = 0;
  36.   cin >> n;
  37.   // n--;
  38.   cin.ignore();
  39.   while (n--) {
  40.     int x;
  41.     cin >> x;
  42.     inputArr.push_back(x);
  43.  
  44.   }
  45.   int k;
  46.   cin >> k;
  47.   findNumber(inputArr, k);
  48.   return 0;
  49. }
  50.  
  51. string ltrim(const string & str) {
  52.   string s(str);
  53.  
  54.   s.erase(
  55.     s.begin(),
  56.     find_if(s.begin(), s.end(), not1(ptr_fun < int, int > (isspace)))
  57.   );
  58.  
  59.   return s;
  60. }
  61.  
  62. string rtrim(const string & str) {
  63.   string s(str);
  64.  
  65.   s.erase(
  66.     find_if(s.rbegin(), s.rend(), not1(ptr_fun < int, int > (isspace))).base(),
  67.     s.end()
  68.   );
  69.  
  70.   return s;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement