Skip to content

Commit 42c7350

Browse files
committed
Algoexpert - Algorithm
1 parent 1b0e5ae commit 42c7350

File tree

8 files changed

+148
-0
lines changed

8 files changed

+148
-0
lines changed

.idea/.gitignore

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/random-golang-ds-algorithms.iml

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"sort"
6+
)
7+
8+
func main() {
9+
arr := []int{1, 2, 3, 5, 6, 8, 9}
10+
fmt.Println("Sorted Squared Array: O(nlogn) ", sortedSquaredArrayBruteForeApproach(arr))
11+
fmt.Println("Sorted Squared Array: O(n)", sortedSquaredArraySolutionTwo(arr))
12+
}
13+
14+
func sortedSquaredArrayBruteForeApproach(array []int) []int {
15+
// Write your code here
16+
arr := make([]int, len(array))
17+
18+
for idx, value := range array {
19+
arr[idx] = value * value
20+
}
21+
sort.Ints(arr)
22+
return arr
23+
}
24+
25+
func sortedSquaredArraySolutionTwo(array []int) []int {
26+
startIdx := 0
27+
endIdx := len(array) - 1
28+
currentIndex := 0
29+
arr := make([]int, len(array))
30+
31+
for currentIndex < len(array) {
32+
currentIndex += 1
33+
arrayStartValue := array[startIdx] * array[startIdx]
34+
arrayEndValue := array[endIdx] * array[endIdx]
35+
if arrayStartValue < arrayEndValue {
36+
arr[len(array) - currentIndex] = arrayEndValue
37+
endIdx -= 1
38+
} else {
39+
arr[len(array) - currentIndex] = arrayStartValue
40+
startIdx += 1
41+
}
42+
}
43+
return arr
44+
}

algoexpert/two-number-sum/main.go

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
//"sort"
6+
)
7+
8+
func main() {
9+
arr := []int{1,-2,3,4,-4,6,-14,8,2}
10+
11+
target := 4
12+
fmt.Println("Number Sum: O(n^2)", twoNumberSumSolutionOne(arr, target))
13+
fmt.Println("Number Sum: O(n)", twoNumberSumSolutionTwo(arr, target))
14+
}
15+
16+
func twoNumberSumSolutionOne(array []int, target int) []int {
17+
for i := 0; i < len(array); i++ {
18+
for j := i + 1; j < len(array); j++ {
19+
if array[i] == array[j] {
20+
continue;
21+
}
22+
if array[i] + array[j] == target {
23+
return []int{array[i], array[j]}
24+
}
25+
}
26+
}
27+
28+
return []int{}
29+
}
30+
31+
func twoNumberSumSolutionTwo(array []int, target int) []int {
32+
nums := map[int]bool{}
33+
34+
for _, num := range array {
35+
checkValue := target - num
36+
if _, found := nums[checkValue]; found {
37+
return []int{checkValue, num}
38+
}
39+
nums[num] = true
40+
}
41+
return []int{}
42+
}

algoexpert/validSubsequence/main.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
arr := []int{5, 1, 22, 25, 6, -1, 8, 10}
7+
sequence := []int{1, 6, -1, 10}
8+
fmt.Println("Number Sum: O(n)", isValidSubsequence(arr, sequence))
9+
}
10+
11+
func isValidSubsequence(array []int, sequence []int) bool {
12+
// Write your code here.
13+
arrayIdx := 0
14+
sequenceIdx := 0
15+
16+
for arrayIdx < len(array) && sequenceIdx < len(sequence) {
17+
if array[arrayIdx] == sequence[sequenceIdx] {
18+
sequenceIdx ++
19+
}
20+
arrayIdx++
21+
}
22+
23+
if len(sequence) == sequenceIdx {
24+
return true
25+
} else {
26+
return false
27+
}
28+
}

go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module random-golang-algorithms
2+
3+
go 1.17

0 commit comments

Comments
 (0)