JUGGLING ALGORITHM IN C++

 Hi coders! Here I am with a problem which is related to a  famous data Structure Array. Many of you people definitely have solved the array rotation problem. There are various methods to rotate an array. So, here is a very efficient algorithm known as Juggling Algorithm to rotate an array with some d elements. This algorithm is based on GCD (Greatest Common Divisor) or HCF(Highest Common Factor).



METHOD TO IMPLEMENT :) CHEER UP GUYS ITS GONNA BE FUN ....

:) FIRSTLY divide the array by finding the GCD of d and n where d is the number of elements by which the array has to be rotated and n is the size of the array.

UK WHAT CHALO EXAMPLE SE SIKHEE ;)

Let an array be like arr[] = {1,2,3,4,5,6,7,8,9} which is to be rotate  by d = 3 elements. Here n = 9 and d = 3 so GCD is 3. 

EASY PEASY RIGHT :>() BUT THEN WHAT..... :/

:[] SIDHI BHASA -:

JITNA BHI HCF AYA NA PURE ARRAY KO UTNE NUMBER OF SET MAI DIVIDE KR DENE KA .. :") MATLAB JAISE AGR ARRAY HAI {1,2,3,4,5,6,7,8,9} AB HCF AYA THA 3 TO YR ARRAY KO 3 HISSO MAI DIVIDE KR DENE KA JAISE {1,2,3},{4,5,6},{7,8,9} BS AB IN 3 SET MAI ROTATION KARENGE :) 

AGR ITNA AA GAYA TOH CHEER UP BS HO GAYA YRR.... :*)


YE SAMGHO KI ROTATION INDEX [0,3,6] PE HOGI :-) KYU KI VO FIRST ELEMENT HAI HAR SET KE TOH PAHALE UNHE UNKI SAHI POSITION PE LE JAIGE :<?

After elements moved in the first set, the array is like : {4,2,3,7,5,6,1,8,9}

After elements moved in the second set, the array is like: {4,5,3,7,8,6,1,2,9}

Finally, after elements moved in the third set, the array is like: {4,5,6,7,8,9,1,2,3}

ISKA MATLAB SAMGHE SAMBHA :>/


Array Rotation In-Place | Codewhoop



Hence the time complexity of rotating an array by juggling algorithm is O(n).
Hope you like the solution to rotate an array using juggling algorithm


AB JARA CODE DEKH LO AUR EK BAR DRY RUN KR LENA SAMAJ JAOGE :)

#include <iostream>

using namespace std;


int gcd(int a,int b)

{

     if(b==0)                  //mere piyare dosto ye bs HCF nikalega

                              //ye to bs normal maths hai

         return a;

    return gcd(b%a, a);

}

void rotation(int arr[],int n,int d)

{                           

    d=d%n;                     //ye isliye taki agr kabhi d>n

    int g_c_d=gcd(d,n);        //to d=d%n d ko chota kr dega fir bhi same ans aye :) 

                               //kyu ki multiple hat jainge aur bs remainder bachega 

    for(int i=0;i<g_c_d;i++)   //aur jitna remainder utna rotation baki multiple se toh 

    {                          //vapas array gum ke vohi ban jata hai na ise ese socho

        int temp=arr[i];       //size =5,d=5,ghum ke vohi phauch jaoge na isse accha 

        int j=i;               //rotation hi mt kaaro :>)

        while(1)

        {

            arr[j]=arr[j+g_c_d];

            j=(j+g_c_d)%n;            //yaha hmne subsets ko rotate kr diya  

            if((j+g_c_d)%n==i)

                break;

        }

        arr[j]=temp;

    }

}


int main()

{

    int n;

    cout<<"enter the size of array"<<endl;

    cin>>n;

    int arr[n];

    cout<<"enter the values"<<endl;

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

        cin>>arr[i];

    int d;

    cout<<"how many rotation"<<endl;

    cin>>d;

    rotation(arr, n, d);

    cout<<"your rotated array"<<endl;

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

        cout<<arr[i]<<"  ";

    cout<<endl;

    return 0;

    

}


AGR NAHI SAMGHE TOH MAI HU HI COMMENT MARO MAI VIDEO BANA KE SAMGHA DUNGA .


THANK YOU

ABHISHEK DANGI.




Comments

Post a Comment