Minimum number of sum and modulo operations using given numbers to reach target
Given a number N, an array arr[], and a target number K, the task is to find minimum number of moves to reach K by choosing any array element and changing N to (N + arr[i]) mod 100000 in each move.
Examples:
Input: N = 99880, K = 89, arr = {100, 3}
Output: 5
Explanation: Number “100” is used 2 times and the number “3” is pushed 3 times, resulting in K from N
Input: N = 10000, K = 10004, arr = {1}
Output: 4
Approach: The above problem can be solved by using Dynamic Programming because it has overlapping subproblems and optimal substructure. Initialize the dp table where dp[i] stores the minimum moves required to reach the status i on N and dp[target] will be the required answer. From each arr[i], find all reachable states and minimize the dp value. Follow the steps below for the approach:
- Initialize an array, say, dp[ ] to store the minimum moves required to reach the status i.
- Iterate over the array arr and check for each position of the current number N as x:
- if dp[x] equals -1, then continue.
- else, iterate a while loop till dp[next] equals -1 or dp[next] is greater than dp[x] + 1 and update next as (x+buttons[i])%100000 and dp[next] as dp[x]+ 1.
- Finally, return the value of dp[target].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minPushes( int N, int K, vector< int > arr)
{
vector< int > dp(100000, -1);
dp[N] = 0;
for ( int i = 0; i < arr.size(); i++) {
for ( int xx = 0; xx < 100000; xx++) {
int x = xx;
if (dp[x] == -1)
continue ;
int next = (x + arr[i]) % 100000;
while (dp[next] == -1
|| dp[next] > dp[x] + 1) {
dp[next] = dp[x] + 1;
x = next;
next = (next + arr[i]) % 100000;
}
}
}
return dp[K];
}
int main()
{
int N = 99880, K = 89;
vector< int > arr{ 100, 3 };
cout << minPushes(N, K, arr);
return 0;
}
|
Java
class GFG{
static int minPushes( int N, int K, int [] arr)
{
int [] dp = new int [ 100000 ];
for ( int i = 0 ; i < dp.length; i++)
dp[i] = - 1 ;
dp[N] = 0 ;
for ( int i = 0 ; i < arr.length; i++) {
for ( int xx = 0 ; xx < 100000 ; xx++) {
int x = xx;
if (dp[x] == - 1 )
continue ;
int next = (x + arr[i]) % 100000 ;
while (dp[next] == - 1
|| dp[next] > dp[x] + 1 ) {
dp[next] = dp[x] + 1 ;
x = next;
next = (next + arr[i]) % 100000 ;
}
}
}
return dp[K];
}
public static void main(String[] args)
{
int N = 99880 , K = 89 ;
int [] arr = { 100 , 3 };
System.out.print(minPushes(N, K, arr));
}
}
|
Python3
def minPushes(N, K, arr):
dp = [ - 1 ] * 100000
dp[N] = 0
for i in range ( len (arr)):
for xx in range ( 100000 ):
x = xx
if (dp[x] = = - 1 ) :
continue
next = (x + arr[i]) % 100000
while (dp[ next ] = = - 1
or dp[ next ] > dp[x] + 1 ) :
dp[ next ] = dp[x] + 1
x = next
next = ( next + arr[i]) % 100000
return dp[K]
N = 99880
K = 89
arr = [ 100 , 3 ]
print (minPushes(N, K, arr))
|
C#
using System;
public class GFG{
static int minPushes( int N, int K, int [] arr)
{
int [] dp = new int [100000];
for ( int i = 0; i < dp.Length; i++)
dp[i] = -1;
dp[N] = 0;
for ( int i = 0; i < arr.Length; i++) {
for ( int xx = 0; xx < 100000; xx++) {
int x = xx;
if (dp[x] == -1)
continue ;
int next = (x + arr[i]) % 100000;
while (dp[next] == -1
|| dp[next] > dp[x] + 1) {
dp[next] = dp[x] + 1;
x = next;
next = (next + arr[i]) % 100000;
}
}
}
return dp[K];
}
public static void Main(String[] args)
{
int N = 99880, K = 89;
int [] arr = { 100, 3 };
Console.Write(minPushes(N, K, arr));
}
}
|
Javascript
<script>
function minPushes(N, K, arr) {
let dp = new Array(100000).fill(-1);
dp[N] = 0;
for (let i = 0; i < arr.length; i++) {
for (let xx = 0; xx < 100000; xx++) {
let x = xx;
if (dp[x] == -1)
continue ;
let next = (x + arr[i]) % 100000;
while (dp[next] == -1
|| dp[next] > dp[x] + 1) {
dp[next] = dp[x] + 1;
x = next;
next = (next + arr[i]) % 100000;
}
}
}
return dp[K];
}
let N = 99880, K = 89;
let arr = [100, 3];
document.write(minPushes(N, K, arr));
</script>
|
Time Complexity: O(N*M)
Auxiliary Space: O(N), since N extra space has been taken.
Last Updated :
20 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...