Last active
March 4, 2024 06:26
-
-
Save alisalehi1380/395d44ed6ba45b2f2b6cdc134c0f5e48 to your computer and use it in GitHub Desktop.
find-pair-numbers-golang
This file contains 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 ( | |
"bufio" | |
"fmt" | |
"os" | |
"strconv" | |
"strings" | |
) | |
func findPair(arr []int, m int) (int, int) { | |
for i := 0; i < len(arr); i++ { | |
for j := i + 1; j < len(arr); j++ { | |
if arr[i]+arr[j] > m { | |
return arr[i], arr[j] | |
} | |
} | |
} | |
return -1, -1 | |
} | |
func main() { | |
// get array numbers from user | |
fmt.Print("Enter an array of numbers (comma-separated): ") | |
scanner := bufio.NewScanner(os.Stdin) | |
scanner.Scan() | |
inputArray := scanner.Text() | |
// convert string to number | |
stringNumbers := strings.Split(inputArray, ",") | |
var numbers []int | |
for _, strNum := range stringNumbers { | |
num, err := strconv.Atoi(strings.TrimSpace(strNum)) | |
if err != nil { | |
fmt.Println("Invalid input. Please enter valid numbers.") | |
return | |
} | |
numbers = append(numbers, num) | |
} | |
// get m number from user | |
fmt.Print("Enter a number (m): ") | |
var m int | |
_, err := fmt.Scan(&m) | |
if err != nil { | |
fmt.Println("Invalid input. Please enter a valid number for m.") | |
return | |
} | |
// find pair numbers | |
resultA, resultB := findPair(numbers, m) | |
// show results | |
fmt.Printf("Pair found: (%d, %d)\n", resultA, resultB) | |
} | |
// for more information please go to "https://github.com/alisalehi1380/find-pair-numbers-golang" repo. tnks | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment