Skip to main content

Time complexity qustion

1 reply [Last post]
Joined: 2012-09-18

what method f do: method "f" fill emptyArr with numbers of arr1 that the method "f" don't find in arr2 and return the biggest number in arr1 that the method "f" canot
find in arr2.
given information - arr1.length == arr2.length, arr1 and arr2 full with whole positive numbers.

qustion is : how to improve the time complexity of this method ???

public int f(int[]arr1, int[]arr2, int[] emptyArr)
int length = a.length;
int k = 0, g = 0, index = 0;

for(int i=0; i < length; i++)
for(int j=0; j < length; j++)
if(arr2[j] == arr1[i])

if(j == length)

= arr1[i];
if(g == 0 || emptyArr
> k) // find the max of arr1 that you don't have in arr2
k = emptyArr
; // max of arr1 that you don't have in arr2
g = 1;



return k; // max of arr1 that you don't have in arr2 .


Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2012-09-28

You need to repost with code which is valid. You also start out saying (int length = a.length) while a is not defined. You are also assigning (emptyArr= arr1[i]) which tries to put an int into an array of int. You will note that you have code which compares emptyArr to k, so comparing an array of ints to an int.