forked from sonal2203/SimpleCode1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleCode1
More file actions
39 lines (29 loc) · 1.07 KB
/
Copy pathSimpleCode1
File metadata and controls
39 lines (29 loc) · 1.07 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
public class ProductOfSums {
public static int productOfSums(int[] arr, int n) {
if (n < 3 || arr == null) {
return -1;
}
// Find the sum of the first part of the array.
int sumOfFirstPart = 0;
for (int i = 0; i < n / 2; i++) {
sumOfFirstPart += arr[i];
}
// Findind the sum of the second part of the array.
int sumOfSecondPart = 0;
for (int i = n / 2; i < n; i++) {
sumOfSecondPart += arr[i];
}
// Return the sum of the product of the sum of first and second part.
return sumOfFirstPart * sumOfSecondPart;
}
public static void main(String[] args) {
// Create an array.
int[] arr = {3, 5, 14, 12, 10, 74};
// Get the length of the array.
int n = arr.length;
// Find the sum of the product of the sum of first and second part of the array.
int productOfSums = productOfSums(arr, n);
// Print the result.
System.out.println("The product of sums is: " + productOfSums);
}
}