Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yogesh-desai/be923ff08d24adafc7f6e26697e81810 to your computer and use it in GitHub Desktop.
Save yogesh-desai/be923ff08d24adafc7f6e26697e81810 to your computer and use it in GitHub Desktop.
Write a function solution that, given an integer N, returns the maximum possible value obtained by inserting one '5' digit inside the decimal representation of integer N. if N= 5859 output= 589. if N= -5859 output= -859 If N = -5000 ouput =0
package main
import (
"fmt"
"strconv"
)
func main() {
fmt.Println(Solution(-5000))
fmt.Println(Solution(15958))
fmt.Println(Solution(-5859))
}
func Solution(N int) int {
var max int
var str = strconv.Itoa(N)
for i := 0; i < len(str); i++ {
if string(str[i]) == "5" {
var temp string
temp = str[0:i] + str[i+1:]
num, _ := strconv.Atoi(temp)
switch {
case N > 0: // For positive numbers
if num > max {
max = num
}
case N < 0: // For negative numbers
if num > N {
max = num
}
}
}
}
return max
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment