# Time complexity question

• 09-17-2012, 11:16 PM
romavolman
Time complexity question
I have method and I need to improve the time complexity.
Method accept two array with same size with hole positive numbers and
Third empty array.How i can improve complexity time in this method ?
public int f(int[] a, int[] b, int[] c)
{
int N = a.length();
int k=0, t=0, p=0;
for(int i = 0; i<N; i++)
{
for(int j = 0; j<N; j++)
{
if(a[i] == b[j])
brake;
}

c[t]=a[i];
if(p==0 || c[t]>k)
{
k = c[t];
p = 1;

}
t++;
}

return k;
}
• 09-18-2012, 06:26 AM
DarrylBurke
Re: Time complexity question
BB Code List - Java Programming Forum

Are yoiu sure that's Java code?
Quote:

Originally Posted by romavolman
brake;

db
• 09-18-2012, 08:29 AM
romavolman
Re: Time complexity question
I have method and I need to improve the time complexity.
Method accept two array with same size with hole positive numbers and
Third empty array.How i can improve complexity time in this method ?
public int f(int[] a, int[] b, int[] c)
{
int N = a.length();
int k=0, t=0, p=0;
for(int i = 0; i<N; i++)
{
for(int j = 0; j<N; j++)
{
if(a[i] == b[j])
break;
}

if(j==N)
{

c[t]=a[i];
if(p==0 || c[t]>k)
{
k = c[t];
p = 1;

}
t++;
}

}

return k;
}
• 09-18-2012, 09:26 AM
kjkrum
Re: Time complexity question
It's very hard to understand when you use meaningless letters for variable names.
• 09-18-2012, 11:38 AM
DarrylBurke
Re: Time complexity question
Also hard to help you when you don't bother to click a link in a response to find why it was offered.

db
• 09-18-2012, 03:21 PM
romavolman
Re: Time complexity question
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])
break;

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

}
index++;

}

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

}
• 09-18-2012, 03:37 PM
DarrylBurke
Re: Time complexity question
If you continue to ignore the link provided in the first response, this thread will be locked and you may be banned for a period.

Using code tags is not optional.

db
• 09-19-2012, 02:56 AM
Norm
Re: Time complexity question
Also posted at time complexity qustion
• 09-19-2012, 06:41 PM
sterf
Re: Time complexity question
Romavolman i agree with other posts that your text is vague.

Perhaps you mean by "time complexity" simply how to make the code run faster, and that implies that you had array sizes in the millions. i guess that your goal is to fill c[] with some local maxima. If that is the case, what you want is to remove the input array cell that you have used, and you did not do that because of the limitations of the datatype array. Correct?

The problem is that you use NxN combinations, that is brute force and will take forever.

Suggestion: first order the a[] and then order the b[]. Then loop once, keeping two pointers p_a and p_b, and then you have only N comparisons. Ordering a list of n elements like a[] and b[] you can use standard method like quicksort, that takes N*log(N) time. So from
NxN you improve your "time complexity" to 3N*log(N).

Good luck!