1

You are given an array A of N numbers, output the count of pairs in the array whose sum is divisible by 4.

The first line of the input contains a single integer T denoting the number of test cases.

The description of T test cases follows.

The first line of each test case contains a single integer N.

The second line contains N space-separated integers

A[0] A[1] ... A[N−1]

representing the array numbers.

/* 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. */
public class Main {
    public static void main (String[] args) throws java.lang.Exception {
        //Scanner scan = new Scanner(System.in);
        BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
        int t = Integer.parseInt(br.readLine());
        //int t= scan.nextInt();
        while (t-- > 0) {
            int n = Integer.parseInt(br.readLine());
            long ans=0;
            int[] arr= new int[n];
            String line = br.readLine();
            String[] strs = line.trim().split("\\s+");
            for (int i = 0; i < n; i++) {
                arr[i]=Integer.parseInt(strs[i]);
            }
            int[] count = new int[4];
            for (int i = 0; i < n; i++) {
                count[arr[i] % 4]++;
            }
            for (int i = 1; i <= 1; i++) {
                ans += count[i] * count[4-i];
            }
            ans += (count[2] * (count[2]-1)) / 2;
            //System.out.println(ans);
            ans += (count[0] * (count[0]-1)) / 2;
            System.out.println(ans);                    
        }
    }
}
5
  • Yes it's an assignment. Not graded. I am able to do it with scanner. I am getting Exception in thread "main" java.lang.NumberFormatException: For input string: "1 " while using Buffered Reader Commented Aug 21, 2021 at 7:47
  • Exception in thread "main" java.lang.NumberFormatException: For input string: "1 " at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68) at java.base/java.lang.Integer.parseInt(Integer.java:658) at java.base/java.lang.Integer.parseInt(Integer.java:776) at Main.main(Main.java:13) Commented Aug 21, 2021 at 7:48
  • There is a space after the 1. Can you see it in the exception message? That's what is causing the exception. Commented Aug 21, 2021 at 8:10
  • Yes i saw that later. I was still wondering if it was coming from t,n or from arr[i]. Commented Aug 21, 2021 at 11:51
  • Well the line numbers in the stacktrace will tell you that. Commented Aug 21, 2021 at 11:59

1 Answer 1

2

You did the right thing to trim when you read the array, but you didn't do it when you read t and n values.

Add trim() call in these lines:

int t = Integer.parseInt(br.readLine().trim());
int n = Integer.parseInt(br.readLine().trim());

Converting a String with spaces to an Integer in java

Also, if it is guaranteed that the numbers are separated by exactly one space, it is better to replace the string splitting with

String[] strs = line.trim().split(" ");

because splitting by one character is much faster than splitting by regex Java split String performances

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.