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++
#include <bits/stdc++.h>
using namespace std;
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) {
pos = i;
break ;
}
}
return (minimum * N + pos);
}
int main()
{
int N = 4;
int A[] = { 3, 0, 2, 1 };
cout << helper(A, N);
return 0;
}
|
Java
import java.util.*;
public class MaxIterations {
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) {
pos = i;
break ;
}
}
return (minimum * N + pos);
}
public static void main(String[] args)
{
int N = 4 ;
int [] A = { 3 , 0 , 2 , 1 };
System.out.println(helper(A, N));
}
}
|
Python3
def helper(A):
A.sort(reverse = True )
minimum = A[ - 1 ]
pos = 0
for i in range ( len (A)):
if A[i] = = minimum:
pos = i
break
return minimum * len (A) + pos
if __name__ = = "__main__" :
N = 4
A = [ 3 , 0 , 2 , 1 ]
print (helper(A))
|
C#
using System;
using System.Linq;
class Program
{
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)
{
pos = i;
break ;
}
}
return (minimum * N + pos);
}
static void Main( string [] args)
{
int N = 4;
int [] A = { 3, 0, 2, 1 };
Console.WriteLine(Helper(A, N));
}
}
|
Javascript
function helper(A) {
A.sort((a, b) => b - a);
let minimum = A[A.length - 1];
let pos = 0;
for (let i = 0; i < A.length; i++) {
if (A[i] === minimum) {
pos = i;
break ;
}
}
return minimum * A.length + pos;
}
const N = 4;
const A = [3, 0, 2, 1];
console.log(helper(A));
|
Time Complexity: O(N*logN)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...