Skip to content

Commit aed551e

Browse files
authored
Merge pull request #1950 from vivekvardhan2810/main
Split Array With Same Average (Leetcode) Added problem number 805
2 parents e95696b + b67332c commit aed551e

File tree

1 file changed

+151
-0
lines changed

1 file changed

+151
-0
lines changed
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
---
2+
id: split-array-with-same-average
3+
title: Split Array With Same Average
4+
sidebar_label: 0805 Split Array With Same Average
5+
tags:
6+
- Java
7+
- Math
8+
- Array
9+
- Binary Search
10+
- Dynamic Programming
11+
- Bitmask
12+
description: "This document provides a solution where we split an array into two non-empty subsets such that both subsets have the same average."
13+
---
14+
15+
## Problem
16+
17+
You are given an integer array $nums$.
18+
19+
You should move each element of $nums$ into one of the two arrays $A$ and $B$ such that $A$ and $B$ are non-empty, and $average(A) == average(B)$.
20+
21+
Return $true$ if it is possible to achieve that and $false$ otherwise.
22+
23+
**Note** that for an array $arr$, $average(arr)$ is the sum of all the elements of $arr$ over the length of $arr$.
24+
25+
### Examples
26+
27+
**Example 1:**
28+
29+
```
30+
Input: nums = [1,2,3,4,5,6,7,8]
31+
32+
Output: true
33+
34+
Explanation: We can split the array into [1,4,5,8] and [2,3,6,7], and both of them have an average of 4.5 .
35+
36+
```
37+
**Example 2:**
38+
39+
```
40+
Input: nums = [3,1]
41+
42+
Output: false
43+
44+
```
45+
46+
### Constraints
47+
48+
- $1 <= nums.length <= 30$
49+
- $0 <= nums[i] <= 10^4$
50+
---
51+
52+
## Approach
53+
54+
To solve the problem, we need to understand the nature of the allowed moves:
55+
56+
1. **Calculate Total Sum**:
57+
58+
- First, calculate the total sum of the array.
59+
60+
2. **Sort the Array**:
61+
62+
- Sorting helps in pruning the search space.
63+
64+
3. **Iterate Over Possible Sizes**:
65+
66+
- Iterate over possible subset sizes from 1 to n/2. For each size, check if the corresponding subset sum can be an integer.
67+
68+
4. **Check for Possible Partition**:
69+
70+
- Use a recursive function with memoization to check if it’s possible to partition the array into subsets with the required sum and size.
71+
72+
## Solution for Split Array With Same Average
73+
74+
- The problem requires us to split an array into two non-empty subsets such that both subsets have the same average.
75+
76+
- For two subsets to have the same average, the sum of elements in each subset divided by their respective lengths must be equal.
77+
78+
#### Code in Java
79+
80+
```java
81+
import java.util.Arrays;
82+
83+
class Solution {
84+
public boolean splitArraySameAverage(int[] nums) {
85+
int n = nums.length;
86+
int totalSum = Arrays.stream(nums).sum();
87+
88+
Arrays.sort(nums);
89+
90+
for (int size = 1; size <= n / 2; size++) {
91+
if (totalSum * size % n == 0) {
92+
int target = totalSum * size / n;
93+
if (canPartition(nums, size, target, 0)) {
94+
return true;
95+
}
96+
}
97+
}
98+
99+
return false;
100+
}
101+
102+
private boolean canPartition(int[] nums, int size, int target, int start) {
103+
if (size == 0) {
104+
return target == 0;
105+
}
106+
107+
for (int i = start; i <= nums.length - size; i++) {
108+
if (i > start && nums[i] == nums[i - 1]) {
109+
continue;
110+
}
111+
112+
if (nums[i] > target) {
113+
break;
114+
}
115+
116+
if (canPartition(nums, size - 1, target - nums[i], i + 1)) {
117+
return true;
118+
}
119+
}
120+
121+
return false;
122+
}
123+
124+
public static void main(String[] args) {
125+
Solution sol = new Solution();
126+
127+
// Test cases
128+
int[] nums1 = {1, 2, 3, 4, 5, 6, 7, 8};
129+
System.out.println(sol.splitArraySameAverage(nums1)); // Output: true
130+
131+
int[] nums2 = {3, 1};
132+
System.out.println(sol.splitArraySameAverage(nums2)); // Output: false
133+
}
134+
}
135+
```
136+
137+
### Complexity Analysis
138+
139+
#### Time Complexity: $O(2^ n/2)$
140+
141+
> **Reason**: Time Complexity is $O(2^ n/2)$. Because Each half of the array can generate $O(2^ n/2)$ subsets.
142+
143+
#### Space Complexity: $O(2^ n/2)$
144+
145+
> **Reason**: The space complexity is $O(2^ n/2)$, Because it helps in Storing subset sums and sizes.
146+
147+
# References
148+
149+
- **LeetCode Problem:** [Split Array With Same Average](https://leetcode.com/problems/split-array-with-same-average/description/)
150+
- **Solution Link:** [Split Array With Same Average Solution on LeetCode](https://leetcode.com/problems/split-array-with-same-average/solutions/)
151+
- **Authors LeetCode Profile:** [Vivek Vardhan](https://leetcode.com/u/vivekvardhan43862/)

0 commit comments

Comments
 (0)