Monk and Rotation

QUESTION

Monk loves to preform different operations on arrays, and so being the principal of Hackerearth School, he assigned a task to his new student Mishki. Mishki will be provided with an integer array A of size \nN and an integer K , where she needs to rotate the array in the right direction by K steps and then print the resultant array. As she is new to the school, please help her to complete the task.\n\nInput:\nThe first line will consists of one integer T denoting the number of test cases. \nFor each test case:\n1) The first line consists of two integers N and K, N being the number of elements in the array and K denotes the number of steps of rotation.\n2) The next line consists of \nN space separated integers , denoting the elements of the array A.\nOutput:\nPrint the required array.\n\nConstraints:\n1N10^5\n0K10^6\n0A[i]10^6.

“TESTCASE_1”: “1\n5 2\n1 2 3 4 5\n###—###SEPERATOR—###—\n4 5 1 2 3”, “TESTCASE_2”: “1\n5 3\n1 2 3 4 5\n###—###SEPERATOR—###—\n3 4 5 1 2”, “TESTCASE_3”: “2\n7 2\n11 39 43 49 51 60 33\n3 2\n1 101 201\n###—###SEPERATOR—###—\n60 33 11 39 43 49 51 \n101 201 1”, “TESTCASE_4”: “1\n7 0\n11 39 43 49 51 60 33\n###—###SEPERATOR—###—\n11 39 43 49 51 60 33”, “TESTCASE_5”: “0\n###—###SEPERATOR—###—\n0

ANSWER

#include <stdio.h>
 
#include <stdio.h>
    #include <stdlib.h>
    #include <memory.h>
    #include<unistd.h>
    #include<sys/types.h>
    #include<sys/stat.h>
    #include<fcntl.h>
    #include<string.h>
     
    typedef struct {	
    	int K ;
    	int N ;
    	int* A ;
    } StrArr ;
     
    void rotandprint(StrArr strA)
    {
    	int i,j,l1 ;
     
    	
    	if(strA.N > strA.K)
    	{
    	  j=strA.N-strA.K ;
    	}
    	else
    	{
    	  l1 = (strA.K % strA.N)  ;
    	  j=strA.N-l1 ;
        }
     
     
     
    	for(i=j;i<strA.N;i++)
    	{
    			printf("%d ",strA.A[i]) ; 
    	}
    	
    	for(i=0;i<j;i++)
    	{
    			printf("%d ",strA.A[i]) ; 
    	}
    	
    	
    	
    	putchar('\n') ; 
    	
    }
     
     
    int main(int argc, char** argv)
    {
      int T,i,j,isz, csz ;
      char tmp[15], c='Z', *l ;
      StrArr strA ;
      isz=sizeof(int) ;
      csz = sizeof(char) ;
     
      scanf("%d",&T) ;
      
      for(i=0;i<T;i++)
      {
    	  scanf("%d %d",&(strA.N),&(strA.K)) ;
      	  strA.A = malloc(isz*(strA.N)) ;	
    	  l=tmp ;
    	  memset(tmp,0,15) ;
    	  for(j=0 ; j < strA.N  ; ) 
    	  {
    	   	  c = getchar() ;
    		  if(c!=' ' && c!='\n' && c>-1)
    		  {
    			*l=c ;
    			l+=csz ;
    		  }
    		  else
    		  {
    		    if(l==tmp) continue ;
    			*((strA.A)+j) = atoi(tmp) ;
    			memset(tmp,0,15) ;
    			l=tmp ;
    			j++ ;			
    		  }
    	      
    	  }
    	  rotandprint(strA) ;
    	  free(strA.A) ;
      }
      
      return 0 ;
    }
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
100% Free SEO Tools - Tool Kits PRO