Minimum 1s to lend power to make whole array powerful using C++


We are given a binary array which can store digits 1’s and 0’s of any given size and an integer variable let’s say, base. The task is to calculate the minimum 1’s that can lend power to other elements of a binary array such that the entire array becomes powerful. An element can lend power to its adjacent element or any other elements within the distance less than base.

Let us see various input output scenarios for this -

In − int arr[] = {1, 1, 0, 1, 1, 0, 1}, int base = 7

Out −Minimum 1s to lend power to make whole array powerful are: 1

Explanation −we are given a binary array of size 7 and a value of base as 7 which means the first encountered digit 1 can lend power to the entire array. Therefore, digit 1 at arr[1] can lend power to all other elements of an array.

In −int arr[] = {1, 1, 0, 1, 1, 0, 1}, int base = 3

Out −Minimum 1s to lend power to make whole array powerful are: 2

Explanation − we are given a binary array of size 7 and a value of base as 2 which means the first encountered digit 1 can lend power to the next two elements of an array. Therefore, digit 1 at arr[2] can lend power to the next two elements of an array and another one at arr[5] can lend power to the next two elements of an array which makes the entire array powerful.

In −int arr[] = {1, 1, 0, 1, 1, 0, 1}, int base = 1

Out −Impossible to make entire array powerful

Explanation −we are given a binary array of size 7 and a value of base as 1 which means the first encountered digit 1 can lend power to the next single element of an array. Therefore, it is impossible to make the entire array powerful.

Approach used in the below program is as follows −

  • Input a binary array of any given size and an integer variable let’s say, base

  • Calculate the size of an array and declare a variable let's say, val of integer type

  • Set the val with the call to a function that will return minimum 1’s required to create a powerful array and if it's not possible then it will return a -1 which will further display an error message.

  • Inside the function Lend_Power(int arr[], int size, int base)

    • Declare an array of integer type values with the size of a binary array.

    • Declare a temporary variable as temp and set it to -1 and count and set it to 0.

    • Start loop FOR from i to 0 till the size of an array. Inside the loop, Check IF arr[i] equals 1 then set temp as i and set arr_2[i] as temp

    • Start loop FOR from 0 till size of an array and set reset_base as i + base - 1 and reset_size as size - 1. Set a variable set as arr_2[min(reset_base, reset_size)].

    • Check IF set = -1 OR set + base <= i then return -1

    • Set variable ‘i’ as set + base.

  • Return count.

Example

#include<bits/stdc++.h>
using namespace std;

int Lend_Power(int arr[], int size, int base)
{
   int arr_2[size];
   int temp = -1;
   int count = 0;
   for(int i = 0; i < size; i++)
   {
      if(arr[i] == 1)
      {
         temp = i;
      }
      arr_2[i] = temp;
   }
   for(int i = 0; i < size;)
   {
      int reset_base = i + base - 1;
      int reset_size = size - 1;

      int set = arr_2[min(reset_base, reset_size)];
      if(set == -1 || set + base <= i)
      {
         return -1;
      }
      i = set + base;
      count++;
   }
   return count;
}
int main()
{
   int arr[] = {1, 1, 0, 1, 1, 0, 1};
   int base = 2;
   int size = sizeof(arr) / sizeof(arr[0]);
   int val = Lend_Power(arr, size, base);
   if(val == -1)
   {
      cout<<"Impossible to make entire array powerful";
   }
   else
   {
      cout<<"Minimum 1s to lend power to make whole array powerful are: "<<val;
   }
   return 0;
}

Output

If we run the above code it will generate the following Out

Minimum 1s to lend power to make whole array powerful are: 3

Updated on: 21-Oct-2021

39 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements