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);                    
        }
    }
}
Adarsh Bahadur
  • 83
  • 3
  • 12
  • 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 – Adarsh Bahadur Aug 21 '21 at 07: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) – Adarsh Bahadur Aug 21 '21 at 07:48
  • There is a space after the `1`. Can you see it in the exception message? That's what is causing the exception. – Stephen C Aug 21 '21 at 08:10
  • Yes i saw that later. I was still wondering if it was coming from t,n or from arr[i]. – Adarsh Bahadur Aug 21 '21 at 11:51
  • Well the line numbers in the stacktrace will tell you that. – Stephen C Aug 21 '21 at 11:59

1 Answers1

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

vszholobov
  • 2,133
  • 1
  • 8
  • 23