# Find instances at end of time frame after auto scaling

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 exceed2* 10.^{8}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 10^{8}, 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 10^{8}, multiply**instances**by 2 and increment**i**by**10**. - Increment the index
**i**by**1**.

- If
- After completing the above steps, print the number of
**instances**as the result.

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` `// instances after compintion` `void` `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 Code` `int` `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

`// Java program for above approach` `class` `GFG{` `// Function to find the number of` `// instances after compintion` `public` `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 Code` `public` `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` |

## Python3

`# Python program for the above approach` `from` `math ` `import` `ceil` `# Function to find the number of` `# instances after completion` `def` `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 Code` `instances ` `=` `2` `arr ` `=` `[` `25` `, ` `23` `, ` `1` `, ` `2` `, ` `3` `, ` `4` `, ` `5` `,` ` ` `6` `, ` `7` `, ` `8` `, ` `9` `, ` `10` `, ` `76` `, ` `80` `]` `# Function Call` `finalInstances(instances, arr)` |

## C#

`// C# program for above approach` `using` `System;` `class` `GFG{` `// Function to find the number of` `// instances after compintion` `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` ` ` `Console.Write(instances);` ` ` `}` `// Driver Code` `static` `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.` |

## Javascript

`<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>` |

**Output:**

2

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**

In case you wish to attend **live classes **with experts, please refer **DSA Live Classes for Working Professionals **and **Competitive Programming Live for Students**.