Maximizing Circular Array Iterations
Last Updated :
12 Jan, 2024
Given a circularly connected array A[] of length N, you are allowed to rearrange the numbers in the array initially in any order to maximize the number of iterations, the task is to determine the maximum number of iterations that can be made by choosing a starting index i (1 <= i <= N) optimally and following the simulation rules below:
- If the current element A[i] is greater than 0, decrement the current element by 1.
- Move to the adjacent right element (circularly connected).
Examples:
Input: N = 3, A[] = {2, 1, 1}
Output: 4
Explanation: One of the optimal orders in which numbers can be arranged: 1->2->3->1 with 4 iterations maximum.
Input: N = 4, A[] = {3, 0, 2, 1}
Output: 3
Explanation: One of the orders in which all the numbers can be arranged is: 4->3->1->2 i.e. A[] = { 1, 2, 3, 0 } with 3 iterations maximum.
Approach: To solve the problem follow the below observations:
Observations:
- It is best to start with the highest number and end with the lowest. So sort the array in descending order.
- The minimum number will provide a bottleneck to the array as we stop whenever any number becomes 0.
- For each iteration, each number decrements by 1. We can iterate fully till minimum > 0 and the answer increases by N.
- When the minimum becomes 0, we would like to iterate all the numbers greater than 0 in the last. As soon as the number becomes 0, we stop.
- If minimum is last number , then we can perform total of minimum*N iterations . After that, we know that only those greater than minimum can iterate . So find position of that last number .
Steps to solve the problem:
- Sort the array in descending order.
- Let answer be ans.
- If the last element is minimum , add (minimum*N) to ans
- Iterate through the array and if A[i] == minimum, add i to ans.
Below is the implementation of the above approach:
C++
// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the
// maximum number of iterations
int helper(int A[], int N)
{
sort(A, A + N, greater<int>());
int minimum = A[N - 1];
int pos;
for (int i = 0; i < N; i++) {
if (A[i] == minimum) {
// The first position which
// is minimum
pos = i;
break;
}
}
return (minimum * N + pos);
}
// Driver Code
int main()
{
int N = 4;
int A[] = { 3, 0, 2, 1 };
// Function Call
cout << helper(A, N);
return 0;
}
Java
import java.util.*;
public class MaxIterations {
// Function to find the maximum number of iterations
static int helper(int[] A, int N)
{
Arrays.sort(A);
for (int i = 0, j = N - 1; i < j; i++, j--) {
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
int minimum = A[N - 1];
int pos = 0;
for (int i = 0; i < N; i++) {
if (A[i] == minimum) {
// The first position which is minimum
pos = i;
break;
}
}
return (minimum * N + pos);
}
// Driver Code
public static void main(String[] args)
{
int N = 4;
int[] A = { 3, 0, 2, 1 };
// Function Call
System.out.println(helper(A, N));
}
}
Python3
# Function to find the maximum number of iterations
def helper(A):
# Sort the array in descending order
A.sort(reverse=True)
# Find the minimum element and its position
minimum = A[-1]
pos = 0
for i in range(len(A)):
if A[i] == minimum:
# The first position which is minimum
pos = i
break
return minimum * len(A) + pos
# Driver Code
if __name__ == "__main__":
N = 4
A = [3, 0, 2, 1]
# Function Call
print(helper(A))
C#
// C# program to implement
// the above approach
using System;
using System.Linq;
class Program
{
// Function to find the
// maximum number of iterations
static int Helper(int[] A, int N)
{
Array.Sort(A);
Array.Reverse(A);
int minimum = A[N - 1];
int pos = 0;
for (int i = 0; i < N; i++)
{
if (A[i] == minimum)
{
// The first position which
// is minimum
pos = i;
break;
}
}
return (minimum * N + pos);
}
// Driver Code
static void Main(string[] args)
{
int N = 4;
int[] A = { 3, 0, 2, 1 };
// Function Call
Console.WriteLine(Helper(A, N));
}
}
JavaScript
// Function to find the maximum number of iterations
function helper(A) {
// Sort the array in descending order
A.sort((a, b) => b - a);
// Find the minimum element and its position
let minimum = A[A.length - 1];
let pos = 0;
for (let i = 0; i < A.length; i++) {
if (A[i] === minimum) {
// The first position which is minimum
pos = i;
break;
}
}
return minimum * A.length + pos;
}
// Driver Code
const N = 4;
const A = [3, 0, 2, 1];
// Function Call
console.log(helper(A));
Time Complexity: O(N*logN)
Auxiliary Space: O(1)
Similar Reads
Generate Circulant Matrix from given Array Given an array A[], the task is to find the circulant matrix made by this array. A circulant matrix is a square matrix of order N x N, where each column is composed of the same elements, but each column is rotated one element to the bottom relative to the preceding column. It is a particular kind of
6 min read
Maximize sum of consecutive differences in a circular array Given an array of n elements. Consider array as circular array i.e element after an is a1. The task is to find maximum sum of the difference between consecutive elements with rearrangement of array element allowed i.e after rearrangement of element find |a1 - a2| + |a2 - a3| + ...... + |an - 1 - an|
5 min read
House Robber - Circular Array Given an array arr[] of positive numbers, find the maximum sum of a subsequence with the constraint that no 2 numbers in the sequence should be adjacent in the array where the last and the first elements are assumed adjacent. Examples: Input: arr[] = [2, 2, 3, 1, 2]Output: 5Explanation: Maximum stol
1 min read
Circular array An array is called circular if we consider the first element as next of the last element. Circular arrays are used to implement queue (Refer to this and this).An example problem : Suppose n people are sitting at a circular table with names A, B, C, D, ... Given a name, we need to print all n people
8 min read
Maximum consecutive oneâs (or zeros) in a binary circular array Given a binary circular array of size N, the task is to find the count maximum number of consecutive 1âs present in the circular array.Examples: Input: arr[] = {1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1} Output: 6 The last 4 and first 2 positions have 6 consecutive ones. Input: a[] = {0, 1, 0, 1, 0, 1, 0,
8 min read
Find the next greater element in a Circular Array Given a circular array arr[] of size n with distinct elements, find the next greater element for each element in the array.Note: The next greater element of an element is the first element greater than it when traversing the array in order (circularly). If no such element exists, return -1 for that
9 min read