![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
|
max multiple of 3
Im working on a program to return the maximum block that is a multiple of 3, in
an array of intergers. Im trying to split the problem in half and add the blocks that are of modulo together and mudulo 1 and 2 from each side. Some times the program works but ill try it on an array like 1 3 8 7 0 and instead of 15 im getting 12. If someone could give me a clue on what im doing wrong i would appreciate it very much.. Thank you public int MaxBlockMult3() {
return MaxBlockMult3(0, num.length-1);
}
private int MaxBlockMult3(int left, int right){
int rtn, maxRight, maxLeft, max;
if(left > right)
rtn = -1;
else if (left == right) //one element array
rtn = num[left];
else {
int mid = (left + right)/2;
int sum = 0;
int maxSumRight[] ;
maxSumRight = new int[3]; //array to store sums
maxSumRight[0] = 0; //sums that have modulo 0
maxSumRight[1] = 0; //sums that have modulo 1
maxSumRight[2] = 0; //sums that have modulo 2
// add the sums of each modulo on the right side
for (int i = mid + 1; i <= right; i++){
sum += num[i];
if (sum % 3 == 0 && sum > maxSumRight[0])
maxSumRight[0] = sum;
else if (sum % 3 == 1 || sum % 3 + 3 == 1 && sum > maxSumRight[1])
maxSumRight[1] = sum;
else if (sum % 3 == 2 || sum % 3 +3 == 2 && sum > maxSumRight[2])
maxSumRight[2] = sum;
}
sum = 0; //reset the sum value
int maxSumLeft[] ;
maxSumLeft = new int[3]; //array to store sums
maxSumLeft[0] = 0; //sums that have modulo 0
maxSumLeft[1] = 0; //sums that have modulo 1
maxSumLeft[2] = 0; //sums that have modulo 2
//add the sums of each modulo on the left side
for (int i = left; i <= mid; i++){
sum += num[i];
if (sum % 3 == 0 && sum > maxSumLeft[0])
maxSumLeft[0] = sum;
else if (sum % 3 == 1 || sum % 3 + 3 == 1 && sum > maxSumLeft[1])
maxSumLeft[1] = sum;
else if (sum % 3 == 2 || sum % 3 + 3 == 2 && sum > maxSumLeft[2])
maxSumLeft[2] = sum;
}
//recursive calls to right and left
maxRight = MaxBlockMult3(mid+1,right );
maxLeft = MaxBlockMult3(left, mid);
// find the maximum block from right and left return
if (maxRight > maxLeft)
max = maxRight;
else
max = maxLeft;
if (maxSumRight[0]+maxSumLeft[0] > max )
max = maxSumRight[0]+maxSumLeft[0];
else if (maxSumRight[1]+maxSumLeft[2] > max)
max = maxSumRight[1]+maxSumLeft[2];
else if (maxSumRight[2]+maxSumLeft[1] > max)
max = maxSumRight[2]+maxSumLeft[1] ;
// checks to make sure the max is a multiple of 3 before returning
if (max % 3 == 0)
rtn = max;
else // return -1 if there are no multiple blocks of 3
rtn = -1;
}
return rtn;
} |
|
|
|
|
|
#2 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 770
Rep Power: 3
![]() |
Not quite understanding the problem here, so let me try to rephrase it. You're trying to find the largest sum of a sub-sequence such that the sum is a multiple of 13? Or the longest sub-sequence who's sum is a multiple of 3?
__________________
<insert disclaimer here> <insert shameless plug for Visual Studio here> |
|
|
|
|
|
#3 |
|
Newbie
|
The longest/largest sum that is a mutiple of 3. Or the maximum contigous block in the array that is a multiple of 3.
Thank you for the reply Jimbo |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Storing Variables with Multiple Dialogs/Forms | PhilBon | Visual Basic .NET | 6 | Oct 11th, 2006 8:14 AM |
| Multiple conditions in an #ifdef statement | JawaKing00 | C | 8 | Apr 26th, 2006 5:11 PM |
| Calculator Multiple Operations? | jl13 | C# | 5 | Oct 12th, 2005 5:32 PM |
| Multiple server clients using sockets + Threads | captainK | Java | 3 | Mar 4th, 2005 11:10 AM |
| multiple definition of variables in include files | carlgreen | C++ | 3 | Feb 26th, 2005 8:02 PM |