Find missing number in a given integer array of 1 to 10 in Golang

Cooking

for this first  we need to calculate the sum of n natural numbers as :  sum= n*(n+1)/2, then subtract the sum of given numbers

package main

import (

“fmt”

)

func main() {

a := []int{1,2, 3, 5, 6,7,8,9,10}

length := len(a)

       /*** here length of array is n-1

sum :=0 

totalSum := ((length + 1)*(length + 2))/2

fmt.Println(totalSum)

for _, val:= range a {

sum = sum + val

}

fmt.Println(sum)

missing := totalSum – sum

fmt.Print(missing)

}


Leave a Reply

Your email address will not be published. Required fields are marked *