Problem Statement :: In Java. Given an array of ints, you can select a group of some of ints, so the group is summed up for a given purpose with this additional limitation: if they must be selected or not selected in an array that is adjacent and identical in value. For example, with the array {1, 2, 2, 2, 5, 2}, or all three 2 in the middle should be selected or not, all as a group. (one cycle can be used to determine the degree of identical values).
groupSumClump(0, {2, 4, 8}, 10) โ true
groupSumClump(0, {1, 2, 4, 8, 1}, 14) โ true
groupSumClump(0, {2, 4, 4, 8}, 14) โ false --> Failing Test Case
groupSumClump(0, {8, 2, 2, 1}, 9) โ true --> Failing Test Case
groupSumClump(0, {8, 2, 2, 1}, 11) โ false --> NegativeArraySizeException
I did the initial analysis, and the partial code is below.
public boolean groupSumClump(int start, int[] nums, int target) {
start = 0;
boolean flag = false;
int highestInteger = getTheBiggest(nums);
if (highestInteger > target) {
flag = false;
} else {
int variable = 0;
for (int i = 0; i < nums.length; i++) {
variable += nums[i];
}
if (variable == target) {
flag = true;
} else {
if (variable < target) {
flag = false;
} else {
flag = summate(highestInteger, target, nums);
}
}
}
return flag;
}
private boolean summate(int highestInteger, int target, int[] nums) {
boolean val = false;
if (highestInteger == target) {
val = true;
} else {
int[] temp = new int[nums.length - 1];
int var = 0;
if ((target - highestInteger) > 0) {
for (int j = 0; j < nums.length-1; j++) {
if (nums[j] != highestInteger) {
temp[var] = nums[j];
if (temp[var] == (target - highestInteger)) {
val = true;
return val;
}
var++;
}
}
val = summate(getTheBiggest(temp), target - highestInteger,
temp);
}
}
return val;
}
private int getTheBiggest(int[] nums) {
int biggestInteger = 0;
for (int i = 0; i < nums.length; i++) {
if (biggestInteger < nums[i]) {
biggestInteger = nums[i];
}
}
return biggestInteger;
}
: , :
Additional Constraint , , , , , . , {1, 2, 2, 2, 5, 2}, 2 , . ( ).
.
, .
.
Culd, , / : - ((
, , group.so , . , . .
โ MISSINGNO: , . .
groupSumClump (0, {2, 4, 4, 8}, 14) โ false
2
8
4
โ true, .
for(int number=0;number<nums.length-1;number++){
if(nums[number]==nums[number+1]){
nums[number]=nums[number]+nums[number+1];
}
}