mirror of
https://github.com/Dentrax/Data-Structures-with-Go
synced 2024-11-12 01:10:40 +00:00
added array-minimum-distance
This commit is contained in:
parent
999ef88bcc
commit
6d3c3b28a7
@ -1,2 +1 @@
|
||||
# Data-Structures-with-Go
|
||||
Data Structures with Go Language
|
||||
<h1 align="center">Data Structures wih Go Language Public Source Repository</h1>
|
||||
|
@ -0,0 +1,33 @@
|
||||
<h1 align="center">Array Minimum Distance Source</h1>
|
||||
|
||||
[What It Is](#what-it-is)
|
||||
|
||||
## What It Is
|
||||
|
||||
* Given an unsorted array `arr[]` and two numbers x and y, find the minimum distance between `x` and `y` in `arr[]`. The array might also contain duplicates. You may assume that both `x` and `y` are different and present in `arr[]`.
|
||||
|
||||
Examples
|
||||
--------------------------
|
||||
|
||||
> Input: arr[] = {1, 2}, x = 1, y = 2
|
||||
> Output: Minimum distance between 1 and 2 is 1.
|
||||
|
||||
> Input: arr[] = {3, 4, 5}, x = 3, y = 5
|
||||
> Output: Minimum distance between 3 and 5 is 2.
|
||||
|
||||
> Input: arr[] = {3, 5, 4, 2, 6, 5, 6, 6, 5, 4, 8, 3}, x = 3, y = 6
|
||||
> Output: Minimum distance between 3 and 6 is 4.
|
||||
|
||||
> Input: arr[] = {2, 5, 3, 5, 4, 4, 2, 3}, x = 3, y = 2
|
||||
> Output: Minimum distance between 3 and 2 is 1.
|
||||
|
||||
**METHOD 1 (Simple)**
|
||||
|
||||
Use two loops: The outer loop picks all the elements of arr[] one by one. The inner loop picks all the elements after the element picked by outer loop. If the elements picked by outer and inner loops have same values as x or y then if needed update the minimum distance calculated so far.
|
||||
|
||||
Algorithm Complexity
|
||||
--------------------------
|
||||
|
||||
| Complexity | Notation |
|
||||
| ----------------- |:---------:|
|
||||
| `Time Complexity` | `O(n^2)` |
|
@ -0,0 +1,49 @@
|
||||
// ====================================================
|
||||
// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal
|
||||
// This program comes with ABSOLUTELY NO WARRANTY; This is free software,
|
||||
// and you are welcome to redistribute it under certain conditions; See
|
||||
// file LICENSE, which is part of this source code package, for details.
|
||||
// ====================================================
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
const MaxUint = ^uint(0)
|
||||
const MinUint = 0
|
||||
const MaxInt = int(MaxUint >> 1)
|
||||
const MinInt = -MaxInt - 1
|
||||
|
||||
func Abs(x int) int {
|
||||
if x < 0 {
|
||||
return -x
|
||||
}
|
||||
if x == 0 {
|
||||
return 0
|
||||
}
|
||||
return x
|
||||
}
|
||||
|
||||
func minDist (arr []int, n, x, y int) int {
|
||||
var min_dist int = MaxInt
|
||||
|
||||
for i := 0; i < n; i++ {
|
||||
for j := i + 1; j < n; j++ {
|
||||
|
||||
if ((x == arr[i] && y == arr[j] || y == arr[i] && x == arr[j]) && min_dist > Abs(i - j)) {
|
||||
min_dist = Abs(i - j)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return min_dist
|
||||
}
|
||||
|
||||
func main() {
|
||||
arr := []int{3, 5, 4, 2, 6, 5, 6, 6, 5, 4, 8, 3}
|
||||
var n int = len(arr)
|
||||
var x int = 3
|
||||
var y int = 6
|
||||
fmt.Printf("Minimum distance between %d and %d is %d", x, y, minDist(arr, n, x, y))
|
||||
}
|
@ -2,8 +2,6 @@
|
||||
|
||||
[What It Is](#what-it-is)
|
||||
|
||||
[How To Use](#how-to-use)
|
||||
|
||||
## What It Is
|
||||
|
||||
* Write a function `rotate(ar[], d, n)` that rotates `arr[]` of size `n` by `d` elements
|
||||
@ -55,8 +53,4 @@ Algorithm Complexity
|
||||
| Complexity | Notation |
|
||||
| ----------------- |:---------:|
|
||||
| `Time Complexity` | `O(n*d)` |
|
||||
| `Auxiliary Space` | `O(1)` |
|
||||
|
||||
|
||||
## How To Use
|
||||
|
||||
| `Auxiliary Space` | `O(1)` |
|
Loading…
Reference in New Issue
Block a user