Rearrange Geek and His Classmate Geeks For Geeks

 Problem Link:


https://practice.geeksforgeeks.org/problems/47e5aa3f32aee9e0405f04960f37c8a562d96a2f/1#


It is also the problem of the day for 18-February-2022



Solution:


long arr[]=new long[n];

        

        for(int i=0;i<n;i++){

            long b=a[i];

            //System.out.println(b);

            int b1=(int)b;

            arr[i]=a[b1];

        }

        

       /* for(int i=0;i<n;i++){

            System.out.println(arr[i]);

        }*/

        

        for(int i=0;i<arr.length;i++){

            a[i]=arr[i];

        }



TIME COMPLEXITY: O(N)[Array Length][GFG Time:3.5/5.0]

SPACE COMPLEXITY:O(N)[Array Is Used]

AUXILIARY SPACE:O[N][Additional Array Is Used]

TOTAL TEST CASES:26

APPROACH USED:

1. First we will traverse the array store the array value of the element.

For better explaination refer this example:

array: 0 5 1 4 2 3

array index: 0 1 2 3 4 5 

Now according to question we need to exchange places and we do that as 

arr[1]=5 now we will find out the value at arr[5] ie 3 and store it in another array as arr1[1]=3.

Similarly, we will do that for all indexes and store their corresponding values in arr1.

Later we will copy the contents of arr1 into the original array.


arr[0]=0-> arr[0]=0 store into arr1[0]  later copy to arr[0]

arr[1]=5 ->arr[5]=3 store into arr1[1] later copy to arr[1]

arr[2]=1 -> arr[1]=5 store into arr1[2] later copy to arr[2]

arr[3]=4 ->arr[4]=2 store into arr1[3] later copy to arr[3]

arr[4]=2 ->arr[2]=1 store into arr1[4] later copy to arr[4]

arr[5]=3 ->arr[3]=4 store into arr1[5] later copy to arr[5]


return arr;



"Thanks For Reading.😇"

"Share Further To Increase Knowledge Treasure.😊"


Comments

Popular posts from this blog

Solutions Of Practice Questions Dated 01-06-2022

CODEFORCES SPY DETECTED ROUND 713

Maximum Winning Score Geeks For Geeks