0 votes
in Other by
Given an array of integers, write a function that returns true if there is a triplet (a, b, c) that satisfies a2 + b2 = c2.

1 Answer

0 votes
by

Import kotlin. math.*

class ArrayTripletsAlg {

    fun doIt(array: Array<Int>): Triple<Int, Int, Int>? {

        fun sort(a: Int, b: Int, c: Int): Triple<Int, Int, Int> {

            return if (a > b) {

                if (a > c) {

                    if (b > c) {

                        Triple(a, b, c)

                    } else {

                        Triple(a, c, b)

                    }

                } else {

                    Triple(c, a, b)

                }

            } else if (b > c) {

                if (a > c) {

                    Triple(b, a, c)

                } else {

                    Triple(b, c, a)

                }

            } else {

                Triple(c, b, a)

            }

        }

        (0 until array.size - 2).forEach { i ->

            (i + 1 until array.size - 1).forEach { j ->

                (j + 1 until array.size).forEach { k ->

                    val triplets = sort(array[i], array[j], array[k])

                    if (triplets.first.toDouble().pow(2) ==

                            (triplets.second.toDouble().pow(2) + triplets.third.toDouble().pow(2)))

                        return triplets

                }

            }

        }

        return null

    }

}

...