Find instances at end of time frame after auto scaling - GeeksforGeeks

06 Oct, 2021
Given an integer, instances, and an array, arr[] of size N representing the average utilization percentage of the computing system at each second, the task is to find the number of instances at the end of the time frame such that the computing system auto-scales the number of instances according to the following rules: 
Average utilization < 25%: Reduce the number of instances by half if the number of instances is greater than 1.
25% ≤ Average utilization ≤ 60%: Take no action.
Average utilization > 60%: Double the number of instances if the doubled value does not exceed 2* 108.
Once an action of adding or reducing the number of instances is performed, the system will stop monitoring for 10 seconds. During that time, the number of instances does not change.
Examples:
Input: instances = 2, arr[] = {25, 23, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 76, 80}Output: 2Explanation:At second 1, arr[0] = 25 ≤ 25, so take no action.At second 2, arr[1] = 23 < 25, so an action is instantiated to halve the number of instances to ceil(2/2) = 1. The system will stop checking for 10 seconds, so from arr[2] through arr[11] no actions will be taken.At second 13, arr[12] = 76 > 60, so the number of instances is doubled from 1 to 2.
There are no more readings to consider and 2 is the final value.
Input: instances = 5, arr = {30, 5, 4, 8, 19, 89}Output: 3Explanation:At second 1, 25 ≤ arr[0] = 30 ≤ 60, so take no action.At second 2, arr[1] = 5 < 25, so an action is instantiated to halve the number of instances to ceil(5/2) = 3.The system will stop checking for 10 seconds, so from arr[2] through arr[5] no actions will be taken.
There are no more readings to consider and 3 is the final answer.
Approach: The given problem can be solved by traversing the given array arr[] and if the current element is less than 25 then divide the number of instances by 2 if the number of instances is greater than 1. Otherwise, if the current value is greater than 60 multiply the number of instances by 2 if the number of instances is not greater than 108, after performing either of the two operations increment the current index by 10. Follow the steps to solve the problem:
Traverse the array, arr[] using the variable i and perform the following steps:If arr[i] is less than 25 and instances is greater than 1, divide the instances by 2 and increment i by 10.If arr[i] is greater than 60 and instances is less than or equal to 108, multiply instances by 2 and increment i by 10.Increment the index i by 1.
If arr[i] is less than 25 and instances is greater than 1, divide the instances by 2 and increment i by 10.
If arr[i] is greater than 60 and instances is less than or equal to 108, multiply instances by 2 and increment i by 10.
Increment the index i by 1.
After completing the above steps, print the number of instances as the result.
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
// C++ program for the above approach #include <bits/stdc++.h>using namespace std; // Function to find the number of// instances after compintionvoid finalInstances(int instances,                                  int arr[], int N){    int i = 0;     // Traverse the array, arr[]    while (i < N)    {                 // If current element is less        // than 25        if (arr[i] < 25 && instances > 1)        {                         // Divide instances by 2 and take ceil value            double temp = (instances / 2.0);            instances = (int)(ceil(temp));            i = i + 10;        }         // If the current element is        // greater than 60        else if (arr[i] > 60 &&                 instances <= (2*pow(10, 8)))        {                         // Double the instances            instances = instances * 2;            i = i + 10;        }        i = i + 1;    }         // Print the instances at the end    // of the traversal    cout << instances;} // Driver Codeint main(){    int instances = 2;    int arr[] = { 25, 23, 1, 2, 3, 4, 5,                  6, 7, 8, 9, 10, 76, 80 };                       int N = sizeof(arr) / sizeof(arr[0]);     // Function Call    finalInstances(instances, arr, N);} // This code is contributed by splevel62
// Java program for above approachclass GFG{ // Function to find the number of// instances after compintionpublic static void finalInstances(int instances,                                  int[] arr){    int i = 0;     // Traverse the array, arr[]    while (i < arr.length)    {                 // If current element is less        // than 25        if (arr[i] < 25 && instances > 1)        {                         // Divide instances by 2            instances = (instances / 2);            i = i + 10;        }         // If the current element is        // greater than 60        else if (arr[i] > 60 &&                 instances <= Math.pow(10, 8))        {                         // Double the instances            instances = instances * 2;            i = i + 10;        }        i = i + 1;    }         // Print the instances at the end    // of the traversal    System.out.println(instances);} // Driver Codepublic static void main(String args[]){    int instances = 2;    int[] arr = { 25, 23, 1, 2, 3, 4, 5,                  6, 7, 8, 9, 10, 76, 80 };     // Function Call    finalInstances(instances, arr);}} // This code is contributed by _saurabh_jaiswal
# Python program for the above approachfrom math import ceil # Function to find the number of# instances after completiondef finalInstances(instances, arr):    i = 0     # Traverse the array, arr[]    while i < len(arr):         # If current element is less        # than 25        if arr[i] < 25 and instances > 1:                       # Divide instances by 2            instances = ceil(instances / 2)            i += 10         # If the current element is        # greater than 60        elif arr[i] > 60 and instances <= 10**8:                       # Double the instances            instances *= 2            i += 10        i += 1     # Print the instances at the end    # of the traversal    print(instances) # Driver Codeinstances = 2 arr = [25, 23, 1, 2, 3, 4, 5,       6, 7, 8, 9, 10, 76, 80] # Function CallfinalInstances(instances, arr)
// C# program for above approachusing System; class GFG{ // Function to find the number of// instances after compintionstatic void finalInstances(int instances, int[] arr){        int i = 0;           // Traverse the array, arr[]        while (i < arr.Length) {             // If current element is less            // than 25            if (arr[i] < 25 && instances > 1) {                 // Divide instances by 2                instances = (instances / 2);                i = i + 10;            }             // If the current element is            // greater than 60            else if (arr[i] > 60 && instances <= Math.Pow(10, 8))            {                 // Double the instances                instances = instances * 2;                i = i + 10;             }            i = i + 1;         }        // Print the instances at the end        // of the traversal        Console.Write(instances);    }  // Driver Codestatic void Main(){    int instances = 2;     int[] arr = {25, 23, 1, 2, 3, 4, 5,        6, 7, 8, 9, 10, 76, 80};     // Function Call    finalInstances(instances, arr);}} // This code is contributed by sanjoy_62.
<script>          // JavaScript Program for the above approach         // Function to find the number of        // instances after completion        function finalInstances(instances, arr) {            let i = 0;             // Traverse the array, arr[]            while (i < arr.length) {                 // If current element is less                // than 25                if (arr[i] < 25 && instances > 1) {                     // Divide instances by 2                    instances = Math.ceil(instances / 2);                    i = i + 10;                }                 // If the current element is                // greater than 60                else if (arr[i] > 60 && instances <= Math.pow(10, 8))                {                     // Double the instances                    instances = instances * 2;                    i = i + 10;                 }                i = i + 1;             }            // Print the instances at the end            // of the traversal            document.write(instances);        }        // Driver Code        let instances = 2;         let arr = [25, 23, 1, 2, 3, 4, 5,            6, 7, 8, 9, 10, 76, 80];         // Function Call        finalInstances(instances, arr);     // This code is contributed by Potta Lokesh     </script>
2
 
Time Complexity: O(N)Auxiliary Space: O(1)
lokeshpotta20
sanjoy_62
_saurabh_jaiswal
splevel62
mishrapriyank17
Amazon
interview-preparation
Arrays
Mathematical
Amazon
Arrays
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Stack Data Structure (Introduction and Program)
Top 50 Array Coding Problems for Interviews
Introduction to Arrays
Multidimensional Arrays in Java
Linear Search
Program for Fibonacci numbers
C++ Data Types
Write a program to print all permutations of a given string
Set in C++ Standard Template Library (STL)
Program to find GCD or HCF of two numbers