Skip to content

Commit 3b9b714

Browse files
authored
Create Split Array
1 parent 4d552e3 commit 3b9b714

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
Given an integer array A of size N, check if the input array can be splitted in two parts such that -
3+
- Sum of both parts is equal
4+
- All elements in the input, which are divisible by 5 should be in same group.
5+
- All elements in the input, which are divisible by 3 (but not divisible by 5) should be in other group.
6+
- Elements which are neither divisible by 5 nor by 3, can be put in any group.
7+
Groups can be made with any set of elements, i.e. elements need not to be continuous. And you need to consider each and every element of input array in some group.
8+
Return true, if array can be split according to the above rules, else return false.
9+
Note : You will get marks only if all the test cases are passed.
10+
11+
Input Format :
12+
Line 1 : Integer N (size of array)
13+
Line 2 : Array A elements (separated by space)
14+
15+
Output Format :
16+
true or false
17+
18+
Constraints :
19+
1 <= N <= 50
20+
21+
Sample Input 1 :
22+
2
23+
1 2
24+
Sample Output 1 :
25+
false
26+
27+
Sample Input 2 :
28+
3
29+
1 4 3
30+
Sample Output 2 :
31+
true
32+
*/
33+
34+
public class solution {
35+
36+
public static boolean splitArray(int input[]) {
37+
/* Your class should be named solution
38+
* Don't write main().
39+
* Don't read input, it is passed as function argument.
40+
* Return output and don't print it.
41+
* Taking input and printing output is handled automatically.
42+
*/
43+
return splitArray(input,0,0,0);
44+
}
45+
46+
public static boolean splitArray(int arr[], int si, int lsum, int rsum)
47+
{
48+
//lsum for sum of numbers divisible by 5 and rsum for sum of numbers divisible by 3
49+
50+
if (si == arr.length)
51+
{
52+
return lsum == rsum;
53+
}
54+
55+
if (arr[si] % 5 == 0)
56+
{
57+
lsum += arr[si];
58+
}
59+
else if (arr[si] % 3 == 0)
60+
{
61+
rsum += arr[si];
62+
}
63+
else
64+
{
65+
return splitArray(arr,si+1, lsum+arr[si], rsum) || splitArray(arr,si+1,lsum,rsum+arr[si]);
66+
}
67+
68+
return splitArray(arr,si+1,lsum,rsum);
69+
}
70+
}

0 commit comments

Comments
 (0)