Minimize cost to reach the end of given Binary Array using jump of K length after performing given operations
Last Updated :
11 Nov, 2021
Given a binary array arr[] of N integers and an integer P, the task is to find the minimum cost to reach the end of the array from Pth index using jumps of length K. A jump to index i is valid if arr[i] = 1. The array can be modified using the following operations:
- Replace an index having a value 0 to 1. The cost of this operation is X.
- Delete the integer at index P. The cost of this operation is Y.
Example:
Input: arr[] = {0, 0, 0, 1, 1, 0, 0, 1, 1, 1}, P = 6, K = 2, X = 1, Y = 2
Output: 1
Explanation: In 1st operation, replace the value at index 6 to 1. Hence, arr[] = {0, 0, 0, 1, 1, 1, 0, 1, 1, 1}. Initially, the current index = P = 6. Jump from P to P + K => from 6 => 8. Again jump to the next possible index i.e, 8 => 10, which is the end of the array.
Input: arr[] = {0, 1, 0, 0, 0, 1, 0}, P = 4, K = 1, X = 2, Y = 1
Output: 4
Approach: The given problem can be solved based on the following observations:
- For a given P, check if indices P, P+K, P+2K… have their value as 1. If not, then replace them with 1 and maintain their count. Hence, the final cost = count * X.
- Upon applying the second operation i times, the starting index will become P+i. Hence the final cost = (i * Y) + (count * X).
Hence, the given problem can be solved by iterating over all possible values of i in the range [1, N-P) and calculating the cost at each step. It can be done by maintaining an array zeroes[], where zeroes[i] stores the frequency of 0’s at indices i, i+K, i+2K…
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minimumCost( int arr[], int N,
int P, int K, int X,
int Y)
{
P = P - 1;
vector< int > zeros(N, 0);
for ( int i = 0; i < N; i++) {
if (arr[i] == 0) {
zeros[i]++;
}
}
for ( int i = N - K - 1; i >= 0; i--) {
zeros[i] += zeros[i + K];
}
int cost = INT_MAX;
for ( int i = P; i < N; i++) {
cost = min(cost,
((i - P) * Y)
+ (zeros[i] * X));
}
return cost;
}
int main()
{
int arr[] = { 0, 1, 0, 0, 0, 1, 0 };
int P = 4;
int K = 1;
int X = 2;
int Y = 1;
int N = sizeof (arr) / sizeof (arr[0]);
cout << minimumCost(arr, N, P, K, X, Y);
return 0;
}
|
Java
public class GFG {
static int minimumCost( int arr[], int N,
int P, int K, int X,
int Y)
{
P = P - 1 ;
int zeros[] = new int [N] ;
for ( int i = 0 ; i < N; i++) {
if (arr[i] == 0 ) {
zeros[i]++;
}
}
for ( int i = N - K - 1 ; i >= 0 ; i--) {
zeros[i] += zeros[i + K];
}
int cost = Integer.MAX_VALUE;
for ( int i = P; i < N; i++) {
cost = Math.min(cost,
((i - P) * Y)
+ (zeros[i] * X));
}
return cost;
}
public static void main (String[] args) {
int arr[] = { 0 , 1 , 0 , 0 , 0 , 1 , 0 };
int P = 4 ;
int K = 1 ;
int X = 2 ;
int Y = 1 ;
int N = arr.length;
System.out.println(minimumCost(arr, N, P, K, X, Y));
}
}
|
Python3
import sys
def minimumCost(arr, N, P, K, X, Y) :
P = P - 1 ;
zeros = [ 0 ] * N ;
for i in range (N) :
if (arr[i] = = 0 ) :
zeros[i] + = 1 ;
for i in range ( N - K - 1 , - 1 , - 1 ) :
zeros[i] + = zeros[i + K];
cost = sys.maxsize;
for i in range (P, N) :
cost = min (cost,((i - P) * Y) + (zeros[i] * X));
return cost;
if __name__ = = "__main__" :
arr = [ 0 , 1 , 0 , 0 , 0 , 1 , 0 ];
P = 4 ;
K = 1 ;
X = 2 ;
Y = 1 ;
N = len (arr);
print (minimumCost(arr, N, P, K, X, Y));
|
C#
using System;
public class GFG
{
static int minimumCost( int [] arr, int N, int P, int K, int X, int Y)
{
P = P - 1;
int [] zeros = new int [N];
for ( int i = 0; i < N; i++)
{
if (arr[i] == 0)
{
zeros[i]++;
}
}
for ( int i = N - K - 1; i >= 0; i--)
{
zeros[i] += zeros[i + K];
}
int cost = int .MaxValue;
for ( int i = P; i < N; i++)
{
cost = Math.Min(cost,
((i - P) * Y)
+ (zeros[i] * X));
}
return cost;
}
public static void Main()
{
int [] arr = { 0, 1, 0, 0, 0, 1, 0 };
int P = 4;
int K = 1;
int X = 2;
int Y = 1;
int N = arr.Length;
Console.WriteLine(minimumCost(arr, N, P, K, X, Y));
}
}
|
Javascript
<script>
function minimumCost(arr, N, P, K, X, Y)
{
P = P - 1;
var zeros = Array(N).fill(0);
for ( var i = 0; i < N; i++) {
if (arr[i] == 0) {
zeros[i]++;
}
}
for ( var i = N - K - 1; i >= 0; i--) {
zeros[i] += zeros[i + K];
}
var cost = 1000000000;
for ( var i = P; i < N; i++) {
cost = Math.min(cost,
((i - P) * Y)
+ (zeros[i] * X));
}
return cost;
}
var arr = [ 0, 1, 0, 0, 0, 1, 0 ];
var P = 4;
var K = 1;
var X = 2;
var Y = 1;
var N = arr.length;
document.write(minimumCost(arr, N, P, K, X, Y));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...