I am working on printing the index of the numbers that leads to the sum that is entered through input by user. I have basically used the tradition method of using two loops of i & j and iterate until array length. However, when it comes to Go Language, we do have an option of getting index & key value of array using a different format in Go.
Here's my working code:
func findKIndex(arr []int, k int) (int, int) {
index1, index2 := 0, 0
Length := len(arr)
for i := 0; i < Length; i++ {
for j := i + 1; j < Length; j++ {
if arr[i]+arr[j] == k {
index1 = i
index2 = j
}
}
}
return index1, index2
}
How do I do the same thing using :
for idx, key := range arr{
for idx2, key2 := range arr {
//statements
}
}
Basically, I am not able to figure out initiating the inner index with +1 of outer index or may do it in a single loop.