Related
Consider the character string generated by the following rule:
F[0] = "A"
F[1] = "B"
...
F[n] = F[n-1] + F[n-2] with n > 1
Given two positive integers n and k. Let's count the number of characters 'B' in the first k positions of string F[n].
I came up with this idea and got time limit exceeded error:
public class Solution {
public static long[] F = new long[50];
public static Scanner input = new Scanner(System.in);
public static long count(int n, long k) {
if (n == 0 || k == 0) return 0;
else if (n == 1) return 1;
else {
if (k > F[n - 1]) return count(n - 1, F[n - 1]) + count(n - 2, k - F[n - 1]);
else return count(n - 1, k);
}
}
public static void main(String[] args) {
F[0] = 1; F[1] = 1;
for (int i = 2; i < 46; i++) F[i] = F[i - 2] + F[i - 1];
int T = input.nextInt();
while (T-- > 0) {
int n = input.nextInt();
long k = input.nextLong();
System.out.println(count(n, k));
}
}
}
Can someone help me to improve time complexity? Seems my solution has O(n^2) time complexity.
Test case for this question:
Input
Output
4
0 1
0
1 1
1
3 2
1
7 7
4
There seems to be a pattern related to Fibonacci numbers:
A
B
AB 1 + 1 (A count + B count)
BAB 1 + 2
ABBAB 2 + 3
BABABBAB 3 + 5
ABBABBABABBAB 5 + 8
^ k = 7
BABABBAB 3 + 5
^ k = 2 (result = 3)
BAB 1 + 2
^ k = 2 (result = 4)
AB 1 + 1
^ k = 1 = A (result = 4)
Let g(l, r, k) represent the count of Bs in the first k positions of Fib[n] = l + r. Then:
g(l, r, k):
if (1, 1) == (l, r):
return 1 if k == 2 else 0
if (1, 2) == (l, r):
return 1 if k < 3 else 2
ll, rl = getFibSummands(l)
lr, rr = getFibSummands(r)
if k > l:
return rl + g(lr, rr, k - l)
return g(ll, rl, k)
This answer above may have misinterpreted the starting order of concatenation, which possibly should be BA, in which case, we would need to reverse l, r.
A
B
BA 1 + 1 (B count + A count)
BAB 2 + 1
BABBA 3 + 2
BABBABAB 5 + 3
BABBABABBABBA 8 + 5
^ k = 7
BABBABAB
^ k = 7
BAB
^ k = 2 (result = 3)
BA
^ k = 2
A
^ k = 1 (result = 4)
g(l, r, k):
if (1, 1) == (l, r):
return 1 if k == 2 else 0
if (2, 1) == (l, r):
return 1 if k < 3 else 2
ll, rl = getFibSummands(l)
lr, rr = getFibSummands(r)
if k > l:
return ll + g(lr, rr, k - l)
return g(ll, rl, k)
For clarity, define Fib(n) to be the Fibonacci sequence where Fib(0) = Fib(1) = 1, and Fib(n+2) = Fib(n+1) + Fib(n).
Note that F[n] has Fib(n) characters, with Fib(n-1) of them being Bs.
Let C(n, k) be the number of B's in the first k characters of F[n].
The base cases are obvious
C(0, k) = 0
C(n, k) = 0 if k<=0
C(1, 1) = 1
C(2, k) = 1
In general:
C(n, k) = C(n-1, k) if k <= Fib(n-1)
= Fib(n-2) + C(n-1, k - Fib(n-1)) otherwise
This is the observation that F[n] = F[n-1] + F[n-2] and k lies in either the first part or the second. The first part has Fib(n-1) characters of which Fib(n-2) of them are B's.
If you precompute the Fibonacci numbers from 0 to n, then you can compute C(n, k) in O(n) arithmetic operations.
You tagged java, but here's a python solution including test cases:
def C(n, k):
if n == 0: return 0
total = 0
fib = [1, 1]
while len(fib) <= n and fib[-1] <= k:
fib.append(fib[-1] + fib[-2])
n = min(n, len(fib) - 1)
while True:
if n <= 2:
return total + 1
elif k <= fib[n-1]:
n -= 1
else:
total += fib[n-2]
k -= fib[n-1]
n -= 1
tcs = [
([0, 1], 0),
([1, 1], 1),
([3, 2], 1),
([7, 7], 4)
]
for (n, k), want in tcs:
got = C(n, k)
if got != want:
print('C(%d, %d) = %d, want %d' % (n, k, got, want))
This includes an optimization which reduces n so that initially k > Fib(n-1). This makes the code O(min(n, log(k))) arithmetic operations.
Consider the following serie:
1, 1, 2, 2, 4, 8, 32, 256, ...
I wrote this method in Java:
public static int Fibmul(int n){
if (n == 1) return 1;
else if (n == 0) return 0;
else if (n < 0) return -1; // -1 means 'nil'
else {
n = Fibmul(n - 2) * Fibmul(n - 1);
}
return n;
}
To calculate the serie just multiply the last two positions of the elements to obtain the next element, E.g Fibmul(4) should return 4 and Fibmul(6) should return 32.
But this code is wrong an I don't have more ideas, I have clear the algorithm over the paper but I don't know how to implement it.
Can anybody help me?
Thanks in advance.
You're not going to get any higher numbers if your starting numbers are 0 and/or 1.
If your starting numbers are 1 and 2, you will get something like the sequence you described.
public static int Fibmul(int n){
if (n == 1) return 2;
else if (n == 0) return 1;
else if (n < 0) return -1; // -1 means 'nil'
else {
n = Fibmul(n - 2) * Fibmul(n - 1);
}
return n;
}
This will give
1, 2, 2, 4, 8, 32, ...
It cannot start with 1,1,2 if you want to follow the rule you stated, because 1*1 does not equal 2.
NB: Your sequence is actually the ordinary Fibonacci series but with each term used as a power of 2.
Fibonacci: 0, 1, 1, 2, 3, 5, 13, ...
Fibmul: 2**0, 2**1, 2**1, 2**2, 2**3, 2**5, 2**13, ...
You simply need an extra solution for when n == 2 because otherwise you'll remain stuck with 1, 1, 1, 1, 1, ...
public static int Fibmul(int n) {
if (n == 2)
return 2;
if (n == 1)
return 1;
else if (n == 0)
return 0;
else if (n < 0)
return -1; // -1 means 'nil'
return Fibmul(n - 2) * Fibmul(n - 1);
}
Testing it works as expected
Fibmul(0) = 0
Fibmul(1) = 1
Fibmul(2) = 2
Fibmul(3) = 2
Fibmul(4) = 4
Fibmul(5) = 8
Fibmul(6) = 32
Fibmul(7) = 256
Fibmul(8) = 8192
Fibmul(9) = 2097152
I think you are missing the 2 value. This should return 2, and the rest is fine:
else if (n == 2) return 2;
See here: https://code.sololearn.com/ca22A0a236a2
public class Solution {
public ArrayList<Integer> plusOne(ArrayList<Integer> A) {
int n = A.size();
// Add 1 to last digit and find carry
A.set(n - 1, A.get(n - 1) + 1);
int carry = A.get(n - 1) / 10;
A.set(n - 1, A.get(n - 1) % 10);
// Traverse from second last digit
for (int i = n - 2; i >= 0; i--) {
if (carry == 1) {
A.set(i, A.get(i) + 1);
carry = A.get(i) / 10;
A.set(i, A.get(i) % 10);
}
}
// If carry is 1, we need to add
// a 1 at the beginning of vector
if (carry == 1)
A.add(0, 1);
return A;
}
}
Question is:
Given a non-negative number represented as an array of digits,
add 1 to the number ( increment the number represented by the digits ).
The digits are stored such that the most significant digit is at the head of the list.
Example:
If the vector has [1, 2, 3]
the returned vector should be [1, 2, 4]
as 123 + 1 = 124.
Wrong Answer. Your program's output doesn't match the expected output. You can try testing your code with custom input and try putting debug statements in your code.
Your submission failed for the following input:
A : [ 0, 0, 4, 4, 6, 0, 9, 6, 5, 1 ]
Your function returned the following :
0 4 4 6 0 9 6 5 2
The expected returned value :
4 4 6 0 9 6 5 2
Use below method
private ArrayList<Integer> recursiveCheckZero() {
if (arrayList.get(0) == 0) {
arrayList.remove(0);
recursiveCheckZero();
} else {
return arrayList;
}
}
This method will be used to zero at first position, it would be called recursively until all zeros get removed. and when there will be no zero at first position it will return final ArrayList of integers as actual result
int sum=0;
int carry=0;
int i=0;
while (i < A.size() - 1 && A.get(i) == 0) {
A.remove(i); //remove all zeroes in front
}
for(i=A.size()-1;i>=0;i--)
{
int n=A.get(i);
sum=n+carry;
if(i==A.size()-1)
{
sum = sum+1;
}
carry=sum/10;
sum=sum%10;
A.set(i,sum);
}
if (carry !=0)
A.add(0,1);
return A;
I am doing some practice problems for a competition and I have been working on this algorithm like all day. If you want to read the whole problem here it is, but I will give you a short explanation because it is kind of a long problem.
Problem:
You have to verify ID numbers by plugging the ID number into a checksum. The ID needs to be converted to base-10 before you can plug it into the algorithm. The ID number starts out as letters:
Z = 0, Y = 1, X = 2, W = 3, V = 4
I am not having trouble with the conversion from these letters to base-10, my conversion code is good so I'll show you the next part of the problem:
Part 2:
Once you have your base-10 ID number you need to plug it into the following algorithm:
Note: each ID number MUST be 8 digits long, 0's will precede a number that is not at least 8 digits.
checksum = F(0, d0) X F(1, d1) X F(2, d2) ...
So to simplify:
checksum = F(n, dn) X F(n+1, dn) ...
where n is the index of the digit
What is most important here, is that X is not the operation * (multiply). X is it's own operation defined later.
Note: The most significant digit seems to be d7 but I'm not sure, the problem is not very clear about it.
Here are the definitions for f(n1, n2), g(n) and the operator X:
f(n1, n2) =
g(n) =
operator X:
I assumed mod is the same thing as % in my code, I was not sure if there was another mod operation I am not familiar with.
My Structure
This is how I decided I wanted to solve the problem:
Convert the base-10 number into int[8]
Put each digit of the int[8] through f(n, dn)
Use the X operator to then combine them all together.
My Code
Here are my algorithm functions. I can comment them if they are confusing somewhere, but they really follow the algorithm listed above exactly.
/*
* This will return the checksum of the id.
* Formula: F(0, d0) X F(1, d1) ...
*
* F(n, dn) where n is the current index.
* X != * (multiply)!! X is a defined operator
*/
public static int getChecksum(int[] id)
{
int result = 0;
for(int x = 0;x < id.length;x++)
{
if(x == 0)
result = fOfxd(x, id[x]);
else{
result = opX(result, fOfxd(x, id[x]));
}
}
return result;
}
public static int gOfx(int x)
{
return GOFX[x];
}
public static int fOfxd(int x, int d)
{
switch(x)
{
case 0:
return d;
case 1:
return gOfx(d);
default:
return fOfxd(x - 1, gOfx(d));
}
}
public static int opX(int num1, int num2)
{
if(num1 < 5 && num2 < 5)
return (num1 + num2) % 5;
if(num1 < 5 && num2 >= 5)
return (num1 + (num2 - 5)) % 5 + 5;
if(num1 >= 5 && num2 < 5)
return ((num1 - 5) - num2) % 5 + 5;
return (num1 - num2) % 5;
}
public static final int[] GOFX = {1, 5, 7, 6, 2, 8, 3, 0, 9, 4};
Now, here is my main(String args[]) code:
Note: You can assume the functions parseBase10, and toArray are functioning properly. I have checked them with the input / output examples in the problem.
public static void main(String args[])
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while(true)
{
int ids = 0; // how many ids are we checking?
try
{
ids = Integer.parseInt(reader.readLine()); // get user input
String[] list = new String[ids]; // will hold all of the ids
for(int x = 0;x < list.length;x++)
list[x] = reader.readLine(); // reads all of the ids we will be checking
for(int x = 0;x < list.length;x++) // lets check the ids individually now
{
String stringID = list[x]; // the string representation of the id
int base10 = parseBase10(stringID);
int[] id = toArray(base10);
int checksum = getChecksum(id);
System.out.println(stringID);
System.out.println(base10);
System.out.println(Arrays.toString(id));
System.out.println(checksum);
}
}catch(Exception e){e.printStackTrace();}
break;
}
}
Want to compile it yourself?
Here is my full (unedited) code:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
public class Main
{
public static void main(String args[])
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while(true)
{
int ids = 0; // how many ids are we checking?
try
{
ids = Integer.parseInt(reader.readLine()); // get user input
String[] list = new String[ids]; // will hold all of the ids
for(int x = 0;x < list.length;x++)
list[x] = reader.readLine(); // reads all of the ids we will be checking
for(int x = 0;x < list.length;x++) // lets check the ids individually now
{
String stringID = list[x]; // the string representation of the id
int base10 = parseBase10(stringID);
int[] id = toArray(base10);
int checksum = getChecksum(id);
System.out.println(stringID);
System.out.println(base10);
System.out.println(Arrays.toString(id));
System.out.println(checksum);
}
}catch(Exception e){e.printStackTrace();}
break;
}
}
/*
* This will return the checksum of the id.
* Formula: F(0, d0) X F(1, d1) ...
*
* F(n, dn) where n is the current index.
* X != * (multiply)!! X is a defined operator
*/
public static int getChecksum(int[] id)
{
int result = 0;
for(int x = 0;x < id.length;x++)
{
if(x == 0)
result = fOfxd(x, id[x]);
else{
result = opX(result, fOfxd(x, id[x]));
}
}
return result;
}
public static int gOfx(int x)
{
return GOFX[x];
}
public static int fOfxd(int x, int d)
{
switch(x)
{
case 0:
return d;
case 1:
return gOfx(d);
default:
return fOfxd(x - 1, gOfx(d));
}
}
public static int opX(int num1, int num2)
{
if(num1 < 5 && num2 < 5)
return (num1 + num2) % 5;
if(num1 < 5 && num2 >= 5)
return (num1 + (num2 - 5)) % 5 + 5;
if(num1 >= 5 && num2 < 5)
return ((num1 - 5) - num2) % 5 + 5;
return (num1 - num2) % 5;
}
/*
* This will convert a number to an array equivalent of that number
* The result will be 8 digites long with leading 0's if possible.
*
* EX:
* 12345 = {0, 0, 1, 2, 3, 4, 5, 6}
*/
public static int[] toArray(int value)
{
int result[] = new int[8];
for(int x = result.length - 1;x >= 0;x--)
{
result[x] = value % 10;
value /= 10;
}
return result;
}
/*
* converts a String sequence and converts it to a base 10 equivalent.
* Z = 0, Y = 1, X = 2, W = 3, V = 4
*
* EX:
* YY = 11(base-5) = 6(base-10)
*/
public static int parseBase10(String string) throws Exception
{
int multiplier = 1;
int result = 0; // in base 10
for(int x = string.length() - 1;x >= 0;x--)
{
char letter = string.charAt(x); // the letter we are parsing
int value = -1; // initial value, set to -1 to check for parsing error
for(int y = 0;y < VALUES.length;y++)
if(letter == VALUES[y])
value = y; // letter found in VALUES[]
if(value == -1)
throw new Exception("Could not parse: " + letter); // the specified letter was not found
result += (multiplier * value);
/* ^^ this moves the value to the correct digit place by using a multiplier:
* EX:
*
* current result: 45 (base-10)
* new value to parse: 2 (base-5)
* 45(base-10) + (2(base-5) * 25(base-10)) = 245 <-- correct output
*/
multiplier *= 5; // sets up multiplier for next value
}
return result;
}
public static final char[] VALUES = {'Z', 'Y', 'X', 'W', 'V'};
public static final int[] GOFX = {1, 5, 7, 6, 2, 8, 3, 0, 9, 4};
}
Here is the input I am giving my problem:
6
WYYXWVZXX
YWYWYYXWVZYY
YWYWYYXWVZYX
YYZWYYXWVZYX
YXXWYYXWVZXW
XYXWYYXWXYY
Here is what I get:
WYYXWVZXX
1274262
[0, 1, 2, 7, 4, 2, 6, 2]
2 *0*
YWYWYYXWVZYY
81352381
[8, 1, 3, 5, 2, 3, 8, 1]
0
YWYWYYXWVZYX
81352382
[8, 1, 3, 5, 2, 3, 8, 2]
4
YYZWYYXWVZYX
59868007
[5, 9, 8, 6, 8, 0, 0, 7]
0
YXXWYYXWVZXW
73539888
[7, 3, 5, 3, 9, 8, 8, 8]
5 *0*
XYXWYYXWXYY
22520431
[2, 2, 5, 2, 0, 4, 3, 1]
3 *0*
Where you see the *0*'s is where I am supposed to be getting 0, but I am getting a different value. Where is my checksum algorithm messing up?
Reading all of that feel free to ask for clarification on any part of my code.
BUG 1
The error is subtle. First of all, the digit description in the problem is: d7 d6 ... d1 d0
that means, d0 is the unit value of the decimal number.
Then, they say that F is left associative, and describe the process as :
F(0,d0) x F(1,d1) x F(2,d2) x ... x F(6,d6) x F(7,d7)
that means, you must first apply F to the operator to d0. BUT when you create the int array, the element at the 0 index is d7 , and since in this case the order matters, you get a wrong result.
To solve, you just have to reverse your int array rapresentation of the decimal value.
BUG 2
The second mistake is in the operation modulo 5. As you can read in the note of the problem, they say :
Note that -4 mod 5 = 1.
So copy-pasting hte operator x is a mistake. Change it with:
public static int opX(int num1, int num2)
{
if(num1 < 5 && num2 < 5)
return (num1 + num2) % 5;
if(num1 < 5 && num2 >= 5)
return (num1 + (num2 - 5)+5) % 5 + 5;
if(num1 >= 5 && num2 < 5)
return ((num1 - 5) - num2+20) % 5 + 5;
return (num1 - num2 +10) % 5;
}
and you'll get the expected result.
Here is the result with both bugs fixed :
1274262
[2, 6, 2, 4, 7, 2, 1, 0]
0
YWYWYYXWVZYY
81352381
[1, 8, 3, 2, 5, 3, 1, 8]
0
YWYWYYXWVZYX
81352382
[2, 8, 3, 2, 5, 3, 1, 8]
1
YYZWYYXWVZYX
59868007
[7, 0, 0, 8, 6, 8, 9, 5]
0
YXXWYYXWVZXW
73539888
[8, 8, 8, 9, 3, 5, 3, 7]
0
XYXWYYXWXYY
22520431
[1, 3, 4, 0, 2, 5, 2, 2]
0
EDIT
For a more general solution of the BUG 2, check Martijn Courteaux answer.
Your mod logic is broken. The website says:
Note that -4 % 5 = 1.
In Java, this is not true: (-4) % 5 == -4. So implement your own mod(int a, int b) method:
public static int mod(int a, int b)
{
while (a < 0) a += b;
while (a >= b) a -= b;
return a;
}
Or a more performant implementation as suggested by #durron597:
public static int mod(int a, int b)
{
a %= b;
return a < 0 ? a + b : a;
}
This is really important since you will have negative values here
(Eg: assume num1 = 5 and num2 = 4):
if(num1 >= 5 && num2 < 5)
return ((num1 - 5) - num2) % 5 + 5;
I had a recursion interview question problem in Java,Need your help on this.
Write a **Java function** such that :: Given an array of ints, is it possible to divide the ints into two groups, so that the sum of the two groups is the same, with these constraints: all the values that are multiple of 5 must be in one group, and all the values that are a multiple of 3 (and not a multiple of 5) must be in the other. (No loops needed.)
split53({1,1}) → true
split53({1, 1, 1}) → false
split53({2, 4, 2}) → true
PS:This was a Interview Question for hewlett packard
The question can be easily reduced to following: given a set of integers numbers and an integer target, is it possible to find a subset of numbers with sum equal to target?
Let me know if transition needs clarification.
It can be solved with DP in O(numbers.size * target) time. The idea is following
When numbers.size is 0, the only reachable sum is 0.
Suppose we have numbers == {1, 3}, in this case sums {0, 1, 3, 4} are available. What if we add another element to numbers, 4? Now, all old sums can still be reached and some new ones too: {0 + 4, 1 + 4, 3 + 4, 4 + 4}. Thus, for numbers == {1, 3, 4}, available sums are {0, 1, 3, 4, 5, 7, 8}.
In this fashion, adding number by number, you can build the list of reachable sums.
A working example (it doesn't handle negative numbers, but you can easily fix that)
boolean splittable(int[] numbers, int target) {
boolean[] reached = new boolean[target + 1];
reached[0] = true;
for (int number : numbers) {
for (int sum = target - 1; sum >= 0; --sum) {
if (reached[sum] && sum + number <= target) {
reached[sum + number] = true;
}
}
}
return reached[target];
}
Run it
System.out.println(splittable(new int[]{3, 1, 4}, 7)); // => true
System.out.println(splittable(new int[]{3, 1, 4}, 6)); // => false
edit
I just noticed the "recursion" part of the requirement. Well, DP can be rewritten as recursion with memoization, if that's the hard requirement. This would preserve runtime complexity.
edit 2
On groups. You have to assign elements divisible by 3 or 5 to respective groups before you proceed with the algorithm. Let's say, sum of all elements is s, sum of elements divisible by 3 is s3 and sum of elements divisible by 5 but not 3 is s5. In this case, after you assigned those 'special' elements, you have to split the rest that sum in one group is s/2 - s3 and in another s/2 - s5.
Very slow, but working solution:
static boolean canSplit(int[] arr, int lvl, int sum1, int sum2) {
if (arr.length == lvl) {
if (sum1 == sum2) {
return true;
} else {
return false;
}
}
if (arr[lvl] % 5 == 0) {
return canSplit(arr, lvl + 1, sum1 + arr[lvl], sum2);
} else if (arr[lvl] % 3 == 0) {
return canSplit(arr, lvl + 1, sum1, sum2 + arr[lvl]);
}
return canSplit(arr, lvl + 1, sum1 + arr[lvl], sum2) ||
canSplit(arr, lvl + 1, sum1, sum2 + arr[lvl]);
}
Call the function:
canSplit(arr, 0, 0, 0);
Code that would probably get me fired. But will work :D
Entirely recursive, equally deadly.
public boolean split53(int[] nums) {
return split_fn(0, nums, 0, 0, false, false);
}
public boolean split_fn(int start, int[] nums, int left, int right, boolean fiveLeft, boolean chosen) {
if (start >= nums.length) {
if (left == right) return true;
return false;
}
if (nums[start] % 5 == 0) {
if (!chosen) {
return split_fn(start + 1, nums, left + nums[start], right, true, true) || split_fn(start + 1, nums, left, right + nums[start], false, true);
} else {
return split_fn(start + 1, nums, left + ((fiveLeft) ? nums[start] : 0), right + ((!fiveLeft) ? nums[start] : 0), fiveLeft, chosen);
}
}
if (nums[start] % 3 == 0 && nums[start] % 5 != 0) {
if (!chosen) {
return split_fn(start + 1, nums, left + nums[start], right, false, true) || split_fn(start + 1, nums, left, right + nums[start], true, true);
} else {
return split_fn(start + 1, nums, left + ((!fiveLeft) ? nums[start] : 0), right + ((fiveLeft) ? nums[start] : 0), fiveLeft, chosen);
}
}
//standard case
return split_fn(start + 1, nums, left + nums[start], right, fiveLeft, chosen) || split_fn(start + 1, nums, left, right + nums[start], fiveLeft, chosen);
}
Here is the real recursive solution.
private boolean split2(int index, int[] nums, int sum1, int sum2) {
if (index >= nums.length) {
return sum1 == sum2;
}
if (split2(index + 1, nums, sum1 + nums[index], sum2)) {
return true;
}
if (split2(index + 1, nums, sum1, sum2 + nums[index])) {
return true;
}
return false;
}
This code goes through puting every element into one of the group. If in any combination the two groups are equal it returns true. No loops used and in only one function.
Best for all
edit: Your function takes 4 arguments as input whereas the question gets as input only the array. You have to specify that the desired function could be done using yours with the call split2(0,nums,0,0);
I don't know how fast or slow the following solution is. But precisely it is a recursive backtracking solution, which uses no loops as mentioned in the question.
Here is the code snippet:
public boolean split53(int[] nums) {
int start = 0, firstPart = 0, secondPart = 0;
if (split(start, nums, firstPart, secondPart)) {
return true;
}
return false;
}
public boolean split(int start, int[] nums, int firstPart, int secondPart) {
if (start >= nums.length) {
return (firstPart == secondPart);
}
if ((start + 1) < nums.length
&& (nums[start] % 3 != 0)
&& (nums[start + 1] % 5 != 0)
&& split(start + 2, nums, firstPart + nums[start], secondPart
+ nums[start + 1])) {
return true;
}
if ((start + 1) < nums.length
&& (nums[start + 1] % 3 != 0)
&& (nums[start] % 5 != 0)
&& split(start + 2, nums, firstPart + nums[start + 1],
secondPart + nums[start])) {
return true;
}
if ((nums[start] % 3 != 0)
&& split(start + 1, nums, firstPart + nums[start], secondPart)) {
return true;
}
if ((nums[start] % 5 != 0)
&& split(start + 1, nums, firstPart, secondPart + nums[start])) {
return true;
}
return false;
}
Hope it helps.