Learning course: Arrays using Go
Problem Link: CodeChef: Practical coding for everyone
Feedback
package main
import “fmt”
func main() {
var t int
fmt.Scanf(“%d”, &t)
for i := 0; i < t; i++ {
var N int
fmt.Scanf(“%d”, &N)
arr := make([]int, N)
for j := 0; j < N; j++ {
fmt.Scanf("%d", &arr[j])
}
brr := make([]int, N)
for j := 0; j < N; j++ {
fmt.Scanf("%d", &brr[j])
}
dailystreakOm := 0
dailystreakAddy := 0
for j := 0; j < N; j++ {
if arr[j] == 0 {
dailystreakOm = 0
} else {
dailystreakOm++
}
if brr[j] == 0 {
dailystreakAddy = 0
} else {
dailystreakAddy++
}
}
if dailystreakOm > dailystreakAddy {
fmt.Println("Om")
} else if dailystreakOm < dailystreakAddy{
fmt.Println("Addy")
} else {
if dailystreakOm == 0 && dailystreakAddy == 0 {
fmt.Println("Draw")
} else {
fmt.Println("Draw")
}
}
}
}
please elaborate me what might be wrong with this code
@sdas_321
plzz refer the following code for better understanding .
package main
import (
"bufio"
"fmt"
"os"
)
import (
"strconv"
"strings"
)
func readNumbers(s string) []int {
s = strings.TrimSpace(s)
strs := strings.Split(s, " ")
numbers := []int{}
for _, str := range strs {
n, _ := strconv.Atoi(str)
numbers = append(numbers, n)
}
return numbers
}
func main() {
reader := bufio.NewReader(os.Stdin)
// writer := bufio.NewWriter(os.Stdout)
var count int
fmt.Fscanln(reader, &count)
//count = 1
for t := 0; t < count; t++ {
var n int
fmt.Fscanln(reader, &n)
s1, _ := reader.ReadString('\n')
s2, _ := reader.ReadString('\n')
// s = strings.TrimSpace(s)
a := readNumbers(s1)
b := readNumbers(s2)
c1, c2 := findMax(a), findMax(b)
if c1 > c2 {
fmt.Println("Om")
} else if c1 < c2 {
fmt.Println("Addy")
} else {
fmt.Println("Draw")
}
}
}
func findMax(a []int) int {
c, c1 := 0, 0
for _, v := range a {
if v > 0 {
c++
} else {
c = 0
}
if c > c1 {
c1 = c
}
}
return c1
}
1 Like
fix the fmt.Scanf
statements by using double quotes ("
) instead of angled quotes (“
and ”
).
also update the logic for calculating the streaks of non-zero elements in the arrays. ThemaxStreakOm
and maxStreakAddy
variables now keep track of the maximum streak lengths for each person (Om and Addy).
The winner is determined based on the maximum streaks, not just the last element
1 Like