Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1256 from tharunkanna14/patch-5
Browse files Browse the repository at this point in the history
Create 1299_replace_elements_with_greatest_element_on_right_side.go
  • Loading branch information
Ahmad-A0 authored Nov 2, 2022
2 parents f072120 + 3b4c7e4 commit 9999adc
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions go/1299-Replace-Elements-With-Greatest-Element-On-Right-Side.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
func findMaxElement(rightMax int, lastElement int) int {
if lastElement > rightMax {
rightMax = lastElement
}
return rightMax
}

func replaceElements(arr []int) []int {
var rightMax int = -1
for i := len(arr) - 1; i >= 0; i-- {
var newMax int = findMaxElement(rightMax, arr[i])
arr[i] = rightMax
rightMax = newMax
}
return arr
}

0 comments on commit 9999adc

Please sign in to comment.