Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

back to basics #11

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions Array/MidIndexFinder/Problem.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Problem: https://leetcode.com/problems/find-the-middle-index-in-array/
```
Given a 0-indexed integer array nums, find the leftmost middleIndex (i.e., the smallest amongst all the possible ones).

A middleIndex is an index where nums[0] + nums[1] + ... + nums[middleIndex-1] == nums[middleIndex+1] + nums[middleIndex+2] + ... + nums[nums.length-1].

If middleIndex == 0, the left side sum is considered to be 0. Similarly, if middleIndex == nums.length - 1, the right side sum is considered to be 0.

Return the leftmost middleIndex that satisfies the condition, or -1 if there is no such index.



Example 1:

Input: nums = [2,3,-1,8,4]
Output: 3
Explanation: The sum of the numbers before index 3 is: 2 + 3 + -1 = 4
The sum of the numbers after index 3 is: 4 = 4
Example 2:

Input: nums = [1,-1,4]
Output: 2
Explanation: The sum of the numbers before index 2 is: 1 + -1 = 0
The sum of the numbers after index 2 is: 0
Example 3:

Input: nums = [2,5]
Output: -1
Explanation: There is no valid middleIndex.


Constraints:

1 <= nums.length <= 100
-1000 <= nums[i] <= 1000

```
File renamed without changes.
17 changes: 17 additions & 0 deletions Array/MidIndexFinder/go/MidIndexFinder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package _go

func findMiddleIndex(nums []int) int {
sum, leftPrefixSum, rightPrefixSum := 0, 0, 0
for _, num := range nums {
sum += num
}
for i := 0; i < len(nums); i++ {
rightPrefixSum = sum - leftPrefixSum - nums[i]
if leftPrefixSum == rightPrefixSum {
return i
}
leftPrefixSum += nums[i]
}

return -1
}
45 changes: 45 additions & 0 deletions Array/MidIndexFinder/go/MidIndexFinder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package _go

import (
"testing"
)

func Test_findMiddleIndex(t *testing.T) {
type args struct {
nums []int
}
tests := []struct {
name string
args args
want int
}{
{
name: "Test 1",
args: args{
nums: []int{2, 3, -1, 8, 4},
},
want: 3,
},
{
name: "Test 2",
args: args{
nums: []int{1, -1, 4},
},
want: 2,
},
{
name: "Test 3",
args: args{
nums: []int{2, 5},
},
want: -1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := findMiddleIndex(tt.args.nums); got != tt.want {
t.Errorf("findMiddleIndex() = %v, want %v", got, tt.want)
}
})
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Most of these problems are solved using C++. My goal is to publish solutions for
- [Check if the coordinates are in a straight line](https://github.com/ravitandon90/LeetCode-TopProblems/blob/main/Array/LineChecker/Problem.md)
- [Make Two Arrays Equal by Reversing Sub-arrays](https://github.com/ravitandon90/LeetCode-TopProblems/blob/main/Array/MakeTwoArraysEqual/Problem.md)
- [Merge N Sorted Arrays](https://github.com/ravitandon90/LeetCode-TopProblems/blob/main/Array/MergeSortedArray/Problem.md)
- [Find the middle index number of the array](https://github.com/ravitandon90/LeetCode-TopProblems/blob/main/Array/MiddleIndex/Problem.md)
- [Find the missing number from an array](https://github.com/ravitandon90/LeetCode-TopProblems/blob/main/Array/MissingNumber.cpp)
- [Find the missing ranges from an array](https://github.com/ravitandon90/LeetCode-TopProblems/blob/main/Array/MissingRangeFinder.cpp)
- [Move Zeroes](https://github.com/ravitandon90/LeetCode-TopProblems/blob/main/Array/MoveZeroes.cpp)
Expand Down