Last active
October 7, 2021 16:45
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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