LeetCode Entry
1502. Can Make Arithmetic Progression From Sequence
Is IntArray can be arithmetic progression?
1502. Can Make Arithmetic Progression From Sequence easy blog post substack
Join me on Telegram
https://t.me/leetcode_daily_unstoppable/237
Problem TLDR
Is IntArray can be arithmetic progression?
Intuition
Sort, then use sliding window.
Approach
Let’s write Kotlin one-liner.
Complexity
- Time complexity: \(O(nlog(n))\)
- Space complexity: \(O(n)\)
Code
fun canMakeArithmeticProgression(arr: IntArray): Boolean =
arr.sorted().windowed(2).groupBy { it[1] - it[0] }.keys.size == 1