Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,563 questions

56,781 answers

573 users

How to rearrange an array into a zig-zag pattern (a < b > c < d > e < f > g) in Kotlin

3 Answers

0 votes
// This program rearranges an array into a zig-zag pattern, where the elements 
// alternate between being smaller and larger than their neighbors

fun rearrangeIntoZigZag(arr: IntArray) {
    for (i in 0 until arr.lastIndex) {
        val shouldSwap =
            (i % 2 == 0 && arr[i] > arr[i + 1]) ||
            (i % 2 != 0 && arr[i] < arr[i + 1])

        if (shouldSwap) {
            arr[i] = arr[i + 1].also { arr[i + 1] = arr[i] }
        }
    }
}

// a < b > c < d > e < f > g...
// 3 < 5 > 1 < 9 > 6 < 7 > 2 < 4

fun main() {
    val arr = intArrayOf(3, 5, 1, 7, 9, 6, 4, 2)

    rearrangeIntoZigZag(arr)

    println(arr.contentToString())
}


/*
run:

[3, 5, 1, 9, 6, 7, 2, 4]

*/

 



answered Oct 8 by avibootz
edited Oct 8 by avibootz
0 votes
// This program rearranges an array into a zig-zag pattern, where the elements 
// alternate between being smaller and larger than their neighbors

// a < b > c < d > e < f > g...
// 3 < 5 > 1 < 9 > 6 < 7 > 2 < 4

fun rearrangeIntoZigZag(arr: IntArray) {
    for (i in 0 until arr.lastIndex) {
        val shouldSwap =
            (i % 2 == 0 && arr[i] > arr[i + 1]) ||
            (i % 2 == 1 && arr[i] < arr[i + 1])

        if (shouldSwap) {
            val temp = arr[i]
            arr[i] = arr[i + 1]
            arr[i + 1] = temp
        }
    }
}

fun main() {
    val arr = intArrayOf(3, 5, 1, 7, 9, 6, 4, 2)

    rearrangeIntoZigZag(arr)

    println(arr.joinToString(prefix = "[", postfix = "]"))
}


/*
run:

[3, 5, 1, 9, 6, 7, 2, 4]

*/

 



answered Oct 8 by avibootz
0 votes
// This program rearranges an array into a zig-zag pattern, where the elements 
// alternate between being smaller and larger than their neighbors

// a < b > c < d > e < f > g...
// 3 < 5 > 1 < 9 > 6 < 7 > 2 < 4

fun IntArray.zigZag() {
    for (i in 0 until lastIndex) {
        if ((i % 2 == 0 && this[i] > this[i + 1]) ||
            (i % 2 == 1 && this[i] < this[i + 1])) {

            val temp = this[i]
            this[i] = this[i + 1]
            this[i + 1] = temp
        }
    }
}

fun main() {
    val arr = intArrayOf(3, 5, 1, 7, 9, 6, 4, 2)

    arr.zigZag()

    println(arr.contentToString())
}



/*
run:

[3, 5, 1, 9, 6, 7, 2, 4]

*/

 



answered Oct 8 by avibootz

Related questions

...