开发者

CodeChef Array Transform Program

开发者 https://www.devze.com 2023-02-08 13:32 出处:网络
Here\'s the Problem Statement : Given n numbers, you can perform the following operation any number of

Here's the Problem Statement :

Given n numbers, you can perform the following operation any number of times : Choose any subset of the numbers, none of which are 0. Decrement the numbers in the subset by 1, and increment the numbers not in the subset by K. Is it possible to perform operations such that all numbers except one of them become 0 ? Input : The first line contains the number of test cases T. 2*T lines follow, 2 for each case. The first line of a test case contains the numbers n and K. The next line contains n numbers, a_1...a_n. Output : Output T lines, one corresponding to each test case. For a test case, output "YES" if there is a sequence of operations as described, and "NO" otherwise.

Sample Input :
3
2 1
10 10
3 2
1 2 2
3 2
1 2 3

Sample Output :
YES
YES
NO

Constraints :
1 <= T <= 1000
2 <= n <= 100
1 <= K <= 10
0 <= a_i <= 1000

& here's my code :

import java.util.*;

public class ArrayTransform {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int no_of_tests = sc.nextInt();

        int size;
        int a[] = new int[100];
        boolean yes;
        int j;
        int k;
        for (int i = 0; i < no_of_tests; i++) {
            size = sc.nextInt();
            k = sc.nextInt();
            for (j = 0; j < size; j++) {
                a[j] = sc.nextInt();
            }
            yes = is_possible(a, size, k + 1);
            if (yes)
                System.out.println("YES\n");
            else
                System.out.println("NO\n");
        }
    }

    static boolean is_possible(int a[], int size, int k_1) {
        int count = 0;
        int m[] = { -1, -1 };
        int mod;
        for (int i = 0; i < size; i++) {
            mod = a[i] % k_1;
开发者_运维技巧            if (m[0] != mod && m[1] != mod) {
                if (m[0] == -1)
                    m[0] = mod;
                else if (m[1] == -1)
                    m[1] = mod;
                else
                    return false;
            }
        }
        return true;
    }
}


if (m[0] != mod && m[1] != mod)

Here instead of && there should be ||. Only one of the m's need to match the mod.

0

精彩评论

暂无评论...
验证码 换一张
取 消