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

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

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

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,939 questions

51,876 answers

573 users

How to rearrange an array and set arr[i] to arr[arr[index]] to index in VB.NET

1 Answer

0 votes
Imports System

Public Class AClass
    Public Shared Sub rearrange_array(ByVal arr As Integer())
        Dim size As Integer = arr.Length

        For i As Integer = 0 To size - 1
            arr(i) += (arr(arr(i)) Mod size) * size
        Next

        For i As Integer = 0 To size - 1
			arr(i) = arr(i) \ size
        Next
    End Sub

    Public Shared Sub Main(ByVal args As String())
        Dim arr As Integer() = New Integer() {3, 5, 0, 2, 1, 4}

		' arr[arr[0]] = 2 -> arr[0] = 2
		' arr[arr[1]] = 4 -> arr[1] = 4
		' arr[arr[2]] = 3 -> arr[2] = 3
		' arr[arr[3]] = 0 -> arr[3] = 0
		' arr[arr[4]] = 5 -> arr[4] = 5
		' arr[arr[5]] = 1 -> arr[5] = 1

        rearrange_array(arr)

        For Each val As Integer In arr
            Console.Write(val & " ")
        Next
    End Sub
End Class




' run:
'
' 2 4 3 0 5 1
'

 



answered Aug 17, 2022 by avibootz
...