private static int Sum (int[] a, int from, int to){
int total=0;
for (int i=from; i <= to; i++)
res += a[i];
return total;
}
public static int Method3 (int []a){
int temp=0;
for (int i=0; i <= a.length; i++)
{
for (int j=0; j <= a.length; j++)
{
int c = Sum(a,i,j);
if (c%3 == 0)
{
if (j-i+1 > temp)
temp = j-i+1;
}
}
}
return temp;
}
The purpose of Method3
method is to find the longest combination of a given array numbers', so that the sum of the numbers of the combination can be divided by 3 without remainder.
- How do I make it more efficient in terms of both time and memory complexity?
- How do I even approach to something like this?
- ow can I know that the complexity I've reached is the best possible?