The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Java Help
|
Homework - How to do write this method with more efficiency ?
Discuss How to do write this method with more efficiency ? in the Java Help forum on Dev Shed. How to do write this method with more efficiency ? Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

October 1st, 2012, 09:17 AM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 1
Time spent in forums: 26 m 17 sec
Reputation Power: 0
|
|
|
Homework - How to do write this method with more efficiency ?
The program below accept two array with hole pozitive nums, with same size N, and fill empty array "c" with nums of "a" that do not exist in "b" and return the biggest num of "a" that do not exist in "b".
qustion: how to do write this method with more efficiency ?
[PHP]
public int f(int[]a, int[]b, int[] c)
{
int N = a.length;
int j, max = 0, g = 0, t = 0;
for(int i=0; i < N; i++)
{
for(j=0; j<N; j++)
if(b[j] == a[i]) // if find the num in "b" go to the next num in "a"
break;
if(j == N)
{
c[t] =a[i] // fill empty array "c" with nums of "a" that do not exist in "b"
if(g ==0 || c[t] > max)
{
max = c[t]; // remmber the max num in "max"
g = 1;
}
t++;
}
}
return max; // max of "a" that you do not have in "b"
[\PHP]
|

October 2nd, 2012, 02:55 PM
|
|
|
|
If the are guaranteed to be the same size then
Store the size of a or b in an int.
Hold a seperate counter variable that increments one at a time.
Hold a seperate variable for the current max number.
Loop through the specified number of times (which == the size of array a).
If elementA(count) != elementB(count)
{
// add to array list c
// set the new max number to be this number
}
// Now we're at the end of the loop.
// Print out the max number
|

October 3rd, 2012, 04:09 AM
|
 |
kill 9, $$;
|
|
Join Date: Sep 2001
Location: Shanghai, An tSín
|
|
Quote: | Originally Posted by doa24uk If the are guaranteed to be the same size then
Store the size of a or b in an int.
Hold a seperate counter variable that increments one at a time.
Hold a seperate variable for the current max number.
Loop through the specified number of times (which == the size of array a).
If elementA(count) != elementB(count)
{
// add to array list c
// set the new max number to be this number
}
// Now we're at the end of the loop.
// Print out the max number |
That only checks if 'a' and 'b' contain the same elements in the same position in the array, which isn't the original problem.
Hint to OP: this is much more efficient of 'a' and 'b' are sorted.
|

October 6th, 2012, 06:31 AM
|
|
|
Quote: | Originally Posted by ishnid That only checks if 'a' and 'b' contain the same elements in the same position in the array, which isn't the original problem.
Hint to OP: this is much more efficient of 'a' and 'b' are sorted. |
Doh! Of course, you are correct.
Collections.sort() 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|