Geeks For Geeks problem|| Rotating an Array

 
In our programming website we teach computer coding to kids and pros for free online in easy way and gives knowledge  about Data structure and alogorithm in C,Java,Python and other programming languages that may help you land an  online job and make your programmers day successfull.We use best data structure notes and best programming resources. Here goes the article,dont forget to share with your friends ??

Geeks For Geeks have many meticulous problems in their bag,and for any programming enthusiasts it is pleasure to solve them. In our previous post I solved one of the problems you can check  it out here.
        Like our previous post I again want to mention that I am not doing any promotion here,I just love to solve their problems as a code lover.
        In our programming website we teach computer coding to kids and pros for free online in easy way and gives knowledge about Data structure and algorithms in C,Java,Python and other programming languages that may help you land an online job and make your programmers day successful.We use best data structure notes and best programming resources.
Here goes the article,don't forget to share with your friends.
         Let us begin with the problem:
Problem Statement: Let we have T number of testcases. For each testcase we have an array of size                                       N.Now we have to rotate the array by d elements.The first line of input contains
                                   the number of testcase T,the second line contains the size of the array N,third                                       line contains N space separated integers and fourth line contains d.
Example:
                 Input:
                  2
                  5
                  1 2 3 4 5
                  2
                  1
                  4
                  1
                 Output:
                  3 4 5 1 2
                  4
Solution:
#include <stdio.h>

main() {
int a[1000],i,j,k,N,d,T;
scanf("%d",&T); //Taking Number of testcases
while(T--)
{
    scanf("%d",&N); // Taking size of array
    for(i=0;i<N;i++)
        scanf("%d",&a[i]); //Taking array
    scanf("%d",&d);
    for(j=d;j<N;j++) //printing the elements from skipping d elements to last element
    {
        printf("%d ",a[j]);
    }
    for(k=0;k<d;k++)
        printf("%d ",a[k]); //printing 1st d elements after printing the elements of previous segment
   printf("\n");   
}

return 0;

}
Programming website about computer coding for free online in data structures and alogorithms in c,java,python etc with best programming resources
Check out my instagram handle to stay connected.

Post a Comment

0 Comments