HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMajor

'Team Split' problem

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
problemteamsplit

Problem

Below is my solution for this 'Team Split' problem.

In summary, the problem is to find relative strength of 2 teams by finding the difference between their total strength (individual strengths can be found using a quadratic expression of form ax2+bx+c).

The solution in Java manages to solve the problem, but I am regularly getting the Time Limit Exceeded issue.

Can anyone offer advice on how to reduce the complexity of this code?

import java.util.*; 
import java.io.*;

public class Main {
    public static void main (String args[]) throws IOException{
        try{
            BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
            int not=Integer.parseInt(sc.readLine());
            int a,b,c,d;
            for(int i=0;i=0;p=p-2,q=q-2){
                    if(q>=-1){
                        so1=so1+S[p];
                        if(q>=0){
                            so2=so2+S[q];
                        }
                    }
                }
                // System.out.println(so1);
                // System.out.println(so2);
                t=so1-so2;
                if(t<0){
                    t=(-1)*t;
                }
                System.out.println(t);
                //sc.close();
            }
        }catch(NumberFormatException afe){
            System.out.println(afe);
        }
    }
}

Solution

Rewafadhabihlgdity

(If you can't tell, that is supposed to say 'readability'). Leaves some things to be desired.

-
Variable names: All your variable names are three characters or less. This makes it very hard to read your code. Renaming r to playerCount is a start, and S can be called strengths. so1 and so2 should also be renamed, along with t, p and q. Make variables self-documenting by giving them a reasonable name.

-
Spacing: Dontyoufinditabithardtoreadtext/codethatdoesntcontainanyspacesandappropriatepunctuation? (I'm exagerating just to prove a point). Even though it is a bit easier to read lack of code spacing than lack of word-spacing in written text, I find a line like this hard to read:

S[k]=((a*S[k-1]*S[k-1])+(b*S[k-1])+c)%1000000;


For this particular line, consider rewriting it to this:

int previous = strengths[index - 1];
strengths[index] = (a * previous * previous + b * previous + c) % 1000000;


By using better variable names, adding space, extracting a variable, and removing unnecessary parenthesis that line becomes a lot more readable.

The lack of good variable names and spacing especially made this part of the code really hard for me to understand what you are doing and why:

for(int p=r-1,q=r-2;p>=0;p=p-2,q=q-2){
     if(q>=-1){
         so1=so1+S[p];
         if(q>=0){
             so2=so2+S[q];
         }
     }
}


Speed

Unable to reproduce / Unclear what the issues are. It seems to me that your code is quite fast. Even the input 10000000 5 6 7 8 calculated with reasonable speed IMO.

Extensibility

For testing purposes, to ensure both correct results and fast enough results, it would be useful to extract a method to do the actual calculations, and then your outermost loop could be:

for (int i = 0; i < count; i++) {
            String s[] = sc.readLine().trim().split(" ");
            int playerCount = Integer.parseInt(s[0]);
            a = Integer.parseInt(s[1]);
            b = Integer.parseInt(s[2]);
            c = Integer.parseInt(s[3]);
            d = Integer.parseInt(s[4]);
            System.out.println(determineTeamDifferences(playerCount, a, b, c, d));
        }

Code Snippets

S[k]=((a*S[k-1]*S[k-1])+(b*S[k-1])+c)%1000000;
int previous = strengths[index - 1];
strengths[index] = (a * previous * previous + b * previous + c) % 1000000;
for(int p=r-1,q=r-2;p>=0;p=p-2,q=q-2){
     if(q>=-1){
         so1=so1+S[p];
         if(q>=0){
             so2=so2+S[q];
         }
     }
}
for (int i = 0; i < count; i++) {
            String s[] = sc.readLine().trim().split(" ");
            int playerCount = Integer.parseInt(s[0]);
            a = Integer.parseInt(s[1]);
            b = Integer.parseInt(s[2]);
            c = Integer.parseInt(s[3]);
            d = Integer.parseInt(s[4]);
            System.out.println(determineTeamDifferences(playerCount, a, b, c, d));
        }

Context

StackExchange Code Review Q#45146, answer score: 26

Revisions (0)

No revisions yet.