-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d56bd2f
commit c37df64
Showing
70 changed files
with
368 additions
and
238 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
## Problem: Balanced Arrays | ||
|
||
### Description | ||
|
||
Given an array of integers, you are allowed to remove at most one element. After potentially removing one element, determine the maximum sum of elements where the sum of the elements on the left of a certain index is equal to the sum of the elements on the right of that index. If no such equilibrium can be achieved, even after a removal, return -1. | ||
|
||
### Constraints | ||
- The array `arr` consists of integers. | ||
- The size of `arr` is in the range `[3, 10^5]`. | ||
- Each element in the array is in the range `[1, 10^9]`. | ||
|
||
### Output | ||
- Return the maximum sum of the array (excluding the removed element if any) where an equilibrium index exists after potentially removing one element. If no such equilibrium can be found, return `-1`. | ||
|
||
|
||
### Test Case 1 | ||
**Input:** | ||
``` | ||
[6, 1, 6, 3] | ||
``` | ||
**Explanation:** | ||
Without removing any elements, no equilibrium index exists. By removing the last element `3`, the array becomes `[6, 1, 6]`. We can achieve an equilibrium at index `1` (zero-based index), with `6` on both sides. The sum on either side is `6`. | ||
|
||
The other valid partitions (just the partition not the paritions with the sum equal) of `[6,1,6,3]` are : | ||
- `[]` and `[1,6,3]` | ||
- `[6]` and `[6,3]` | ||
- `[6,1]` and `[3]` | ||
- `[6,1,6]` and `[]` | ||
|
||
|
||
**Output:** | ||
``` | ||
6 | ||
``` | ||
|
||
### Test Case 2 | ||
**Input:** | ||
``` | ||
[3, 3, 5, 3, 3] | ||
``` | ||
**Explanation:** | ||
Without removing any elements, the array has an equilibrium index at `2` (zero-based index), with `[3, 3]` on the left and `[3, 3]` on the right, both summing to `6`. No need to remove any elements as this already provides a valid equilibrium. | ||
|
||
**Output:** | ||
``` | ||
6 | ||
``` | ||
|
||
### Test Case 3 | ||
**Input:** | ||
``` | ||
[2, 1, 1, 2] | ||
``` | ||
**Explanation:** | ||
This array needs the removal of either of the 1's both give the same answer. | ||
After removing the first 1 the array becomes `[2,1,2]` | ||
Then we can partition it using the `1` at the `1st` index | ||
The sums of the parts `[2]` on the left and `[2]` on the right (excluding the middle `1`) are both equal to `2`. | ||
|
||
**Output:** | ||
``` | ||
2 | ||
``` | ||
|
||
### Test Case 4 | ||
**Input:** | ||
``` | ||
[10, 20, 10, 5, 15, 5, 10] | ||
``` | ||
**Explanation:** | ||
Removing the element `5` at index `3` results in the array `[10, 20, 10, 15, 5, 10]`. No equilibrium can be found in this modified array that yields a sum larger than any other configuration. Without any removal, equilibrium occurs with the index `2`, but the sum to each side (excluding index `2`) doesn't exceed `30` from other possible configurations. | ||
|
||
**Output:** | ||
``` | ||
30 | ||
``` | ||
|
||
### Test Case 5 | ||
**Input:** | ||
``` | ||
[1, 2, 3] | ||
``` | ||
**Explanation:** | ||
There's no possible way to remove an element and achieve equilibrium in this array. With or without removal, no index meets the requirement where both sides are equal. | ||
|
||
**Output:** | ||
``` | ||
-1 | ||
``` | ||
|
||
### Note | ||
- If there is no solution you have to return `-1` else you will get a Wrong Answer Verdict | ||
- The sum of an empty parition `[]` is considered to be `0` | ||
- The solution should aim to operate in a time complexity that allows for efficient processing, ideally better than `O(n^2)`, considering potential array sizes. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
g++ solutions/ans.cpp -o solutions/ans | ||
|
||
# Create the output directory if it doesn't exist | ||
mkdir -p tests/output | ||
|
||
# Loop through all the input files | ||
for input_file in tests/input/*.txt; do | ||
# Extract the filename without the extension | ||
base_name=$(basename -- "$input_file" .txt) | ||
|
||
# Run the compiled program with input and generate the output | ||
./solutions/ans < "$input_file" > "tests/output/$base_name.txt" | ||
if [ $? -ne 0 ]; then | ||
echo "Error processing file: $input_file" | ||
fi | ||
done | ||
|
||
echo "All tests have been processed." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ARRAYS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Problem Name: "Balanced Arrays" | ||
Function Name: solve | ||
Input Structure: | ||
Input Field: list<int> A | ||
Output Structure: | ||
Output Field: int result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
4 | ||
6 1 6 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
5 | ||
3 3 5 3 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
4 | ||
2 1 1 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
7 | ||
10 20 10 5 15 5 10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
3 | ||
1 2 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
3 | ||
1 1 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
3 | ||
1 3 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
6 |
Oops, something went wrong.