Table of Contents
Recursion
package main
import (
"fmt"
)
func printNameNTimes(name string, i int, N int) {
if i > N {
return
}
fmt.Printf("%s\n", name)
printNameNTimes(name, i+1, N)
}
func printNIncreasingOrder(i int, N int) {
if i > N {
return
}
fmt.Printf("%d\n", i)
printNIncreasingOrder(i+1, N)
}
func printNDescreasingOrder(i int) {
if i < 1 {
return
}
fmt.Printf("%d\n", i)
printNDescreasingOrder(i - 1)
}
func main() {
//case-1 Print name n times
name := "robert"
n1 := 3
start1 := 1
fmt.Println("\nPrint name n times")
printNameNTimes(name, start1, n1)
// case-2 print i from 1 to N
n2 := 4
start2 := 1
fmt.Println("\nPrint N Increasing Order from 1 to N")
printNIncreasingOrder(start2, n2)
// case-3 print i from N to 1
n3 := 7
fmt.Println("\nPrint Descreasing Order from N to 1")
printNDescreasingOrder(n3)
}
Backtracking
SubSequence
Pick or Not pick technique
- Print all the subsequence
- [3, 2, 1] O/P : There will be 2n = 23 = 8 subsequence
- Print all Sq which sums to K
- [1, 2, 1] & sum = 2
- Print only 1st Sq which sums to K
- [1, 2, 1] & sum = 2 O/P : 1, 1
- Print the count of Sq which sums to K
- K [1, 2, 1] & sum = 2 O/P : 2
Visit https: https://codeandalgo.com for more such contents
Leave a Reply