Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3982252
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:30:22+00:00 2026-05-20T05:30:22+00:00

Im almost done with a homework assignment that multiplies polynomials and has to have

  • 0

Im almost done with a homework assignment that multiplies polynomials and has to have its like terms simplified and in order from highest degree to lowest. The 2 statements are also already sorted. My program works perfectly, but it takes too long to get the result (like 2 minutes on my machine), and the web site i use to submit it says time limit exceeded. For the actual multiplication (not shown here), it takes very little time but the combining of like terms takes a while. It takes in 1 linked list that has 2 statements combined, i.e:

2 2 2 1 1 1 //means 2x^2 + 2x +  x
*
3 2 5 1 1 0 //means 3x^2 + 5x + 1

and i turn it into 2 2 2 1 1 1 3 2 5 1 1 0 for processing.

Anyone know how i could speed this up a bit? Thanks.

    public MyLinkedList add(MyLinkedList combinedList ) {
    MyLinkedList tempCombinedList = new MyLinkedList();
    MyLinkedList resultList = new MyLinkedList();
    //check highest power now that its sorted.
    tempCombinedList=null;
    tempCombinedList = new MyLinkedList();
    int highestPower=0;

    //we need to find highest power
    for(int l=2;l<=combinedList.size();l=l+2) {
        if((Integer)combinedList.get(l)>highestPower) {
            highestPower=(Integer)combinedList.get(l);
            System.out.println("highest power is "+highestPower);
        }
    }

    int tempAddition=0;     
    while(highestPower!=-1) {
        for(int z=2;z<=combinedList.size();z=z+2) {
            if((Integer)combinedList.get(z)==highestPower) {
                tempAddition=tempAddition+(Integer)combinedList.get(z-1);
            }
        }
        if((tempAddition!=0)) { //we arent allowed to have a 0 coefficient in there....
            resultList.add(tempAddition);
            resultList.add(highestPower);
        }
        else if(((tempAddition==0)&&(highestPower==0))) { //unless the exponent is 0 too
            resultList.add(tempAddition);
            resultList.add(highestPower);
        }
        tempAddition=0; //clear the variable for the next roud
        highestPower--; //go down in power and check again.
        }
return resultList;

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T05:30:22+00:00Added an answer on May 20, 2026 at 5:30 am

    Your code look like you are using a list with alternating factor and exponent. This is likely not the reason for your performance problem, but makes the code harder to read – additionally to your casting.

    Use a class like

    class Monomial implements Comparable<Monom> {
       private int exponent;
       private int factor;
    
       // TODO: get methods, constructor
    
       public int compareTo(Monomial other) {
           return this.exponent - other.exponent;
       }
    
       public Monomial times(Monomial other) {
          // here your multiplication code
       }
    
    
    }
    

    and then you can have a List<Monomial> instead of your list of integers. Firstly, this makes your code more readable, and secondly, you can now (after the multiplication) simply sort your list, so you later don’t have to go through the whole list again and again.

    Then, as you are always using the .get(i) access to your list, don’t use a linked list, use an ArrayList (or a similar structure). (For a linked list, you for each access have to iterate through the list to get the element you want, for a array-list not.) Alternatively, use an Iterator (or the enhanced for loop) instead of index-access.


    Actually, if you sort (and simplify) the factors before multiplying, you can multiply them already in the right sequence so you don’t really have to simplify afterwards. As an example, it is

    (2x^2 + 3x + 0) * (3x^2 + 5x + 1)
    = (2*2) * x^4 +
      (2*5  + 3*3) * x^3 +
      (2*1 + 3*5 + 0*3) * x^2 +
      (3*1 + 0*5) * x^1 +
      (0*1) * x^0
    = 4 * x^4 + 19 * x^3 + 17 * x^2 + 3 * x 
    

    (You get the scheme: in each line the factors from the first polynomial are sorted downwards, the ones from the second polynomial upwards).

    (And you can leave away the 0 terms already at the beginning, if you want).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am almost done with my project, but have one final looming problem that
Almost every Python web framework has a simple server that runs a wsgi application
I know you don't like helping others in their homework but I have to
I'm almost done coding my project and I have come across 1 to 2
I'm almost done with this assignment, and it's killing me. This is my THIRD
The title say's almost all. What I have is short lived server processes that
I'm writing this in the forlorn hope that someone has already done something similar.
My homework assignment is to create a shell (done) with a history function (done)
I'm almost done with this online library: http://gramma.ro I have grade C in YSlow
In almost any Windows application, I notice that holding the mouse button down in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.