java - 大输入数组中的奇数异或对

标签 java arrays bit-manipulation xor

You are given an array A1,A2...AN. You have to tell how many pairs (i, j) exist such that 1 ≤ i < j ≤ N and Ai XOR Aj is odd.

Input and Output First line T, the number of testcases. Each testcase: first line N, followed by N integers in next line. For each testcase, print the required answer in one line.

Constraints 1 ≤ T ≤ 10 1 ≤ N ≤ 10^5 0 ≤ Ai ≤ 10^9.

我的代码:

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int totalTestCaseT = Integer.parseInt(reader.readLine());
        StringBuilder outputOddCount = new StringBuilder();

        for (int i = 0; i < totalTestCaseT; i++) {
            int lengthOinputT = Integer.parseInt(reader.readLine());
            String input = reader.readLine().trim();
            long oddXorCount = getOddXorCount(input, lengthOinputT);
            outputOddCount.append(oddXorCount);
            outputOddCount.append("\n");
        }

        System.out.println(outputOddCount);
    }

    private static long getOddXorCount(String input, int lengthOinputT) {

        String[] inputArray = input.split(" ");
        int oddCount = 0, evenCount = 0;
        for (int i = 0; i < lengthOinputT; i++) {
        String lastDigit = String.valueOf((inputArray[i]
                    .charAt(inputArray[i].length() - 1)));
            int unitDigit = Integer.parseInt(lastDigit);
            if ((unitDigit & 1) == 1) {
                oddCount++;
            } else
                evenCount++;
        }
        return oddCount * evenCount;
    }

它适用于 N 的某个值,但不适用于大 N ~100000。

示例输入:Input 1 Input 2 .

最初我是这样写的,没有任何功能,主类中的所有东西都是这样的,并且它通过了所有测试

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String line = br.readLine();
    int tCases = Integer.parseInt(line);

    for (int i = 0; i < tCases; i++) {
        long oCount = 0, eCount = 0;
        int N = Integer.parseInt(br.readLine());
        String[] A = br.readLine().toString().split(" ");
        for (int j = 0; j < N; j++) {
            int unitDigit = Integer
                    .parseInt((A[j].charAt(A[j].length() - 1)) + "");
            if (unitDigit % 2 == 0)
                eCount++;
            else
                oCount++;
        }
        System.out.println(eCount * oCount);
    }

这是我的提交 1. Submission of code 1 2. Submission of code 2

最佳答案

在适用于所有输入的版本中,您使用 long 来保存计数器:

long oCount = 0, eCount = 0;

在对某些输入不起作用的版本中,您使用 int 来保存计数器:

int oddCount = 0, evenCount = 0;

也许您遇到了 int 溢出。

例如,如果偶数的个数是所有数的一半,则oddCount 和evenCount 都会是50,000。 50,000*50,000 是 2,500,000,000,大于 int 的最大值。因此 oddCount * evenCount 会溢出。

关于java - 大输入数组中的奇数异或对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30680615/

相关文章:

java - 打印输入与数组元素匹配的整行

java - 为什么这不仅会返回回文字符串?

c++ - 在 C/C++ 中检查最低有效位 (LSB) 和最高有效位 (MSB) 的值

c++ - 检查 uint8_t[8] 是否包含任何非 0 并使用一次内存负载访问非零插槽

java - 如何在 Emacs 中为 Android 开发?

Java boolean 值未从 if 语句中传递出去

c - 从指针数组中删除指针

matlab - eps() 在 MATLAB 中是如何计算的?

java - 即使将新数组长度设置为旧数组长度,也会出现 ArrayIndexOutOfBoundsException

arrays - 根据参数从函数内部的数组返回值