Skip to content

Given three integers a, b, and n,output the following series: a+20b,a+20b+21b,……,a+20b+21b+…+2n−1ba+20b,a+20b+21b,……,a+20b+21b+…+2n−1b

Constraints:

  • 0≤t≤500
  • 0≤a,b≤50
  • 1≤n≤15

Sample Input:

2
0 2 10
5 3 5

Sample Output

2 6 14 30 62 126 254 510 1022 2046
8 14 26 50 98

This works in my IDE but when I try it in an online editor in hackerrank.com it throws an exception:

Exception in thread “main” java.util.NoSuchElementException: No line foundat java.util.Scanner.nextLine(Scanner.java:1585)at Solution.main(Solution.java:24)

Please explain why this happens.Thanks!

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

public class Solution {

    public static void main(String[] args) {
        int[] a = new int[10];
        int[] b = new int[10];
        int[] n = new int[10];
        int t;
        int sum;
        StringBuilder sb =new StringBuilder();

        Scanner iput = new Scanner(System.in);

        t = Integer.parseInt(iput.nextLine());

        if (t <= 500) {

            for (int i = 0; i < t; i++) {
                a[i] = Integer.parseInt(iput.next());
                b[i] = Integer.parseInt(iput.next());
                n[i] = Integer.parseInt(iput.next());
                iput.nextLine();
            }
        } else
            System.out.println("Enter value less than 500");

        if (t <= 500) {

            for (int i = 0; i < t; i++) {

                if (a[i] <= 50 && b[i] <= 50 && n[i] <= 15 && n[i] != 0) {

                    for (int j=0;j<n[i];j++) {

                        sum = a[i];

                        for (int k = j;k >=0; k--) {

                            sum+=Math.pow(2,k)*b[i];
                        }
                        sb=sb.append(Integer.toString(sum)).append(" ");
                    }
                    System.out.println(sb);
                    sb.delete(0,sb.toString().length());
                } else
                    System.out.println("Enter the values within the allowed limits");
            }
        }
    }    
}

Answer

Remove iput.nextLine(); on line 24 so that no extra reading will happen.