├── .gitignore ├── push.sh ├── practice └── algorithms │ ├── implementation │ ├── utopian-tree.go │ ├── bon-appetit.go │ ├── sherlock-and-squares.go │ ├── counting-valleys.go │ ├── chocolate-feast.go │ ├── service-lane.go │ ├── circular-array-rotation.go │ ├── repeated-strings.go │ ├── day-of-the-programmer.go │ ├── jumping-on-the-clouds.go │ ├── cut-the-sticks.go │ ├── drawing-book.go │ ├── sock-merchant.go │ ├── grading-students.go │ ├── migratory-birds.go │ ├── divisible-sum-pairs.go │ ├── breaking-the-records.go │ ├── apple-and-orange.go │ ├── birthday-chocolate.go │ ├── between-two-sets.go │ ├── find-digits.go │ └── angry-professor.go │ └── warmup │ ├── solve-me-first.go │ ├── simple-array-sum.go │ ├── a-very-big-sum.go │ ├── staircase.go │ ├── mini-max-sum.go │ ├── time-conversion.go │ ├── compare-the-triplets.go │ ├── birthday-cake-candles.go │ ├── plus-minus.go │ └── diagonal-difference.go ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Binaries for programs and plugins 2 | *.exe 3 | *.exe~ 4 | *.dll 5 | *.so 6 | *.dylib 7 | *.swp 8 | # Test binary, build with `go test -c` 9 | *.test 10 | 11 | # Output of the go coverage tool, specifically when used with LiteIDE 12 | *.out 13 | -------------------------------------------------------------------------------- /push.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | echo -e "\033[0;32mPushing Code to GitHub...\033[0m" 4 | 5 | 6 | # Add changes to git. 7 | git add . 8 | 9 | # Commit changes. 10 | msg="Hacker rank code update on `date`" 11 | if [ $# -eq 1 ] 12 | then msg="$1" 13 | fi 14 | git commit -m "$msg" 15 | 16 | # Push source and build repos. 17 | git push origin master 18 | 19 | -------------------------------------------------------------------------------- /practice/algorithms/implementation/utopian-tree.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | var t int 7 | fmt.Scan(&t) 8 | for t > 0 { 9 | h := 1 10 | var n int 11 | 12 | fmt.Scan(&n) 13 | for i := 1; i <= n; i++ { 14 | if i%2 == 0 { 15 | h++ 16 | } else { 17 | h = h * 2 18 | } 19 | } 20 | fmt.Println(h) 21 | t-- 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /practice/algorithms/implementation/bon-appetit.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | var n, k, t, sum, b int 7 | fmt.Scan(&n, &k) 8 | for i := 0; i < n; i++ { 9 | fmt.Scan(&t) 10 | if i != k { 11 | sum += t 12 | } 13 | } 14 | fmt.Scan(&b) 15 | if b == sum/2 { 16 | fmt.Println("Bon Appetit") 17 | } else { 18 | fmt.Println(b - (sum / 2)) 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /practice/algorithms/implementation/sherlock-and-squares.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "math" 6 | ) 7 | 8 | func main() { 9 | var n int 10 | fmt.Scan(&n) 11 | for n > 0 { 12 | var a, b int 13 | fmt.Scan(&a, &b) 14 | 15 | low := math.Ceil(math.Sqrt(float64(a))) 16 | high := math.Floor(math.Sqrt(float64(b))) 17 | fmt.Printf("%.0f\n", (high + 1 - low)) 18 | n-- 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /practice/algorithms/implementation/counting-valleys.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | var n int 7 | var s string 8 | 9 | fmt.Scan(&n, &s) 10 | 11 | depth := 0 12 | valley := 0 13 | for i := 0; i < n; i++ { 14 | if s[i] == 'U' { 15 | depth++ 16 | } 17 | if s[i] == 'D' { 18 | depth-- 19 | } 20 | 21 | if depth == 0 && s[i] == 'U' { 22 | valley++ 23 | } 24 | } 25 | fmt.Println(valley) 26 | } 27 | -------------------------------------------------------------------------------- /practice/algorithms/implementation/chocolate-feast.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | var t int 7 | fmt.Scan(&t) 8 | 9 | for t > 0 { 10 | var n, c, m int 11 | 12 | fmt.Scan(&n, &c, &m) 13 | 14 | wrappers := n / c 15 | 16 | ans := n / c 17 | 18 | for wrappers >= m { 19 | ans = ans + (wrappers / m) 20 | wrappers = (wrappers / m) + (wrappers % m) 21 | } 22 | fmt.Println(ans) 23 | t-- 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /practice/algorithms/implementation/service-lane.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | var n, t int 7 | fmt.Scan(&n, &t) 8 | 9 | w := make([]int, n) 10 | 11 | for i := 0; i < n; i++ { 12 | fmt.Scan(&w[i]) 13 | } 14 | 15 | for t > 0 { 16 | i, j := 0, 0 17 | fmt.Scan(&i, &j) 18 | min := w[i] 19 | for i <= j { 20 | if w[i] < min { 21 | min = w[i] 22 | } 23 | i++ 24 | } 25 | 26 | fmt.Println(min) 27 | t-- 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /practice/algorithms/implementation/circular-array-rotation.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | var n, k, q int 7 | fmt.Scan(&n, &k, &q) 8 | 9 | nums := make([]int, n) 10 | 11 | for i := 0; i < n; i++ { 12 | fmt.Scan(&nums[i]) 13 | } 14 | k = k % n 15 | for i := 0; i < q; i++ { 16 | final := 0 17 | fmt.Scan(&final) 18 | if final-k < 0 { 19 | fmt.Println(nums[final+n-k]) 20 | } else { 21 | fmt.Println(nums[final-k]) 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /practice/algorithms/implementation/repeated-strings.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | var ( 7 | n int 8 | s string 9 | ) 10 | fmt.Scan(&s, &n) 11 | var l int = len(s) 12 | 13 | times := n / l 14 | itr := n % l 15 | 16 | count := 0 17 | 18 | for i := 0; i < l; i++ { 19 | if s[i] == 'a' { 20 | count++ 21 | } 22 | } 23 | 24 | count = count * times 25 | 26 | for i := 0; i < itr; i++ { 27 | if s[i] == 'a' { 28 | count++ 29 | } 30 | } 31 | fmt.Println(count) 32 | } 33 | -------------------------------------------------------------------------------- /practice/algorithms/implementation/day-of-the-programmer.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | var year int 7 | fmt.Scan(&year) 8 | 9 | if year < 1918 { 10 | if year%4 == 0 { 11 | fmt.Printf("12.09.%v", year) 12 | } else { 13 | fmt.Printf("13.09.%v", year) 14 | } 15 | } else if year > 1918 { 16 | if year%400 == 0 || (year%4 == 0 && year%100 != 0) { 17 | fmt.Printf("12.09.%v", year) 18 | } else { 19 | fmt.Printf("13.09.%v", year) 20 | } 21 | } else { 22 | fmt.Printf("26.09.1918") 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /practice/algorithms/implementation/jumping-on-the-clouds.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | var n int 7 | fmt.Scan(&n) 8 | 9 | clouds := make([]int, n) 10 | for i := 0; i < n; i++ { 11 | fmt.Scan(&clouds[i]) 12 | } 13 | 14 | position, jumps := 0, 0 15 | for position < n-1 { 16 | if position != n-2 { 17 | if clouds[position+2] == 0 { 18 | position += 2 19 | } else { 20 | position++ 21 | } 22 | } else { 23 | position++ 24 | } 25 | jumps++ 26 | 27 | } 28 | fmt.Println(jumps) 29 | } 30 | -------------------------------------------------------------------------------- /practice/algorithms/warmup/solve-me-first.go: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author :- Rishabh Jain 4 | Solution for :- https://www.hackerrank.com/challenges/solve-me-first/problem 5 | blog for this code :- https://rishabh1403.com/posts/coding/hackerrank/2018/08/hackerrank-solve-me-first-solution/ 6 | video explanation for this code :- https://www.youtube.com/watch?v=yNzVtH2xsfQ 7 | 8 | */ 9 | 10 | package main 11 | 12 | import ( 13 | "fmt" 14 | ) 15 | 16 | func main() { 17 | var a, b, sum int 18 | fmt.Scanf("%d\n%d", &a, &b) 19 | //fmt.Scan(&a,&b) 20 | sum = a + b 21 | fmt.Println(sum) 22 | } 23 | -------------------------------------------------------------------------------- /practice/algorithms/warmup/simple-array-sum.go: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author :- Rishabh Jain 4 | Solution for :- https://www.hackerrank.com/challenges/simple-array-sum/problem 5 | blog for this code :-https://rishabh1403.com/posts/coding/hackerrank/2018/08/hackerrank-solution-of-simple-array-sum-in-golang/ 6 | video explanation for this code :- https://www.youtube.com/watch?v=lUftBPb2gx0 7 | 8 | */ 9 | 10 | package main 11 | 12 | import "fmt" 13 | 14 | func main() { 15 | var n, temp, sum int 16 | fmt.Scan(&n) 17 | for i := 0; i < n; i++ { 18 | fmt.Scan(&temp) 19 | sum += temp 20 | } 21 | fmt.Println(sum) 22 | } 23 | -------------------------------------------------------------------------------- /practice/algorithms/warmup/a-very-big-sum.go: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author :- Rishabh Jain 4 | Solution for :- https://www.hackerrank.com/challenges/a-very-big-sum/problem 5 | blog for this code :- https://rishabh1403.com/posts/coding/hackerrank/2018/08/hackerrank-solution-of-a-very-big-sum-in-golang/ 6 | video explanation for this code :- https://www.youtube.com/watch?v=LhgqF1vzkGc 7 | 8 | */ 9 | 10 | package main 11 | 12 | import "fmt" 13 | 14 | func main() { 15 | var n, temp, sum int64 16 | fmt.Scan(&n) 17 | for i := int64(0); i < n; i++ { 18 | fmt.Scan(&temp) 19 | sum += temp 20 | } 21 | fmt.Println(sum) 22 | } 23 | -------------------------------------------------------------------------------- /practice/algorithms/implementation/cut-the-sticks.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | var n int 7 | fmt.Scan(&n) 8 | nums := make([]int, n) 9 | for i := 0; i < n; i++ { 10 | fmt.Scan(&nums[i]) 11 | } 12 | 13 | for { 14 | min := 1001 15 | length := 0 16 | for i := 0; i < n; i++ { 17 | if nums[i] != 0 { 18 | 19 | length++ 20 | if nums[i] < min { 21 | min = nums[i] 22 | } 23 | } 24 | 25 | } 26 | for i := 0; i < n; i++ { 27 | if nums[i] != 0 { 28 | nums[i] = nums[i] - min 29 | } 30 | 31 | } 32 | if length == 0 { 33 | break 34 | } 35 | fmt.Println(length) 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /practice/algorithms/warmup/staircase.go: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author :- Rishabh Jain 4 | Solution for :- https://www.hackerrank.com/challenges/staircase/problem 5 | blog for this code :-https://rishabh1403.com/posts/coding/hackerrank/2018/09/hackerrank-solution-of-staircase-in-golang/ 6 | video explanation for this code :- https://www.youtube.com/watch?v=f9hHJSxUfX4 7 | 8 | */ 9 | 10 | package main 11 | 12 | import "fmt" 13 | 14 | func main() { 15 | var n int 16 | fmt.Scan(&n) 17 | 18 | for i := 0; i < n; i++ { 19 | for j := 0; j < n-i-1; j++ { 20 | fmt.Print(" ") 21 | } 22 | for j := 0; j <= i; j++ { 23 | fmt.Print("#") 24 | } 25 | fmt.Println() 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /practice/algorithms/warmup/mini-max-sum.go: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author :- Rishabh Jain 4 | Solution for :- https://www.hackerrank.com/challenges/mini-max-sum/problem 5 | blog for this code :-https://rishabh1403.com/posts/coding/hackerrank/2018/09/hackerrank-solution-of-mini-max-sum-in-golang/ 6 | video explanation for this code :- https://www.youtube.com/watch?v=-LlUwKzx-0k 7 | 8 | */ 9 | package main 10 | 11 | import ( 12 | "fmt" 13 | "sort" 14 | ) 15 | 16 | func main() { 17 | 18 | a := make([]int, 5) 19 | 20 | fmt.Scan(&a[0], &a[1], &a[2], &a[3], &a[4]) 21 | sort.Ints(a) 22 | min := a[0] + a[1] + a[2] + a[3] 23 | max := a[1] + a[2] + a[3] + a[4] 24 | fmt.Println(min, max) 25 | } 26 | -------------------------------------------------------------------------------- /practice/algorithms/implementation/drawing-book.go: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author :- Rishabh Jain 4 | Solution for :- https://www.hackerrank.com/challenges/drawing-book/problem 5 | blog for this code :-https://rishabh1403.com/posts/coding/hackerrank/2020/04/hackerrank-solution-of-drawing-book-in-golang 6 | Youtube video :- https://youtu.be/yTbd7vRDF4Y 7 | 8 | */ 9 | 10 | package main 11 | 12 | import "fmt" 13 | 14 | func main() { 15 | var n, p int 16 | fmt.Scan(&n, &p) 17 | front := p / 2 18 | 19 | back := (n - p) / 2 20 | 21 | if n%2 == 0 { 22 | back = (n + 1 - p) / 2 23 | } 24 | 25 | if front < back { 26 | fmt.Println(front) 27 | } else { 28 | fmt.Println(back) 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /practice/algorithms/warmup/time-conversion.go: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author :- Rishabh Jain 4 | Solution for :- https://www.hackerrank.com/challenges/time-conversion/problem 5 | blog for this code :-https://rishabh1403.com/posts/coding/hackerrank/2018/09/hackerrank-solution-of-time-conversion-in-golang/ 6 | video explanation for this code :- https://www.youtube.com/watch?v=jX8hNDV7P9o 7 | 8 | */ 9 | package main 10 | 11 | import "fmt" 12 | 13 | func main() { 14 | var h, m, s int 15 | var suffix string 16 | fmt.Scanf("%d:%d:%d%s", &h, &m, &s, &suffix) 17 | if suffix == "AM" && h == 12 { 18 | h = 0 19 | } 20 | if suffix == "PM" && h != 12 { 21 | h += 12 22 | } 23 | fmt.Printf("%02d:%02d:%02d", h, m, s) 24 | } 25 | -------------------------------------------------------------------------------- /practice/algorithms/implementation/sock-merchant.go: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author :- Rishabh Jain 4 | Solution for :- https://www.hackerrank.com/challenges/sock-merchant/problem 5 | blog for this code :-https://rishabh1403.com/posts/coding/hackerrank/2020/04/hackerrank-solution-of-sock-merchant-in-golang 6 | Youtube video :- https://youtu.be/9s2ZlFjX-yw 7 | 8 | */ 9 | 10 | package main 11 | 12 | import "fmt" 13 | 14 | func main() { 15 | var n int 16 | fmt.Scan(&n) 17 | 18 | socks := make([]int, 101) 19 | 20 | for i := 0; i < n; i++ { 21 | var t int 22 | fmt.Scan(&t) 23 | 24 | socks[t]++ 25 | } 26 | 27 | count := 0 28 | 29 | for _, v := range socks { 30 | count += v / 2 31 | } 32 | 33 | fmt.Println(count) 34 | } 35 | -------------------------------------------------------------------------------- /practice/algorithms/implementation/grading-students.go: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author :- Rishabh Jain 4 | Solution for :- https://www.hackerrank.com/challenges/grading/problem 5 | blog for this code :-https://rishabh1403.com/posts/coding/hackerrank/2018/09/hackerrank-solution-of-grading-students-in-golang/ 6 | video explanation for this code :- https://www.youtube.com/watch?v=Dk7QrcfYBjE 7 | 8 | */ 9 | package main 10 | 11 | import ( 12 | "fmt" 13 | ) 14 | 15 | func main() { 16 | 17 | var n, t int 18 | fmt.Scan(&n) 19 | 20 | for i := 0; i < n; i++ { 21 | fmt.Scan(&t) 22 | if t < 38 { 23 | fmt.Println(t) 24 | } else { 25 | if t%5 > 2 { 26 | fmt.Println(((t / 5) + 1) * 5) 27 | } else { 28 | fmt.Println(t) 29 | } 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /practice/algorithms/warmup/compare-the-triplets.go: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author :- Rishabh Jain 4 | Solution for :- https://www.hackerrank.com/challenges/compare-the-triplets/problem 5 | blog for this code :- https://rishabh1403.com/posts/coding/hackerrank/2018/08/hackerrank-solution-of-compare-the-triplets-in-golang/ 6 | video explanation for this code :- https://www.youtube.com/watch?v=4y8ceyunvOQ 7 | 8 | */ 9 | 10 | package main 11 | 12 | import "fmt" 13 | 14 | func main() { 15 | var a [3]int 16 | var b [3]int 17 | fmt.Scan(&a[0], &a[1], &a[2]) 18 | fmt.Scan(&b[0], &b[1], &b[2]) 19 | var ar, br int 20 | for i := 0; i < 3; i++ { 21 | //compare here 22 | if a[i] > b[i] { 23 | ar++ 24 | } else if b[i] > a[i] { 25 | br++ 26 | } 27 | } 28 | fmt.Println(ar, br) 29 | } 30 | -------------------------------------------------------------------------------- /practice/algorithms/implementation/migratory-birds.go: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author :- Rishabh Jain 4 | Solution for :- https://www.hackerrank.com/challenges/migratory-birds/problem 5 | blog for this code :-https://rishabh1403.com/posts/coding/hackerrank/2018/10/hackerrank-solution-of-migratory-birds-in-golang/ 6 | video explanation for this code :- https://www.youtube.com/watch?v=IoX_OBCVIOw 7 | 8 | */ 9 | package main 10 | 11 | import "fmt" 12 | 13 | func main() { 14 | var n, t int 15 | 16 | fmt.Scan(&n) 17 | 18 | a := make([]int, 5) 19 | 20 | for i := 0; i < n; i++ { 21 | fmt.Scan(&t) 22 | t = t - 1 23 | a[t]++ 24 | } 25 | max, occ := a[0], 0 26 | 27 | for i, num := range a { 28 | if num > max { 29 | max = num 30 | occ = i + 1 31 | } 32 | } 33 | fmt.Println(occ) 34 | } 35 | -------------------------------------------------------------------------------- /practice/algorithms/implementation/divisible-sum-pairs.go: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author :- Rishabh Jain 4 | Solution for :- https://www.hackerrank.com/challenges/divisible-sum-pairs/problem 5 | blog for this code :-https://rishabh1403.com/posts/coding/hackerrank/2018/10/hackerrank-solution-of-divisible-sum-pairs-in-golang/ 6 | video explanation for this code :- https://www.youtube.com/watch?v=UgTCStBlcj4 7 | 8 | */ 9 | package main 10 | 11 | import "fmt" 12 | 13 | func main() { 14 | var n, k int 15 | fmt.Scan(&n, &k) 16 | 17 | a := make([]int, n) 18 | 19 | for i := 0; i < n; i++ { 20 | fmt.Scan(&a[i]) 21 | } 22 | 23 | c := 0 24 | 25 | for i := 0; i < n; i++ { 26 | for j := i + 1; j < n; j++ { 27 | if (a[i]+a[j])%k == 0 { 28 | c++ 29 | } 30 | } 31 | } 32 | fmt.Println(c) 33 | } 34 | -------------------------------------------------------------------------------- /practice/algorithms/warmup/birthday-cake-candles.go: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author :- Rishabh Jain 4 | Solution for :- https://www.hackerrank.com/challenges/birthday-cake-candles/problem 5 | blog for this code :- https://rishabh1403.com/posts/coding/hackerrank/2018/09/hackerrank-solution-of-birthday-cake-candles-in-golang/ 6 | video explanation for this code :- https://www.youtube.com/watch?v=3iT1akTZzqU 7 | 8 | */ 9 | 10 | package main 11 | 12 | import "fmt" 13 | 14 | func main() { 15 | var n, t, max, occ int 16 | fmt.Scan(&n) 17 | 18 | a := []int{} 19 | for i := 0; i < n; i++ { 20 | fmt.Scan(&t) 21 | a = append(a, t) 22 | if t > max { 23 | max = t 24 | } 25 | } 26 | 27 | for _, num := range a { 28 | if num == max { 29 | occ++ 30 | } 31 | } 32 | 33 | fmt.Println(occ) 34 | 35 | } 36 | -------------------------------------------------------------------------------- /practice/algorithms/warmup/plus-minus.go: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author :- Rishabh Jain 4 | Solution for :- https://www.hackerrank.com/challenges/plus-minus/problem 5 | blog for this code :- https://rishabh1403.com/posts/coding/hackerrank/2018/08/hackerrank-solution-of-plus-minus-in-golang/ 6 | video explanation for this code :- https://www.youtube.com/watch?v=7996I_bqVmk 7 | 8 | */ 9 | 10 | package main 11 | 12 | import "fmt" 13 | 14 | func main() { 15 | var l, p, n, z, t int 16 | fmt.Scan(&l) 17 | 18 | for i := 0; i < l; i++ { 19 | fmt.Scan(&t) 20 | if t > 0 { 21 | p++ 22 | } else if t < 0 { 23 | n++ 24 | } else { 25 | z++ 26 | } 27 | } 28 | 29 | pf := float64(p) / float64(l) 30 | nf := float64(n) / float64(l) 31 | zf := float64(z) / float64(l) 32 | 33 | fmt.Printf("%.6f\n", pf) 34 | fmt.Printf("%.6f\n", nf) 35 | fmt.Printf("%.6f\n", zf) 36 | 37 | } 38 | -------------------------------------------------------------------------------- /practice/algorithms/implementation/breaking-the-records.go: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author :- Rishabh Jain 4 | Solution for :- https://www.hackerrank.com/challenges/breaking-best-and-worst-records/problem 5 | blog for this code :-https://rishabh1403.com/posts/coding/hackerrank/2018/10/hackerrank-solution-of-breaking-the-records-in-golang/ 6 | video explanation for this code :- https://www.youtube.com/watch?v=7RKyPKN1rP0 7 | 8 | */ 9 | 10 | package main 11 | 12 | import ( 13 | "fmt" 14 | ) 15 | 16 | func main() { 17 | var n, min, max, cmin, cmax int 18 | fmt.Scan(&n) 19 | 20 | for i := 0; i < n; i++ { 21 | var t int 22 | fmt.Scan(&t) 23 | if i == 0 { 24 | min = t 25 | max = t 26 | } else { 27 | if t < min { 28 | cmin++ 29 | min = t 30 | } 31 | if t > max { 32 | cmax++ 33 | max = t 34 | } 35 | } 36 | } 37 | fmt.Println(cmax, cmin) 38 | } 39 | -------------------------------------------------------------------------------- /practice/algorithms/implementation/apple-and-orange.go: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author :- Rishabh Jain 4 | Solution for :- https://www.hackerrank.com/challenges/apple-and-orange/problem 5 | blog for this code :-https://rishabh1403.com/posts/coding/hackerrank/2018/09/hackerrank-solution-of-apple-and-orange-in-golang/ 6 | video explanation for this code :- https://www.youtube.com/watch?v=UJBdyCUR4h8 7 | 8 | */ 9 | package main 10 | 11 | import ( 12 | "fmt" 13 | ) 14 | 15 | func main() { 16 | 17 | var s, t, a, b, m, n int 18 | fmt.Scan(&s, &t) 19 | fmt.Scan(&a, &b) 20 | fmt.Scan(&m, &n) 21 | var ac, bc int 22 | for i := 0; i < m; i++ { 23 | var temp int 24 | fmt.Scan(&temp) 25 | if a+temp >= s && a+temp <= t { 26 | ac++ 27 | } 28 | } 29 | for i := 0; i < n; i++ { 30 | var temp int 31 | fmt.Scan(&temp) 32 | if b+temp >= s && b+temp <= t { 33 | bc++ 34 | } 35 | } 36 | fmt.Printf("%d\n%d", ac, bc) 37 | } 38 | -------------------------------------------------------------------------------- /practice/algorithms/warmup/diagonal-difference.go: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author :- Rishabh Jain 4 | Solution for :- https://www.hackerrank.com/challenges/diagonal-difference/problem 5 | blog for this code :- https://rishabh1403.com/posts/coding/hackerrank/2018/08/hackerrank-solution-of-diagonal-difference-in-golang/ 6 | video explanation for this code :- https://www.youtube.com/watch?v=4b0RpyRfKY8 7 | 8 | */ 9 | 10 | package main 11 | 12 | import ( 13 | "fmt" 14 | "math" 15 | ) 16 | 17 | func main() { 18 | 19 | var n int 20 | fmt.Scan(&n) 21 | 22 | a := make([][]int, n) 23 | 24 | lsum, rsum := 0, 0 25 | 26 | for i := 0; i < n; i++ { 27 | a[i] = make([]int, n) 28 | for j := 0; j < n; j++ { 29 | fmt.Scan(&a[i][j]) 30 | if i == j { 31 | lsum += a[i][j] 32 | } 33 | if i+j == n-1 { 34 | rsum += a[i][j] 35 | } 36 | } 37 | } 38 | 39 | diff := math.Abs(float64(lsum - rsum)) 40 | fmt.Println(diff) 41 | 42 | } 43 | -------------------------------------------------------------------------------- /practice/algorithms/implementation/birthday-chocolate.go: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author :- Rishabh Jain 4 | Solution for :- https://www.hackerrank.com/challenges/the-birthday-bar/problem 5 | blog for this code :-https://rishabh1403.com/posts/coding/hackerrank/2018/10/hackerrank-solution-of-birthday-chocolate-in-golang/ 6 | video explanation for this code :- https://www.youtube.com/watch?v=W55rjPR7TtM 7 | 8 | */ 9 | 10 | package main 11 | 12 | import ( 13 | "fmt" 14 | ) 15 | 16 | func main() { 17 | var n, d, m, c int 18 | fmt.Scan(&n) 19 | a := make([]int, n) 20 | 21 | for i := 0; i < n; i++ { 22 | fmt.Scan(&a[i]) 23 | } 24 | fmt.Scan(&d, &m) 25 | 26 | for i := 0; i < n; i++ { 27 | length, sum := 0, 0 28 | for j := i; j < n; j++ { 29 | length++ 30 | sum += a[j] 31 | if sum == d && length == m { 32 | c++ 33 | break 34 | } 35 | if sum > d || length > m { 36 | break 37 | } 38 | } 39 | } 40 | fmt.Println(c) 41 | 42 | } 43 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Rishabh Jain 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /practice/algorithms/implementation/between-two-sets.go: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author :- Rishabh Jain 4 | Solution for :- https://www.hackerrank.com/challenges/between-two-sets/problem 5 | blog for this code :-https://rishabh1403.com/posts/coding/hackerrank/2018/09/hackerrank-solution-of-between-two-sets-in-golang/ 6 | video explanation for this code :- https://www.youtube.com/watch?v=LJ6nUUZpBiE 7 | 8 | */ 9 | package main 10 | 11 | import ( 12 | "fmt" 13 | ) 14 | 15 | func main() { 16 | 17 | var n, m int 18 | fmt.Scan(&n, &m) 19 | 20 | a := make([]int, n) 21 | b := make([]int, m) 22 | 23 | for i := 0; i < n; i++ { 24 | fmt.Scan(&a[i]) 25 | } 26 | for j := 0; j < m; j++ { 27 | fmt.Scan(&b[j]) 28 | } 29 | 30 | var c int 31 | 32 | for i := 1; i <= 100; i++ { 33 | factor := true 34 | for j := 0; j < n; j++ { 35 | if i%a[j] != 0 { 36 | factor = false 37 | break 38 | } 39 | } 40 | if factor { 41 | for j := 0; j < m; j++ { 42 | if b[j]%i != 0 { 43 | factor = false 44 | break 45 | } 46 | } 47 | } 48 | if factor { 49 | c++ 50 | } 51 | } 52 | fmt.Println(c) 53 | } 54 | -------------------------------------------------------------------------------- /practice/algorithms/implementation/find-digits.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "io" 7 | "os" 8 | "strconv" 9 | "strings" 10 | ) 11 | 12 | // Complete the findDigits function below. 13 | func findDigits(n int32) int32 { 14 | num := n 15 | var count int32 = 0 16 | for n > 0 { 17 | 18 | if digit := n % 10; digit != 0 && num%digit == 0 { 19 | count++ 20 | } 21 | n = n / 10 22 | } 23 | 24 | return count 25 | 26 | } 27 | 28 | func main() { 29 | reader := bufio.NewReaderSize(os.Stdin, 1024*1024) 30 | 31 | stdout, err := os.Create(os.Getenv("OUTPUT_PATH")) 32 | checkError(err) 33 | 34 | defer stdout.Close() 35 | 36 | writer := bufio.NewWriterSize(stdout, 1024*1024) 37 | 38 | tTemp, err := strconv.ParseInt(readLine(reader), 10, 64) 39 | checkError(err) 40 | t := int32(tTemp) 41 | 42 | for tItr := 0; tItr < int(t); tItr++ { 43 | nTemp, err := strconv.ParseInt(readLine(reader), 10, 64) 44 | checkError(err) 45 | n := int32(nTemp) 46 | 47 | result := findDigits(n) 48 | 49 | fmt.Fprintf(writer, "%d\n", result) 50 | } 51 | 52 | writer.Flush() 53 | } 54 | 55 | func readLine(reader *bufio.Reader) string { 56 | str, _, err := reader.ReadLine() 57 | if err == io.EOF { 58 | return "" 59 | } 60 | 61 | return strings.TrimRight(string(str), "\r\n") 62 | } 63 | 64 | func checkError(err error) { 65 | if err != nil { 66 | panic(err) 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /practice/algorithms/implementation/angry-professor.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "io" 7 | "os" 8 | "strconv" 9 | "strings" 10 | ) 11 | 12 | // Complete the angryProfessor function below. 13 | func angryProfessor(k int32, a []int32) string { 14 | var count int32 = 0 15 | 16 | for _, v := range a { 17 | if v <= 0 { 18 | count++ 19 | } 20 | } 21 | if count < k { 22 | return "YES" 23 | } 24 | 25 | return "NO" 26 | } 27 | 28 | func main() { 29 | reader := bufio.NewReaderSize(os.Stdin, 1024*1024) 30 | 31 | stdout, err := os.Create(os.Getenv("OUTPUT_PATH")) 32 | checkError(err) 33 | 34 | defer stdout.Close() 35 | 36 | writer := bufio.NewWriterSize(stdout, 1024*1024) 37 | 38 | tTemp, err := strconv.ParseInt(readLine(reader), 10, 64) 39 | checkError(err) 40 | t := int32(tTemp) 41 | 42 | for tItr := 0; tItr < int(t); tItr++ { 43 | nk := strings.Split(readLine(reader), " ") 44 | 45 | nTemp, err := strconv.ParseInt(nk[0], 10, 64) 46 | checkError(err) 47 | n := int32(nTemp) 48 | 49 | kTemp, err := strconv.ParseInt(nk[1], 10, 64) 50 | checkError(err) 51 | k := int32(kTemp) 52 | 53 | aTemp := strings.Split(readLine(reader), " ") 54 | 55 | var a []int32 56 | 57 | for i := 0; i < int(n); i++ { 58 | aItemTemp, err := strconv.ParseInt(aTemp[i], 10, 64) 59 | checkError(err) 60 | aItem := int32(aItemTemp) 61 | a = append(a, aItem) 62 | } 63 | 64 | result := angryProfessor(k, a) 65 | 66 | fmt.Fprintf(writer, "%s\n", result) 67 | } 68 | 69 | writer.Flush() 70 | } 71 | 72 | func readLine(reader *bufio.Reader) string { 73 | str, _, err := reader.ReadLine() 74 | if err == io.EOF { 75 | return "" 76 | } 77 | 78 | return strings.TrimRight(string(str), "\r\n") 79 | } 80 | 81 | func checkError(err error) { 82 | if err != nil { 83 | panic(err) 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Hackerrank Solutions in Golang 2 | 3 | [![Maintenance](https://img.shields.io/badge/Maintained%3F-yes-green.svg)](https://github.com/rishabh1403/hackerrank-golang-solutions/graphs/commit-activity) [![made-with-Go](https://img.shields.io/badge/Made%20with-Go-blue.svg)](https://golang.org/) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com) [![Open Source Love](https://badges.frapsoft.com/os/v1/open-source.svg?v=103)](https://github.com/ellerbrock/open-source-badges/) [![MIT Licence](https://badges.frapsoft.com/os/mit/mit.svg?v=103)](https://opensource.org/licenses/mit-license.php) [![HitCount](http://hits.dwyl.io/rishabh1403/hackerrank-golang-solutions.svg)](http://hits.dwyl.io/rishabh1403/hackerrank-golang-solutions) 4 | 5 | Solutions of all the questions from Hackerrank in Golang. 6 | 7 | ***This is an on-going series where I post solutions of questions on hackerrank with explanations on my [blog](https://rishabh1403.com) and each blog post is accompanied by a video on my [YouTube](https://www.youtube.com/channel/UC4syrEYE9_fzeVBajZIyHlA) channel. Use the links below for the question statement, complete source code, blog post, and video for that particular question.*** 8 | 9 | # Practice 10 | 11 | ## Algorithms 12 | 13 | ### Warmup 14 | 15 | * Solve me First - [Question](https://www.hackerrank.com/challenges/solve-me-first/problem) | [Source Code](https://github.com/rishabh1403/hackerrank-golang-solutions/blob/master/practice/algorithms/warmup/solve-me-first.go) | [Blog](https://rishabh1403.com/posts/coding/hackerrank/2018/08/hackerrank-solve-me-first-solution/) | [Youtube Video](https://www.youtube.com/watch?v=yNzVtH2xsfQ 16 | ) 17 | * Simple Array Sum - [Question](https://www.hackerrank.com/challenges/simple-array-sum/problem) | [Source Code](https://github.com/rishabh1403/hackerrank-golang-solutions/blob/master/practice/algorithms/warmup/simple-array-sum.go) | [Blog](https://rishabh1403.com/posts/coding/hackerrank/2018/08/hackerrank-solution-of-simple-array-sum-in-golang/) | [Youtube Video](https://www.youtube.com/watch?v=lUftBPb2gx0) 18 | * Compare the Triplets - [Question](https://www.hackerrank.com/challenges/compare-the-triplets/problem) | [Source Code](https://github.com/rishabh1403/hackerrank-golang-solutions/blob/master/practice/algorithms/warmup/compare-the-triplets.go) | [Blog](https://rishabh1403.com/posts/coding/hackerrank/2018/08/hackerrank-solution-of-compare-the-triplets-in-golang/) | [Youtube Video](https://www.youtube.com/watch?v=4y8ceyunvOQ) 19 | * A Very Big Sum - [Question](https://www.hackerrank.com/challenges/a-very-big-sum/problem) | [Source Code](https://github.com/rishabh1403/hackerrank-golang-solutions/blob/master/practice/algorithms/warmup/a-very-big-sum.go) | [Blog](https://rishabh1403.com/posts/coding/hackerrank/2018/08/hackerrank-solution-of-a-very-big-sum-in-golang/) | [Youtube Video](https://www.youtube.com/watch?v=LhgqF1vzkGc) 20 | * Diagonal Difference - [Question](https://www.hackerrank.com/challenges/diagonal-difference/problem) | [Source Code](https://github.com/rishabh1403/hackerrank-golang-solutions/blob/master/practice/algorithms/warmup/diagonal-difference.go) | [Blog](https://rishabh1403.com/posts/coding/hackerrank/2018/08/hackerrank-solution-of-diagonal-difference-in-golang/) | [Youtube Video](https://www.youtube.com/watch?v=4b0RpyRfKY8) 21 | * Plus Minus - [Question](https://www.hackerrank.com/challenges/plus-minus/problem) | [Source Code](https://github.com/rishabh1403/hackerrank-golang-solutions/blob/master/practice/algorithms/warmup/plus-minus.go) | [Blog](https://rishabh1403.com/posts/coding/hackerrank/2018/08/hackerrank-solution-of-plus-minus-in-golang/) | [Youtube Video](https://www.youtube.com/watch?v=7996I_bqVmk) 22 | * Staircase - [Question](https://www.hackerrank.com/challenges/staircase/problem) | [Source Code](https://github.com/rishabh1403/hackerrank-golang-solutions/blob/master/practice/algorithms/warmup/staircase.go) | [Blog](https://rishabh1403.com/posts/coding/hackerrank/2018/09/hackerrank-solution-of-staircase-in-golang/) | [Youtube Video](https://www.youtube.com/watch?v=f9hHJSxUfX4) 23 | * Mini-Max Sum - [Question](https://www.hackerrank.com/challenges/mini-max-sum/problem) | [Source Code](https://github.com/rishabh1403/hackerrank-golang-solutions/blob/master/practice/algorithms/warmup/mini-max-sum.go) | [Blog](https://rishabh1403.com/posts/coding/hackerrank/2018/09/hackerrank-solution-of-mini-max-sum-in-golang/) | [Youtube Video](https://www.youtube.com/watch?v=-LlUwKzx-0k) 24 | * Birthday Cake Candles - [Question](https://www.hackerrank.com/challenges/birthday-cake-candles/problem) | [Source Code](https://github.com/rishabh1403/hackerrank-golang-solutions/blob/master/practice/algorithms/warmup/birthday-cake-candles.go) | [Blog](https://rishabh1403.com/posts/coding/hackerrank/2018/09/hackerrank-solution-of-birthday-cake-candles-in-golang/) | [Youtube Video](https://www.youtube.com/watch?v=3iT1akTZzqU) 25 | * Time Conversion - [Question](https://www.hackerrank.com/challenges/time-conversion/problem) | [Source Code](https://github.com/rishabh1403/hackerrank-golang-solutions/blob/master/practice/algorithms/warmup/time-conversion.go) | [Blog](https://rishabh1403.com/posts/coding/hackerrank/2018/09/hackerrank-solution-of-time-conversion-in-golang/) | [Youtube Video](https://www.youtube.com/watch?v=jX8hNDV7P9o) 26 | 27 | ***This completes warmup section as of now. Will add new solutions as and when Hackerrank adds it. Please raise an issue if I get delayed in adding it.*** 28 | 29 | 30 | ### Implementation 31 | 32 | * Time Conversion - [Question](https://www.hackerrank.com/challenges/grading/problem) | [Source Code](https://github.com/rishabh1403/hackerrank-golang-solutions/blob/master/practice/algorithms/implementation/grading-students.go) | [Blog](https://rishabh1403.com/posts/coding/hackerrank/2018/09/hackerrank-solution-of-grading-students-in-golang/) | [Youtube Video](https://www.youtube.com/watch?v=Dk7QrcfYBjE) 33 | * Apple and Orange - [Question](https://www.hackerrank.com/challenges/apple-and-orange/problem) | [Source Code](https://github.com/rishabh1403/hackerrank-golang-solutions/blob/master/practice/algorithms/implementation/apple-and-orange.go) | [Blog](https://rishabh1403.com/posts/coding/hackerrank/2018/09/hackerrank-solution-of-apple-and-orange-in-golang/) | [Youtube Video](https://www.youtube.com/watch?v=UJBdyCUR4h8) 34 | * Between Two Sets - [Question](https://www.hackerrank.com/challenges/between-two-sets/problem) | [Source Code](https://github.com/rishabh1403/hackerrank-golang-solutions/blob/master/practice/algorithms/implementation/between-two-sets.go) | [Blog](https://rishabh1403.com/posts/coding/hackerrank/2018/09/hackerrank-solution-of-between-two-sets-in-golang/) | [Youtube Video](https://www.youtube.com/watch?v=LJ6nUUZpBiE) 35 | * Breaking The Records - [Question](https://www.hackerrank.com/challenges/breaking-best-and-worst-records/problem) | [Source Code](https://github.com/rishabh1403/hackerrank-golang-solutions/blob/master/practice/algorithms/implementation/breaking-the-records.go) | [Blog](https://rishabh1403.com/posts/coding/hackerrank/2018/10/hackerrank-solution-of-breaking-the-records-in-golang/) | [Youtube Video](https://www.youtube.com/watch?v=7RKyPKN1rP0) 36 | * Birthday Chocolate - [Question](https://www.hackerrank.com/challenges/the-birthday-bar/problem) | [Source Code](https://github.com/rishabh1403/hackerrank-golang-solutions/blob/master/practice/algorithms/implementation/birthday-chocolate.go) | [Blog](https://rishabh1403.com/posts/coding/hackerrank/2018/10/hackerrank-solution-of-birthday-chocolate-in-golang/) | [Youtube Video](https://www.youtube.com/watch?v=W55rjPR7TtM) 37 | * Divisible Sum Pairs - [Question](https://www.hackerrank.com/challenges/divisible-sum-pairs/problem) | [Source Code](https://github.com/rishabh1403/hackerrank-golang-solutions/blob/master/practice/algorithms/implementation/divisible-sum-pairs.go) | [Blog](https://rishabh1403.com/posts/coding/hackerrank/2018/10/hackerrank-solution-of-divisible-sum-pairs-in-golang/) | [Youtube Video](https://www.youtube.com/watch?v=UgTCStBlcj4) 38 | * Migratory Birds - [Question](https://www.hackerrank.com/challenges/migratory-birds/problem) | [Source Code](https://github.com/rishabh1403/hackerrank-golang-solutions/blob/master/practice/algorithms/implementation/migratory-birds.go) | [Blog](https://rishabh1403.com/posts/coding/hackerrank/2018/10/hackerrank-solution-of-migratory-birds-in-golang/) | [Youtube Video](https://www.youtube.com/watch?v=IoX_OBCVIOw) 39 | * Sock Merchant - [Question](https://www.hackerrank.com/challenges/sock-merchant/problem) | [Source Code](https://github.com/rishabh1403/hackerrank-golang-solutions/blob/master/practice/algorithms/implementation/sock-merchant.go) | [Blog](https://rishabh1403.com/posts/coding/hackerrank/2020/04/hackerrank-solution-of-sock-merchant-in-golang) | [Youtube Video](https://youtu.be/9s2ZlFjX-yw) 40 | * Drawing Book - [Question](https://www.hackerrank.com/challenges/drawing-book/problem) | [Source Code](https://github.com/rishabh1403/hackerrank-golang-solutions/blob/master/practice/algorithms/implementation/drawing-book.go) | [Blog](https://rishabh1403.com/posts/coding/hackerrank/2020/04/hackerrank-solution-of-drawing-book-in-golang) | [Youtube Video](https://youtu.be/yTbd7vRDF4Y) 41 | 42 | ***Will keep adding more links here as the series progresses, generally I post 3 updates every week.*** 43 | 44 | --------------------------------------------------------------------------------