Given an integer k and an array op[], in a single operation op[0] will be added to k and then in the second operation k = k + op[1] and so on in a circular manner until k > 0. The task is to print the operation number in which k will be reduced to ? 0. If it impossible to reduce k with the given operations then print -1.
Examples:
Input: op[] = {-60, 10, -100}, k = 100
Output: 3
Operation 1: 100 – 60 = 40
Operation 2: 40 + 10 = 50
Operation 3: 50 – 100 = -50
Input: op[] = {1, 1, -1}, k = 10
Output: -1
Input: op[] = {-60, 65, -1, 14, -25}, k = 100000
Output: 71391
Approach: Count the number of times all the operations can be performed on the number k without actually reducing it to get the result. Then update count = times * n where n is the number of operations. Now, for the remaining operations perform each of the operation one by one and increment count. The first operation when k is reduced to ? 0, print the count.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int operations( int op[], int n, int k)
{
int i, count = 0;
int nVal = 0;
int minimum = INT_MAX;
for (i = 0; i < n; i++)
{
nVal += op[i];
minimum = min(minimum , nVal);
if ((k + nVal) <= 0)
return (i + 1);
}
if (nVal >= 0)
return -1;
int times = (k - abs (minimum )) / abs (nVal);
k = (k - (times * abs (nVal)));
count = (times * n);
while (k > 0) {
for (i = 0; i < n; i++) {
k = k + op[i];
count++;
if (k <= 0)
break ;
}
}
return count;
}
int main() {
int op[] = { -60, 65, -1, 14, -25 };
int n = sizeof (op)/ sizeof (op[0]);
int k = 100000;
cout << operations(op, n, k) << endl;
}
|
Java
class GFG {
static int operations( int op[], int n, int k)
{
int i, count = 0 ;
int nVal = 0 ;
int min = Integer.MAX_VALUE;
for (i = 0 ; i < n; i++) {
nVal += op[i];
min = Math.min(min, nVal);
if ((k + nVal) <= 0 )
return (i + 1 );
}
if (nVal >= 0 )
return - 1 ;
int times = (k - Math.abs(min)) / Math.abs(nVal);
k = (k - (times * Math.abs(nVal)));
count = (times * n);
while (k > 0 ) {
for (i = 0 ; i < n; i++) {
k = k + op[i];
count++;
if (k <= 0 )
break ;
}
}
return count;
}
public static void main(String[] args)
{
int op[] = { - 60 , 65 , - 1 , 14 , - 25 };
int n = op.length;
int k = 100000 ;
System.out.print(operations(op, n, k));
}
}
|
Python3
def operations(op, n, k):
i, count = 0 , 0
nVal = 0
minimum = 10 * * 9
for i in range (n):
nVal + = op[i]
minimum = min (minimum , nVal)
if ((k + nVal) < = 0 ):
return (i + 1 )
if (nVal > = 0 ):
return - 1
times = (k - abs (minimum )) / / abs (nVal)
k = (k - (times * abs (nVal)))
count = (times * n)
while (k > 0 ):
for i in range (n):
k = k + op[i]
count + = 1
if (k < = 0 ):
break
return count
op = [ - 60 , 65 , - 1 , 14 , - 25 ]
n = len (op)
k = 100000
print (operations(op, n, k))
|
C#
using System;
class GFG
{
static int operations( int []op, int n, int k)
{
int i, count = 0;
int nVal = 0;
int min = int .MaxValue;
for (i = 0; i < n; i++)
{
nVal += op[i];
min = Math.Min(min, nVal);
if ((k + nVal) <= 0)
return (i + 1);
}
if (nVal >= 0)
return -1;
int times = (k - Math.Abs(min)) / Math.Abs(nVal);
k = (k - (times * Math.Abs(nVal)));
count = (times * n);
while (k > 0)
{
for (i = 0; i < n; i++)
{
k = k + op[i];
count++;
if (k <= 0)
break ;
}
}
return count;
}
static void Main()
{
int []op = { -60, 65, -1, 14, -25 };
int n = op.Length;
int k = 100000;
Console.WriteLine(operations(op, n, k));
}
}
|
PHP
<?php
function operations( $op , $n , $k )
{
$count = 0;
$nVal = 0;
$minimum = PHP_INT_MAX;
for ( $i = 0; $i < $n ; $i ++)
{
$nVal += $op [ $i ];
$minimum = min( $minimum , $nVal );
if (( $k + $nVal ) <= 0)
return ( $i + 1);
}
if ( $nVal >= 0)
return -1;
$times = round (( $k - abs ( $minimum )) /
abs ( $nVal ));
$k = ( $k - ( $times * abs ( $nVal )));
$count = ( $times * $n );
while ( $k > 0)
{
for ( $i = 0; $i < $n ; $i ++)
{
$k = $k + $op [ $i ];
$count ++;
if ( $k <= 0)
break ;
}
}
return $count ;
}
$op = array (-60, 65, -1, 14, -25 );
$n = sizeof( $op );
$k = 100000;
echo operations( $op , $n , $k );
?>
|
Javascript
<script>
function operations(op,n,k)
{
let i, count = 0;
let nVal = 0;
let min = Number.MAX_VALUE;
for (i = 0; i < n; i++) {
nVal += op[i];
min = Math.min(min, nVal);
if ((k + nVal) <= 0)
return (i + 1);
}
if (nVal >= 0)
return -1;
let times = Math.floor((k - Math.abs(min)) / Math.abs(nVal));
k = (k - (times * Math.abs(nVal)));
count = (times * n);
while (k > 0) {
for (i = 0; i < n; i++) {
k = k + op[i];
count++;
if (k <= 0)
break ;
}
}
return count;
}
let op=[-60, 65, -1, 14, -25];
let n = op.length;
let k = 100000;
document.write(operations(op, n, k));
</script>
|
Time Complexity: O(n*k)
Auxiliary Space: O(1)