# C Programming – HackerRank Solution | Rotation Factor |

### (HackerRank) Write a Modular C Programming code to solve Rotation Factor, If the array was not rotated then the array would have been sorted. Find ‘K’

Given an array which is rotated towards right by some factor ‘K’. If the array was not rotated then the array would have been sorted. Find ‘K’

Input Format

First Line contains ‘t’ number of test cases.

Next ‘t’ line conatins ‘N’ number of array elements followed by N array elements

Constraints

N>0 and N<=10^5

-10^5<=a[i]<=10^5

Output Format

For each test case output the rotation factor in new line.

Sample Input 0

```-4
```

Sample Output 0

```Invalid
```

Sample Input 1

```2
5 4 5 1 2 3
6 17 25 29 6 8 10
```

Sample Output 1

```2
3
```

Explanation 1

``````First Line is 2, which represents number of test cases.

The first number of second line is the number of array elements.

For 1st test case, the array is 4 5 1 2 3, If the array wouldn't had been rotated then the array would have been 1 2 3 4 5, we see that the array is rotated towards right circularly by factor 2.

Similarly for 2nd test case the array is rotated right by factor 3.``````

CODE:

```#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>

int check_ascending(int arrray[],int n);
int check_descending(int array[],int n);
int main() {
int test;
scanf("%d",&test);
if(test<0) { printf("Invalid\n");} while(test>0)
{
int k=0,n,i,temp;
scanf("%d",&n);
int array[n];
for(i=0;i<n;i++)
{
scanf("%d",&array[i]);
}
while(1)
{
if(check_ascending(array,n)==1)
break;
else if(check_descending(array,n)==1)
break;
for(i=0;i<n-1;i++)
{
temp=array[i];
array[i]=array[i+1];
array[i+1]=temp;
}
k++;
}
printf("%d\n",k);
test--;
}
return 0;
}
int check_ascending(int array[],int n)
{
int i,status=1;
for(i=0;i<n-1;i++) { if(array[i]>array[i+1])
{
status=0;
break;
}
}
return status;
}
int check_descending(int array[],int n)
{
{
int i,status=1;
for(i=0;i<n-1;i++)
{
if(array[i]<array[i+1])
{
status=0;
break;
}
}
return status;
}
}

```

OUTPUT

```Congratulations, you passed the sample test case.

Click the Submit Code button to run your code against all the test cases.

Input (stdin)

-4

Invalid
Expected Output

Invalid```

Please find some more codes of Loops, Condition Statements, 1D Arrays, 2D Arrays, Strings, Pointers, Data Structures, Files, Linked lists, MISC, Solved model question papers & Hacker Rank all solutions on the below page:

Top 100+ C Programming codes – KLE Technological University 