WA in Polynomial multiplication (POLMUL)

please help me with this question

i am getting correct output with the sample input
but after submitting i am getting WA for all test cases .
Please HElp !!!

You mind posting your solution? Don’t forget to format your code if you’re posting your code. You may post an ideone link. :slightly_smiling_face:

/* package codechef; // don't place package name! */

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

/* Name of the class has to be “Main” only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
// Scanner sc=new Scanner (System.in);
// int t =sc.nextInt();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int t =Integer.parseInt(br.readLine().trim());
while(t–>0)
{
String s[]=br.readLine().trim().split(“\s+”);
int n=Integer.parseInt(s[0]);
int m=Integer.parseInt(s[1]);
int p[]=new int[n];
int q[]=new int[m];
int a[]=new int[n+m-1];
s=br.readLine().trim().split(“\s+”);
for(int i=0;i<n;i++)
{
p[i]=Integer.parseInt(s[i]);
}
s=br.readLine().trim().split(“\s+”);
for(int i=0;i<m;i++)
{
q[i]=Integer.parseInt(s[i]);
}

	    for(int i=0;i<n;i++)
	    {
	        for(int j=0;j<m;j++)
	        {
	            a[i+j] +=p[i]*q[j];
	        }
	    }
	    for(int i=0;i<a.length;i++)
	      System.out.print(a[i]+" ");
	      
	    System.out.println(); 
	}
}

}

oops you forgot to format the whole code!

1 Like

If you are having trouble formatting the code, please save your code to a pastebin and share the link here!

Is clicking this so hard? ¯\_(ツ)_/¯
I assure you, its not as harmful as it looks like. :slight_smile:

2 Likes
import java.util.;
import java.lang.;
import java.io.*;

/* Name of the class has to be “Main” only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
// Scanner sc=new Scanner (System.in);
// int t =sc.nextInt();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int t =Integer.parseInt(br.readLine().trim());
while(t–>0)
{
String s[]=br.readLine().trim().split("\s+");
int n=Integer.parseInt(s[0]);
int m=Integer.parseInt(s[1]);
int p[]=new int[n];
int q[]=new int[m];
int a[]=new int[n+m-1];
s=br.readLine().trim().split("\s+");
for(int i=0;i<n;i++)
{
p[i]=Integer.parseInt(s[i]);
}
s=br.readLine().trim().split("\s+");
for(int i=0;i<m;i++)
{
q[i]=Integer.parseInt(s[i]);
}

	    for(int i=0;i<n;i++)
	    {
	        for(int j=0;j<m;j++)
	        {
	            a[i+j] +=p[i]*q[j];
	        }
	    }
	    for(int i=0;i<a.length;i++)
	      System.out.print(a[i]+" ");
	      
	    System.out.println(); 
	}
} 
2 Likes

Hint

@line 22 - you need more than 4 bytes of storage for numbers that can be as large as 1410065408000 :wink:

1 Like

thanks :grin:

1 Like

if you are programming in c++ , then ,
use long long int instead of int .
use fast way of extracting input and showing output .

use this :-

typedef long long int ll;
ios_base::sync_with_stdio(false);
cout.tie(NULL);
cin.tie(NULL);

My code also works fine in local IDE but getting WA when submitting.

import java.util.*;

class Expression{
    public static void main(String[] ar) {
        Scanner sc =  new Scanner(System.in);
        int t = sc.nextInt();
        if(t > 10)
            return;
        while(t --> 0){
            int np = sc.nextInt();
            int nq = sc.nextInt();
            long[] p = new long[np];
            long[] q = new long[nq];
            for(int i =0; i < np; i++){
              p[i] = sc.nextInt();
            }
            for(int i =0; i < nq; i++){
              q[i] = sc.nextInt();
            }
            long[] a = new long[np+nq-1];
            for(int i = 0; i < np; i++){
                for(int j = 0; j < nq; j++){
                    a[i+j] = a[i+j] + (p[i] * q[j]);
                }
            }
            String ans = "";
            for(int i =0; i < a.length; i++)
              ans += a[i]+" ";
            System.out.println(ans.trim());
        }
    }
}