Rearrange an array such that ' arr [ j ] ' becomes ' i ' if ' arr [ i ] ' is ' j ' | Set 1 | A simple C # program to rearrange contents of arr [ ] such that arr [ j ] becomes j if arr [ i ] is j ; A simple method to rearrange ' arr [ 0 . . n - 1 ] ' so that ' arr [ j ] ' becomes ' i ' if ' arr [ i ] ' is ' j ' ; retrieving old value and storing with the new one ; retrieving new value ; A utility function to print contents of arr [ 0. . n - 1 ] ; Driver program
using System ; class GFG { static void rearrange ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) { arr [ arr [ i ] % n ] += i * n ; } for ( int i = 0 ; i < n ; i ++ ) { arr [ i ] /= n ; } } static void printArray ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; } static public void Main ( ) { int [ ] arr = { 2 , 0 , 1 , 4 , 5 , 3 } ; int n = arr . Length ; Console . WriteLine ( " Given ▁ array ▁ is ▁ : ▁ " ) ; printArray ( arr , n ) ; rearrange ( arr , n ) ; Console . WriteLine ( " Modified ▁ array ▁ is ▁ : " ) ; printArray ( arr , n ) ; } }