Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int getArraySum(int[] a) {
- int sum = 0;
- for (int i = 0; i < a.length; i++) {
- sum = sum + a[i];
- }
- return sum;
- }
- int[] invertArray(int[] a) {
- int[] app = new int[a.length];
- for (int i = 0; i < a.length; i++) {
- if (a[i] == 0) {
- app[i] = 1;
- } else {
- app[i] = 0;
- }
- }
- return app;
- }
- boolean check(int[] a, int[] b) {
- int[] c = new int[b.length];
- for (int i = 0; i < a.length; i++) {
- c[i] = a[i] * b[i];
- }
- int sum = getArraySum(c);
- if (sum % 10 == 0 && sum != 0) {
- // guardo se la somma degli elementi non in a è pari
- int[] c1 = invertArray(b);
- int sumNotA = 0;
- int[] res = new int[c1.length];
- for (int q = 0; q < b.length; q++) {
- res[q] = a[q] * c1[q];
- }
- sumNotA = getArraySum(res);
- if (sumNotA % 2 != 0) {
- return true;
- } else {
- return false;
- }
- } else {
- return false;
- }
- }
- boolean recursivePartition(int[] a, int[] b, int p) {
- boolean cond = false;
- if (p == 0) {
- cond = check(a, b);
- } else {
- b[p - 1] = 0;
- cond = recursivePartition(a, b, p - 1);
- if (!cond) { // 2° call
- b[p - 1] = 1;
- cond = recursivePartition(a, b, p - 1);
- }
- }
- return cond;
- }
- boolean strangePartition(int[] a) {
- return recursivePartition(a, new int[a.length], a.length);
- }
- int[] a = {82, 17, 76, 18, 62};
- System.out.println(strangePartition(a));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement