Questions on Arrays Part-4

Photo by AltumCode on Unsplash

Questions on Arrays Part-4

Q17. Given 3 arrays sorted in increasing order. Find the common elements in all 3 arrays.

A17. In this question, we will use a 3 pointer approach.

public class CommonElements {
    static void common(int[] A, int[] B, int[] C){
        int a=0,b=0,c=0;
        List<Integer> list = new ArrayList<>();
        while(a<A.length && b< B.length && c<C.length){
            if(A[a] == B[b] && B[b]== C[c]){
                list.add(A[a]);
                a++;
                b++;
                c++;
            }
            if(A[a]<B[b] || A[a]<C[c]){
                a++;
            }
            if(B[b]<A[a] || B[b]<C[c]){
                b++;
            }
            if(C[c]<A[a] || C[c]<B[b]){
                c++;
            }
        }
        System.out.println(list);
    }
}

The time complexity of this program is O(n) and the space complexity is O(n) also.

Q18. Find the factorial of a large number.

A18. For this, we use ArrayList as we don't know the size of the factorial number.

public class Factorial {
    static ArrayList<Integer> fact(int n){
        ArrayList<Integer> list = new ArrayList<>();
        list.add(1);
        for(int i=1; i<=n; i++){
            int k =0, carry=0;
            while(k<list.size()){
                int temp= list.get(k) * i +carry;
                list.set(k,temp%10);
                carry = temp/10;
                k++;
            }
            while(carry!=0){
                list.add(carry%10);
                carry/=10;
            }
        }
        Collections.reverse(list);
        return list;
    }

    public static void main(String[] args) {
        System.out.println(fact(5));
    }
}

The time and space complexity for this algorithm is O(n).

Q19. Find the maximum product subarray.

A19. For this question, we use Kadanes Algorithm we studied in previous blogs. We keep on iterating using two-pointers max and min. Multiplying each array element with both max and min values if it's positive, if negative we swap those values and multiply to keep intact the max and min properties for the designated variables. At the end, we check which is bigger out of the two and return that result.

public class MaxProductSubArr{
    static long maxProduct(int[] a, int n){
        if(n==0){
            return 0; }
        long max=1,min=1,res=1;
        for(int i=0; i<n; i++){
            if(a[i]>0){
                max = max*a[i];
                min = Math.min(1,a[i]*min);
            }
            else if(a[i]<0){
                // Swap
                long temp = max;
                max = min;
                min = temp;

                max = Math.max(1, max*a[i]);
                min = min * a[i]; 
            }
            else{ // a[i] == 0
                max = 1; min = 1;
            }
            res = Math.max(max,min);
        }
    return res;
    }
}

The time complexity of this algorithm is O(n) and the space complexity is O(1).

Q20. Given an array, find the length of the longest consecutive sequence.

A20. There are two approaches to this, direct method is to sort the array first and then check pairs of elements if they are consecutive and increment the length. The other efficient method is to use HashSet.

public class LongestConsecutiveSequence {
    static int longSeq(int[] a){
        HashSet<Integer> set = new HashSet<>();
        int ans=0;
        for(int i=0; i<a.length;i++){
            set.add(a[i]);
        }
        for(int i=0; i<a.length; i++){
            if(!set.contains(a[i]-1)){
                int j= a[i];
                while(set.contains(j)) {
                    j++;
                }
                if(ans<j-a[i]){
                    ans = j-a[i];
                }
            }
        }
        return ans;
    }
}

The time complexity and space complexity is O(n).