csansoon

P7.12 P12675 Common elements

Nov 14th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.73 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5.  
  6. int common_elements(const vector<int>& X, const vector<int>& Y) {
  7.         int common = 0;
  8.         int x = X.size();
  9.         int y = Y.size();
  10.         int i = 0;
  11.         int j = 0;
  12.         while (i <= x-1 && j <= y-1) {
  13.                 if (X[i] == Y[j]) {
  14.                         ++common;
  15.                         ++i;
  16.                         ++j;
  17.                 }
  18.                 else if (X[i] > Y[j]) {
  19.                         ++j;
  20.                 }
  21.                 else if (X[i] < Y[j]) {
  22.                         ++i;          
  23.                 }
  24.         }
  25.        
  26.         return common;
  27. }
  28.  
  29. // (c) Carlos Sansón (Best pro1 delegate ever for sure) @csansoon
Add Comment
Please, Sign In to add comment