Polynomial Class: Add and Multiply
Problem
Write a Polynomial class. The coefficients are given from the constant term upwards: 1 2 means 1 + 2x. Two polynomials are given on two lines. Print two lines: sum: followed by the coefficients of p + q, and product: followed by the coefficients of p * q. Remove zero coefficients at the top (the highest powers), but keep at least one number, so the zero polynomial prints 0.
Constraints
- 1 <= coefficients per line <= 100
- |coefficient| <= 1000
Examples
Input
1 2 3 4
Output
sum: 4 6 product: 3 10 8
Solutions
Keeping the rules in the class (not in main) lets you reuse add and multiply anywhere. Python can even use the operators + and * through __add__ and __mul__. Multiplication is O(n * m); fast methods like FFT are only needed for very long polynomials.
/*
* Problem : Process lines.
* Input : Two lines of coefficients.
* Output : Two result lines.
* Approach: Operators or methods on the class.
* Time : O(n * m)
* Space : O(n + m)
* Java : 8 or newer.
*/
import java.util.Scanner;
public class Main {
// Step 1: A Polynomial keeps its coefficients in an array. Index i is the coefficient of x^i.
static class Polynomial {
private final long[] coefficients;
Polynomial(long[] coefficients) {
// Step 2: Cut zero coefficients at the top, but keep one number.
int size = coefficients.length;
while (size > 1 && coefficients[size - 1] == 0) {
size--;
}
this.coefficients = java.util.Arrays.copyOf(coefficients, size);
}
// Step 3: Add the coefficients with the same power.
Polynomial add(Polynomial other) {
long[] result = new long[Math.max(coefficients.length, other.coefficients.length)];
for (int i = 0; i < result.length; i++) {
long left = i < coefficients.length ? coefficients[i] : 0;
long right = i < other.coefficients.length ? other.coefficients[i] : 0;
result[i] = left + right;
}
return new Polynomial(result);
}
// Step 4: Multiply every term of one polynomial with every term of the other. Powers add up.
Polynomial multiply(Polynomial other) {
long[] result = new long[coefficients.length + other.coefficients.length - 1];
for (int i = 0; i < coefficients.length; i++) {
for (int j = 0; j < other.coefficients.length; j++) {
result[i + j] += coefficients[i] * other.coefficients[j];
}
}
return new Polynomial(result);
}
@Override
public String toString() {
StringBuilder text = new StringBuilder();
for (int i = 0; i < coefficients.length; i++) {
text.append(i == 0 ? "" : " ").append(coefficients[i]);
}
return text.toString();
}
static Polynomial parse(String line) {
String[] part = line.trim().split("\\s+");
long[] values = new long[part.length];
for (int i = 0; i < part.length; i++) {
values[i] = Long.parseLong(part[i]);
}
return new Polynomial(values);
}
}
// This method solves the task. It receives the input and returns the answer.
static String[] solve(String[] lines) {
Polynomial p = Polynomial.parse(lines[0]);
Polynomial q = Polynomial.parse(lines[1]);
return new String[] {"sum: " + p.add(q), "product: " + p.multiply(q)};
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
java.util.List<String> all = new java.util.ArrayList<>();
while (scanner.hasNextLine()) {
String line = scanner.nextLine().trim();
if (!line.isEmpty()) {
all.add(line);
}
}
String[] lines = all.toArray(new String[0]);
System.out.println(String.join("\n", solve(lines)));
}
}
Hints (1)
- Why do the powers add when two terms are multiplied?