Thread: Time complexity question

1. Member
Join Date
Sep 2012
Posts
4
Rep Power
0

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;
}

2. Re: Time complexity question

BB Code List - Java Programming Forum

Are yoiu sure that's Java code?
Originally Posted by romavolman
brake;
db

3. Member
Join Date
Sep 2012
Posts
4
Rep Power
0

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;
}

4. Re: Time complexity question

It's very hard to understand when you use meaningless letters for variable names.

5. 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

6. Member
Join Date
Sep 2012
Posts
4
Rep Power
0

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 .
}

}
Last edited by romavolman; 09-18-2012 at 04:28 PM.

7. 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

8. Re: Time complexity question

Also posted at time complexity qustion

9. Member
Join Date
Apr 2012
Posts
6
Rep Power
0

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!

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•