YOU HAVE BEEN GIVEN AN INTEGER ARRAY/LIST(ARR) OF SIZE N. IT HAS BEEN SORTED(IN INCREASING ORDER) AND THEN ROTATED BY SOME NUMBER ‘K’ IN THE CLOCKWISE DIRECTION. YOUR TASK IS TO WRITE A FUNCTION THAT RETURNS THE VALUE OF ‘K’, THAT MEANS, THE INDEX FROM WHICH THE ARRAY/LIST HAS BEEN ROTATED.

TITLE:-Check Array Rotation
PROBLEM:-

You have been given an integer array/list(ARR) of size N. It has been sorted(in increasing order) and then rotated by some number ‘K’ in the clockwise direction.

Your task is to write a function that returns the value of ‘K’, that means, the index from which the array/list has been rotated.

Input format :
The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow.

First line of each test case or query contains an integer 'N' representing the size of the array/list.

Second line contains 'N' single space separated integers representing the elements in the array/list.
Output Format :
For each test case, print the value of 'K' or the index from which which the array/list has been rotated.

Output for every test case will be printed in a separate line.
Constraints :
1 <= t <= 10^2
0 <= N <= 10^5
Time Limit: 1 sec
Sample Input 1:
1
6
5 6 1 2 3 4
Sample Output 1:
2
Sample Input 2:
2
5
3 6 8 9 10
4
10 20 30 1
Sample Output 2:
0
3

SOLUTION:-

#include <iostream>
using namespace std;
int arrayRotateCheck(int *input, int size)
{
    //Write your code here
    int pos=0,min=input[0];
    for(int i=1;i<size;i++)
    {
        if(min>input[i])
        {
            min=input[i];
            pos=i;
        }
    }
    return pos;
}

int main()
{

	int t;
	cin >> t;
	while (t--)
	{

		int size;
		cin >> size;
		int *input = new int[size];

		for (int i = 0; i < size; i++)
		{
			cin >> input[i];
		}

		cout << arrayRotateCheck(input, size) << endl;
		delete[] input;
	}
	
	return 0;
}

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *