Skip to content

Instantly share code, notes, and snippets.

@tmosest
Created January 24, 2017 22:17
Show Gist options
  • Save tmosest/05101e61012a0fe38d5f6f8843738401 to your computer and use it in GitHub Desktop.
Save tmosest/05101e61012a0fe38d5f6f8843738401 to your computer and use it in GitHub Desktop.
Multiply Complexities
// Computers m binary searches
public static boolean containsElements(int[] seaches, int[] array)
{
boolean hasElements = true;
int m = searches.length;
int n = array.length;
// O(m) loop
for(int i = 0; i < m; i++) {
// Each Binary Search has O(lg n) complexity
if(binarySearch(array, searches[i] == false)
hasElements = false;
}
// Now for each O(m) we are performing a O(lg n) operation
// Therefore we O(m * lg n)
return hasElements;
}
// Brute force method to check if an array has unique elements
public static boolean containsUniqueElements(int[] array)
{
int n = array.length;
boolean hasUniqueElements = true;
// O(n) Outer for loop.
for(int i = 0; i < n; i++) {
// O(n) Inner for loop.
for(int j = 0; j < n; j++) {
if(i != j && a[i] == a[j])
hasUniqueElements = fasle;
}
}
/* For each of the O(n) outer loop operations we are performing
another O(n) operations which makes this O(n * n) = O (n ^ 2)
*/
return hasUniqueElements;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment