You are not logged in. Please login at www.codechef.com to post your questions!

×

what is wrong in my code?

import java.util.Scanner;
class Rupsa
{
    public static void main(String args[])
    {
        Scanner input  = new Scanner(System.in);
        //System.out.println("Enter umber of test cases : ");
        int n = input.nextInt();
        int a[][] = new int[n][];
        int x,y,sum;
        //System.out.println("Enter number of numbers in each test case : ");
        for(int i=0;i<n;i++)
        {
            a[i] = new int[input.nextInt()+1];
            for(int j=0;j<a[i].length;j++)
            {
                a[i][j] = input.nextInt();
            }
        }
        for(int i=0;i<n;i++)
        {
            sum = 0;


                for(int j=0;j<a[i].length;j++)
                {
                    for(int k = j+1;k<a[i].length;k++)
                    {
                        if(j<2)
                        {
                            sum = sum+(a[i][j]*a[i][k]*(int)Math.pow(2,(a[i].length-k)));
                        }
                        else
                        {
                            sum = sum+(a[i][j]*a[i][k]*(int)Math.pow(2,(a[i].length-k+1)));
                        }
                    }
                }
                 if(a[i].length==1)
                System.out.println(a[i][0]);
                else
                {       
                    System.out.println(sum);
                }

        }
    }
}

asked 16 Jul '17, 20:41

kondasaiganesh's gravatar image

0★kondasaiganesh
1
accept rate: 0%

edited 16 Jul '17, 21:49

vijju123's gravatar image

5★vijju123 ♦♦
15.5k12066


Overflow. Array's length can be upto 10^5, you cant just Math.pow(2,10^5), it will cause heavy overflow.

If you are a beginner, skip this Q for now, its a easy-medium question as labelled by tag (IDK just WHO put it in beginner's section...). It needs you to know some tricks, which you will learn during course of practicing (Easy section)problems and participating in contests.

link

answered 16 Jul '17, 21:48

vijju123's gravatar image

5★vijju123 ♦♦
15.5k12066
accept rate: 18%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×334
×255
×30
×21
×19
×15
×12

question asked: 16 Jul '17, 20:41

question was seen: 406 times

last updated: 16 Jul '17, 23:02