I have a time complexity of ( log ()+()), how should I modify the code to have a complexity of ( log ()+())

46 Views Asked by At

I have this code, and the time complexity is

( log ()+())  but I now I need a code that does the same thing but with a time complexity of

( log ()+()) , I do not understand what should I consider for getting that time complexity.

import java.util.Arrays;

public class Main {

    // Returns true if set1[] and set2[] are disjoint, else false
    static boolean areDisjoint(int[] set1, int[] set2, int m, int n) {
        // Sort the set1 array
        Arrays.sort(set1);

        // Take every element of set2[] and search it in the sorted set1 array
        for (int i = 0; i < n; i++) {
            // Binary search to find the lower bound of set2[i] in set1
            int lb = Arrays.binarySearch(set1, set2[i]);

            // If the element is present in set1, return false
            if (lb >= 0)
                return false;
        }

        // If no element of set2 is present in set1, return true
        return true;
    }

    // Driver program to test the above function
    public static void main(String[] args) {
        int[] set1 = {12, 34, 11, 9, 3};
        int[] set2 = {7, 2, 1, 5};
        int m = set1.length;
        int n = set2.length;
        System.out.println(areDisjoint(set1, set2, m, n) ? "Yes" : "No");
    }
}

I was trying to use just quicksort but still I do not get how can I get this

( log ()+())  if the original code has a time complexity of

( log ()+())

0

There are 0 best solutions below