Intersection of Three Sorted Arrays

Given three integer arrays arr1, arr2 and arr3 sorted in strictly increasing order, return a sorted array of only the integers that appeared in all three arrays.

 

Example 1:

Input: arr1 = [1,2,3,4,5], arr2 = [1,2,5,7,9], arr3 = [1,3,4,5,8]
Output: [1,5]
Explanation: Only 1 and 5 appeared in the three arrays.

 

Constraints:


Solution:

class Solution {
    public List<Integer> arraysIntersection(int[] arr1, int[] arr2, int[] arr3) {
        int i = 0, j = 0, k = 0;
        List<Integer> result = new ArrayList();
        while (i < arr1.length && j < arr2.length && k < arr3.length) {
            int a = arr1[i], b = arr2[j], c = arr3[k];
            if (a == b && b == c) {
                i ++;
                j ++;
                k ++;
                result.add(a);
            } else if (a <= b && a <= c) {
                i ++;
            } else if (b <= a && b <= c) {
                j ++;
            } else {
                k ++;
            }
        }
        return result;
    }
}