Find common elements in 3 sorted arrays

1 min read

https://practice.geeksforgeeks.org/problems/common-elements1132/1

class Solution {
public:
  vector<int> common_element(vector<int> v1, vector<int> v2) {
    vector<int> v;
    sort(v1.begin(), v1.end());
    sort(v2.begin(), v2.end());

    int i = 0, j = 0;
    int n1 = v1.size(), n2 = v2.size();
    while (i < n1 && j < n2) {
      if (v1[i] == v2[j]) {
        v.push_back(v1[i]);
        i++;
        j++;
      } else if (v1[i] < v2[j]) {
        i++;
      } else {
        j++;
      }
    }

    return v;
  }
};
Find all pairs on integer array whose sum is equal to K
Rearrange the array in alternating positive and negative items with O(1) extra space