Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 1st, 2012, 09:17 AM
romavolman romavolman is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 1 romavolman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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]

Reply With Quote
  #2  
Old October 2nd, 2012, 02:55 PM
doa24uk doa24uk is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Posts: 73 doa24uk User rank is Sergeant Major (2000 - 5000 Reputation Level)doa24uk User rank is Sergeant Major (2000 - 5000 Reputation Level)doa24uk User rank is Sergeant Major (2000 - 5000 Reputation Level)doa24uk User rank is Sergeant Major (2000 - 5000 Reputation Level)doa24uk User rank is Sergeant Major (2000 - 5000 Reputation Level)doa24uk User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 17 h 28 m 19 sec
Reputation Power: 31
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

Reply With Quote
  #3  
Old October 3rd, 2012, 04:09 AM
ishnid's Avatar
ishnid ishnid is offline
kill 9, $$;
Dev Shed God 4th Plane (6500 - 6999 posts)
 
Join Date: Sep 2001
Location: Shanghai, An tSín
Posts: 6,894 ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level) 
Time spent in forums: 4 Months 2 Weeks 1 Day 22 h 14 m 53 sec
Reputation Power: 3885
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.

Reply With Quote
  #4  
Old October 6th, 2012, 06:31 AM
doa24uk doa24uk is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Posts: 73 doa24uk User rank is Sergeant Major (2000 - 5000 Reputation Level)doa24uk User rank is Sergeant Major (2000 - 5000 Reputation Level)doa24uk User rank is Sergeant Major (2000 - 5000 Reputation Level)doa24uk User rank is Sergeant Major (2000 - 5000 Reputation Level)doa24uk User rank is Sergeant Major (2000 - 5000 Reputation Level)doa24uk User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 17 h 28 m 19 sec
Reputation Power: 31
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()

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Homework - How to do write this method with more efficiency ?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap