Learning GoLang: Maximum Gap
Given an integer array nums, return the maximum difference between two successive elements in its sorted form. If the array contains less than two elements, return 0.Example 1:
Input: nums = [3,6,9,1]
Output: 3
Explanation: The sorted form of the array is [1,3,6,9], either (3,6) or (6,9) has the maximum difference 3.
Example 2:Input: nums = [10]
Output: 0
Explanation: The array contains less than 2 elements, therefore return 0.Constraints:
1 <= nums.length <= 104
0 <= nums[i] <= 109
GoLang Exercise as follows:
func maximumGap(nums []int) int {
if len(nums) < 2 {
return 0
}
var ans = 0
sort.Ints(nums)
for i := 1; i < len(nums); i ++ {
ans = int(math.Max(float64(ans), float64(nums[i] - nums[i - 1])))
}
return ans
}
Time complexity is O(NLogN)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Thank you for reading ^^^^^^^^^^^^^^^
NEW! Following my Trail (Upvote or/and Downvote)
Follow me for topics of Algorithms, Blockchain and Cloud.
I am @justyy - a Steem Witness
https://steemyy.com
My contributions
- Video Downloader
- Steem Blockchain Tools
- Free Cryptos API
- VPS Database
- Computing Technology Blog
- A few useless tools
- And some other online software/tools
- Merge Files/Videos
- LOGO Turtle Programming Chrome Extension
- Teaching Kids Programming - Youtube Channel and All Contents
Delegation Service
Support me
If you like my work, please:
- Buy Me a Coffee, Thanks!
- Become my Sponsor, Thanks!
- Voting for me:
https://steemit.com/~witnesses type in justyy and click VOTE
- Delegate SP: https://steemyy.com/sp-delegate-form/?delegatee=justyy
- Vote @justyy as Witness: https://steemyy.com/witness-voting/?witness=justyy&action=approve
- Set @justyy as Proxy: https://steemyy.com/witness-voting/?witness=justyy&action=proxy
Alternatively, you can vote witness or set proxy here: https://steemit.com/~witnesses
Eventhough I don't know GoLang, I coould grasp the algorithm. I think I should try solving the same problem in Python. Would be a fun to do that🙂.
Yes... i am learning Go recently...