Open In App

Maximizing Circular Array Iterations

Last Updated : 12 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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));


Output

3








Time Complexity: O(N*logN)
Auxiliary Space: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads