-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathSplit Array
More file actions
81 lines (68 loc) · 2.02 KB
/
Split Array
File metadata and controls
81 lines (68 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
Split Array
Send Feedback
Given an integer array A of size N, check if the input array can be splitted in two parts such that -
- Sum of both parts is equal
- All elements in the input, which are divisible by 5 should be in same group.
- All elements in the input, which are divisible by 3 (but not divisible by 5) should be in other group.
- Elements which are neither divisible by 5 nor by 3, can be put in any group.
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.
Return true, if array can be split according to the above rules, else return false.
Note : You will get marks only if all the test cases are passed.
Input Format :
Line 1 : Integer N (size of array)
Line 2 : Array A elements (separated by space)
Output Format :
true or false
Constraints :
1 <= N <= 50
Sample Input 1 :
2
1 2
Sample Output 1 :
false
Sample Input 2 :
3
1 4 3
Sample Output 2 :
true
code in java *************************************************************
import java.util.Scanner;
public class solution
{
public static boolean splitArray (int input[])
{
return splitArray (input, 0, input.length - 1, 0, 0);
}
public static boolean splitArray (int[]input, int start, int end, int lSum,
int rSum)
{
if (start > end)
{
return lSum == rSum;
}
if (input[start] % 5 == 0)
{
return splitArray (input, start + 1, end, lSum + input[start], rSum);
}
else if (input[start] % 3 == 0)
{
return splitArray (input, start + 1, end, lSum, rSum + input[start]);
}
else
{
return splitArray (input, start + 1, end, lSum + input[start], rSum)
|| splitArray (input, start + 1, end, lSum, rSum + input[start]);
}
}
public static void main (String[]args)
{
Scanner sc = new Scanner (System.in);
int n = sc.nextInt ();
int[] arr = new int[n];
for (int i = 0; i < n; i++)
{
arr[i] = sc.nextInt ();
}
System.out.print (splitArray (arr));
}
}