Count of possible rotations of given Array to remove largest element from first half
Last Updated :
28 Mar, 2022
Given an array arr[ ] with even length N, the task is to find the number of cyclic shifts (rotations) possible for this array, such that the first half of array does not contain the maximum element.
Examples:
Input: N = 6, arr[ ] = { 3, 3, 5, 3, 3, 3 }
Output: 3
Explanation: The maximum element here is 5 at index 2. This 5 can be shifted to second half of the array using any of the below three valid right shifts:
- shift by 1: (3, 3, 3, 5, 3, 3)
- shift by 2: (3, 3, 3, 3, 5, 3)
- shift by 3: (3, 3, 3, 3, 3, 5)
Input: N = 6, arr[ ] = {8, 8, 9, 8, 8, 9}
Output: 0
Explanation: Any rotation in the article wont help in removing the maximum element from 1st half, as there are two occurrences of 9 at index 2 and 5. So any rotation will keep atleast one 9 at index 0, 1 or 2.
Naive Approach: The most basic approach to solve this problem, is based on below idea:
Find all rotations of given array. In the end, just return the count of such rotations which do not have the maximum element in first half.
Time Complexity: O(N3) where N^2 is for rotations and N is for finding maximum in each rotation.
Auxiliary Space: O(1)
Efficient Approach: The idea to solve the problem is by traversing the array and some basic concepts of maths.
The idea is to find distance between maximum elements and simply check if the range is greater than half of array size. If yes, rotation is possible, or else rotation is not possible.
For case when rotation is possible, we can find the [range - (n/2)] as the valid number of rotations.
Follow the steps to solve the problem:
- Firstly, find the maximum element
- Then find the maximal ranges between pairs of maximum elements.
- A range with length m adds max(m-frac(N/2)+1, 0) to the answer.
Below is the implementation of the above approach:
C++
// C++ program for the above approach.
#include <bits/stdc++.h>
using namespace std;
// Function to find the number of cyclic shifts
int find(int arr[], int N)
{
int maxele = *max_element(arr, arr + N);
int left = -1;
int right = -1;
// Placing left pointer
// On its correct position
for (int i = 0; i < N; i++) {
if (arr[i] == maxele) {
left = i;
break;
}
}
// Placing right pointer
// On its correct position
for (int i = N - 1; i >= 0; i--) {
if (arr[i] == maxele) {
right = i;
break;
}
}
int ans = (N / 2) - (right - left);
if (ans <= 0) {
return 0;
}
else {
return ans;
}
}
// Driver Code
int main()
{
int arr[] = { 3, 3, 5, 3, 3, 3 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function call
cout << find(arr, N);
return 0;
}
Java
// Java program for the above approach.
import java.io.*;
class GFG {
// Function to find the number of cyclic shifts
static int find(int arr[], int N)
{
int maxele = Integer.MIN_VALUE;
for(int i = 0; i < N; i++){
maxele = Math.max(arr[i], maxele);
}
int left = -1;
int right = -1;
// Placing left pointer
// On its correct position
for (int i = 0; i < N; i++) {
if (arr[i] == maxele) {
left = i;
break;
}
}
// Placing right pointer
// On its correct position
for (int i = N - 1; i >= 0; i--) {
if (arr[i] == maxele) {
right = i;
break;
}
}
int ans = (N / 2) - (right - left);
if (ans <= 0) {
return 0;
}
else {
return ans;
}
}
// Driver Code
public static void main (String[] args) {
int arr[] = { 3, 3, 5, 3, 3, 3 };
int N = arr.length;
// Function call
System.out.print(find(arr, N));
}
}
// This code is contributed by hrithikgarg03188.
Python3
# Python code for the above approach
# Function to find the number of cyclic shifts
def find(arr, N):
maxele = max(arr)
left = -1
right = -1
# Placing left pointer
# On its correct position
for i in range(N):
if (arr[i] == maxele):
left = i
break
# Placing right pointer
# On its correct position
for i in range(N - 1, -1, -1):
if (arr[i] == maxele):
right = i
break
ans = (N // 2) - (right - left)
if (ans <= 0):
return 0
else:
return ans
# Driver Code
arr = [3, 3, 5, 3, 3, 3]
N = len(arr)
# Function call
print(find(arr, N))
# This code is contributed by shinjanpatra
C#
// C# program for the above approach.
using System;
class GFG {
// Function to find the number of cyclic shifts
static int find(int[] arr, int N)
{
int maxele = Int32.MinValue;
for (int i = 0; i < N; i++) {
maxele = Math.Max(arr[i], maxele);
}
int left = -1;
int right = -1;
// Placing left pointer
// On its correct position
for (int i = 0; i < N; i++) {
if (arr[i] == maxele) {
left = i;
break;
}
}
// Placing right pointer
// On its correct position
for (int i = N - 1; i >= 0; i--) {
if (arr[i] == maxele) {
right = i;
break;
}
}
int ans = (N / 2) - (right - left);
if (ans <= 0) {
return 0;
}
else {
return ans;
}
}
// Driver Code
public static void Main()
{
int[] arr = { 3, 3, 5, 3, 3, 3 };
int N = arr.Length;
// Function call
Console.Write(find(arr, N));
}
}
// This code is contributed by Samim Hossain Mondal.
JavaScript
<script>
// JavaScript code for the above approach
// Function to find the number of cyclic shifts
function find(arr, N) {
let maxele = Math.max([...arr]);
let left = -1;
let right = -1;
// Placing left pointer
// On its correct position
for (let i = 0; i < N; i++) {
if (arr[i] == maxele) {
left = i;
break;
}
}
// Placing right pointer
// On its correct position
for (let i = N - 1; i >= 0; i--) {
if (arr[i] == maxele) {
right = i;
break;
}
}
let ans = Math.floor(N / 2) - (right - left);
if (ans <= 0) {
return 0;
}
else {
return ans;
}
}
// Driver Code
let arr = [3, 3, 5, 3, 3, 3];
let N = arr.length;
// Function call
document.write(find(arr, N));
// This code is contributed by Potta Lokesh
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Javascript Program to Find element at given index after a number of rotations An array consisting of N integers is given. There are several Right Circular Rotations of range[L..R] that we perform. After performing these rotations, we need to find element at a given index.Examples : Input : arr[] : {1, 2, 3, 4, 5} ranges[] = { {0, 2}, {0, 3} } index : 1 Output : 3 Explanation
4 min read
Count elements less than or equal to a given value in a sorted rotated array Given a sorted array of n distinct integers rotated at some point. Given a value x. The problem is to count all the elements in the array which are less than or equal to x. Examples: Input : arr[] = {4, 5, 8, 1, 3}, x = 6 Output : 4 Input : arr[] = {6, 10, 12, 15, 2, 4, 5}, x = 14 Output : 6 Naive A
15 min read
Javascript Program to Maximize count of corresponding same elements in given Arrays by Rotation Given two arrays arr1[] and arr2[] of N integers and array arr1[] has distinct elements. The task is to find the maximum count of corresponding same elements in the given arrays by performing cyclic left or right shift on array arr1[]. Examples: Input: arr1[] = { 6, 7, 3, 9, 5 }, arr2[] = { 7, 3, 9,
3 min read
Find elements larger than half of the elements in an array | Set 2 Given an array arr[] consisting of N positive integers, the task is to find the elements which are greater than at least half of the array elements. Examples: Input: arr[] = {1, 6, 3, 4}Output: 4 6Explanation:Size of the array is 4. The elements which are greater than atleast N/2(= 4/2 = 2) elements
7 min read
Count rotations required to sort given array in non-increasing order Given an array arr[] consisting of N integers, the task is to sort the array in non-increasing order by minimum number of anti-clockwise rotations. If it is not possible to sort the array, then print "-1". Otherwise, print the count of rotations. Examples: Input: arr[] = {2, 1, 5, 4, 3}Output: 2Expl
7 min read
Find the maximum possible value of last element of the Array Given a non-negative array arr of size N and an integer M representing the number of moves such that in one move, the value of any one element in the array decreases by one, and the value of its adjacent element on the right increases by one. The task is to find the maximum possible value of the las
7 min read
Javascript Program to Find the Mth element of the Array after K left rotations Given non-negative integers K, M, and an array arr[] with N elements find the Mth element of the array after K left rotations. Examples: Input: arr[] = {3, 4, 5, 23}, K = 2, M = 1Output: 5Explanation:Â The array after first left rotation a1[ ] = {4, 5, 23, 3}The array after second left rotation a2[ ]
2 min read
Javascript Program to Count of rotations required to generate a sorted array Given an array arr[], the task is to find the number of rotations required to convert the given array to sorted form.Examples: Input: arr[] = {4, 5, 1, 2, 3}Â Output: 2Â Explanation:Â Sorted array {1, 2, 3, 4, 5} after 2 anti-clockwise rotations. Input: arr[] = {2, 1, 2, 2, 2}Â Output: 1Â Explanation:Â So
4 min read
Javascript Program to Count rotations required to sort given array in non-increasing order Given an array arr[] consisting of N integers, the task is to sort the array in non-increasing order by minimum number of anti-clockwise rotations. If it is not possible to sort the array, then print "-1". Otherwise, print the count of rotations. Examples: Input: arr[] = {2, 1, 5, 4, 3}Output: 2Expl
3 min read
Javascript Program to Find Mth element after K Right Rotations of an Array Given non-negative integers K, M, and an array arr[ ] consisting of N elements, the task is to find the Mth element of the array after K right rotations. Examples: Input: arr[] = {3, 4, 5, 23}, K = 2, M = 1 Output: 5 Explanation: The array after first right rotation a1[ ] = {23, 3, 4, 5} The array a
8 min read