Circular Array Shift

Company: Cognizant_20june

Difficulty: medium

Problem Statement

Circular Array Shift Problem Description You are given an integer array A of length N , indexed from 1 to N . A circular left shift moves every element one position towards the front; the element that was first wraps around and becomes the last. A circular right shift moves every element one position towards the back; the element that was last wraps around and becomes the first. Each such operation counts as one shift . Starting from the given array, you may perform any number of circular shifts, each one a left shift or a right shift, in any order you like. You want to reach an array in which at least one element is equal to its own 1-based index - that is, some position i (with 1 <= i <= N ) holds the value i . Print the minimum total number of shift operations needed to reach such an array. If no sequence of shifts can ever produce one, print -1 . Input Format The first line contains a single integer N , the number of elements. The second line contains N space-separated intege