![JAR search and dependency download from the Maven repository](/logo.png)
g1901_2000.s1995_count_special_quadruplets.Solution.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-kotlin Show documentation
Show all versions of leetcode-in-kotlin Show documentation
Kotlin-based LeetCode algorithm problem solutions, regularly updated
package g1901_2000.s1995_count_special_quadruplets
// #Easy #Array #Enumeration #2023_06_21_Time_146_ms_(100.00%)_Space_34.3_MB_(50.00%)
class Solution {
fun countQuadruplets(nums: IntArray): Int {
var count = 0
// max nums value is 100 so two elements sum can be max 200
val m = IntArray(201)
for (i in 1 until nums.size - 2) {
for (j in 0 until i) {
// update all possible 2 sums
m[nums[j] + nums[i]]++
}
for (j in i + 2 until nums.size) {
// fix third element and search for fourth - third in 2 sums as a + b + c = d == a
// + b = d - c
val diff = nums[j] - nums[i + 1]
if (diff >= 0) {
count += m[diff]
}
}
}
return count
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy