package com.fishercoder.solutions;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 1213. 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:
 *
 * 1 <= arr1.length, arr2.length, arr3.length <= 1000
 * 1 <= arr1[i], arr2[i], arr3[i] <= 2000
 * */
public class _1213 {
    public static class Solution1 {
        public List<Integer> arraysIntersection(int[] arr1, int[] arr2, int[] arr3) {
            List<Integer> result = new ArrayList<>();
            Set<Integer> set2 = Arrays.stream(arr2).boxed().collect(Collectors.toSet());
            Set<Integer> set3 = Arrays.stream(arr3).boxed().collect(Collectors.toSet());
            for (int i = 0; i < arr1.length; i++) {
                if (i >= arr2.length || i >= arr3.length) {
                    break;
                } else {
                    if (set2.contains(arr1[i]) && set3.contains(arr1[i])) {
                        result.add(arr1[i]);
                    }
                }
            }
            return result;
        }
    }
}

@@ -78,95 +78,12 @@
 til.
-Arrays;%0Aimport java.util.List;%0Aimport java.util.Set;%0Aimport java.util.stream.Collectors
+List
 ;%0A%0A/
@@ -644,16 +644,149 @@
 tion1 %7B%0A
+        /**credit: https://leetcode.com/problems/intersection-of-three-sorted-arrays/discuss/397603/Simple-Java-solution-beats-100*/%0A
         
@@ -915,10 +915,8 @@
 List
-%3C%3E
 ();%0A
@@ -931,172 +931,40 @@
     
-Set%3CInteger%3E set2 = Arrays.stream(arr2).boxed().collect(Collectors.toSet());%0A            Set%3CInteger%3E set3 = Arrays.stream(arr3).boxed().collect(Collectors.toSet())
+int i = 0;%0A            int j = 0
 ;%0A  
@@ -977,104 +977,155 @@
     
-for (
 int 
-i
+k
  = 0;
- i %3C arr1.length; i++) %7B%0A                if (i %3E= arr2.length %7C%7C i %3E= arr3.length
+%0A            while (i %3C arr1.length && j %3C arr2.length && k %3C arr3.length) %7B%0A                if (arr1%5Bi%5D == arr2%5Bj%5D && arr1%5Bi%5D == arr3%5Bk%5D
 ) %7B%0A
@@ -1148,13 +1148,27 @@
     
-b
 re
-ak
+sult.add(arr1%5Bi%5D)
 ;%0A  
@@ -1185,159 +1185,262 @@
     
-%7D else %7B%0A                    if (set2.contains(arr1%5Bi%5D) && set3.contains(arr1%5Bi%5D)) %7B%0A                        result.add(arr1%5Bi%5D);%0A                    %7D
+    i++;%0A                    j++;%0A                    k++;%0A                %7D else if (arr1%5Bi%5D %3C arr2%5Bj%5D) %7B%0A                    i++;%0A                %7D else if (arr2%5Bj%5D %3C arr3%5Bk%5D) %7B%0A                    j++;%0A                %7D else %7B%0A                    k++;
 %0A   

refactor 1213