{"record":{"id":"288ceb88fe6018a3","repo":"TheAlgorithms/Go","slug":"target-not-found-in-array","errorCode":null,"errorMessage":"target not found in array","messagePattern":"target not found in array","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"search/errors.go","lineNumber":6,"sourceCode":"package search\n\nimport \"errors\"\n\n// ErrNotFound is returned by search functions when target is not found\nvar ErrNotFound = errors.New(\"target not found in array\")\n","sourceCodeStart":1,"sourceCodeEnd":7,"githubUrl":"https://github.com/TheAlgorithms/Go/blob/5ba447ec5ff3d1213de65b92e726ee74c5d5cc19/search/errors.go#L1-L7","documentation":"search.ErrNotFound (search/errors.go:6) is the sentinel error returned by search functions (Binary, BinaryIterative, LowerBound) and by math.FindKthMax/FindKthMin (via kthNumber) when the requested target or rank cannot be found in the slice. Callers get -1 as the value alongside this error. For the kth-number functions it fires when k is out of range: k < 0 or k >= len(nums) after index conversion.","triggerScenarios":"Calling search.Binary/BinaryIterative/LowerBound on a slice that does not contain the target (or is not sorted as required); calling math.FindKthMax(nums, k) with k < 1 or k > len(nums); calling FindKthMin with k < 1 or k > len(nums); passing an empty slice.","commonSituations":"Off-by-one in 1-based vs 0-based k (FindKthMax(nums, 0) or k = len(nums)); searching for a value absent from data; forgetting the slice must be sorted for binary search, so a present element is missed; empty slices from upstream queries.","solutions":["Validate k against 1 <= k <= len(nums) (and len(nums) > 0) before calling FindKthMax/FindKthMin","Ensure the slice is sorted ascending before calling binary-search functions","Always check err != nil and treat -1 as a sentinel value, not a valid result","Check membership/emptiness of the slice before searching for a specific target"],"exampleFix":"// before\nv, _ := math.FindKthMax(nums, k) // ErrNotFound swallowed, v == -1\n// after\nif k < 1 || k > len(nums) {\n    return 0, fmt.Errorf(\"k=%d out of range for %d elements\", k, len(nums))\n}\nv, err := math.FindKthMax(nums, k)\nif err != nil {\n    if errors.Is(err, search.ErrNotFound) { /* handle miss */ }\n}","handlingStrategy":"try-catch","validationCode":"if len(nums) == 0 || k < 1 || k > len(nums) {\n    return 0, fmt.Errorf(\"k=%d invalid for slice of len %d\", k, len(nums))\n}\n// for binary search: sort.Ints(nums) before calling","typeGuard":"func validRank(k, n int) bool { return n > 0 && k >= 1 && k <= n }","tryCatchPattern":"v, err := math.FindKthMax(nums, k)\nif err != nil {\n    if errors.Is(err, search.ErrNotFound) {\n        // fallback: return default or report k out of range\n    }\n    return 0, err\n}","preventionTips":["Clamp or validate k to the range [1, len(nums)] before calling","Sort the slice before binary search (Binary/BinaryIterative require sorted input)","Never treat the -1 return value as data; always check the error with errors.Is","Guard against empty slices before any search or kth-element call"],"tags":["go","search","not-found","invalid-input"],"backgroundTag":"target-not-found","analyzedSha":"5ba447ec5ff3d1213de65b92e726ee74c5d5cc19","analyzedAt":"2026-09-02T21:54:30.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}