# Minimum number of sum and modulo operations using given numbers to reach target

• Last Updated : 20 Jul, 2022

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

 `// C++ implementation of the above approach` `#include ``using` `namespace` `std;` `// Function to find the minimum moves``// to reach K from N``int` `minPushes(``int` `N, ``int` `K, vector<``int``> arr)``{` `    ``// Initialization of dp vector``    ``vector<``int``> dp(100000, -1);` `    ``// dp[i] = minimum pushes``    ``// required to reach i``    ``dp[N] = 0;` `    ``// Traversing through the buttons``    ``for` `(``int` `i = 0; i < arr.size(); i++) {` `        ``// Iterating through all the positions``        ``for` `(``int` `xx = 0; xx < 100000; xx++) {``            ``int` `x = xx;` `            ``// If not visited``            ``if` `(dp[x] == -1)``                ``continue``;` `            ``// Next status of lock``            ``int` `next = (x + arr[i]) % 100000;``            ``while` `(dp[next] == -1``                   ``|| dp[next] > dp[x] + 1) {``                ``dp[next] = dp[x] + 1;` `                ``// Advance to next state``                ``x = next;``                ``next = (next + arr[i]) % 100000;``            ``}``        ``}``    ``}` `    ``// Return the final dp[target]``    ``return` `dp[K];``}` `// Driver function``int` `main()``{``    ``// Given Input``    ``int` `N = 99880, K = 89;``    ``vector<``int``> arr{ 100, 3 };` `    ``// Function Call``    ``cout << minPushes(N, K, arr);` `    ``return` `0;``}`

## Java

 `// Java implementation of the above approach``class` `GFG{` `// Function to find the minimum moves``// to reach K from N``static` `int` `minPushes(``int` `N, ``int` `K, ``int``[] arr)``{` `    ``// Initialization of dp vector``    ``int``[] dp = ``new` `int``[``100000``];``    ``for` `(``int` `i = ``0``; i < dp.length; i++)``        ``dp[i] = -``1``;``  ` `    ``// dp[i] = minimum pushes``    ``// required to reach i``    ``dp[N] = ``0``;` `    ``// Traversing through the buttons``    ``for` `(``int` `i = ``0``; i < arr.length; i++) {` `        ``// Iterating through all the positions``        ``for` `(``int` `xx = ``0``; xx < ``100000``; xx++) {``            ``int` `x = xx;` `            ``// If not visited``            ``if` `(dp[x] == -``1``)``                ``continue``;` `            ``// Next status of lock``            ``int` `next = (x + arr[i]) % ``100000``;``            ``while` `(dp[next] == -``1``                   ``|| dp[next] > dp[x] + ``1``) {``                ``dp[next] = dp[x] + ``1``;` `                ``// Advance to next state``                ``x = next;``                ``next = (next + arr[i]) % ``100000``;``            ``}``        ``}``    ``}` `    ``// Return the final dp[target]``    ``return` `dp[K];``}` `// Driver function``public` `static` `void` `main(String[] args)``{``    ``// Given Input``    ``int` `N = ``99880``, K = ``89``;``    ``int``[] arr = { ``100``, ``3` `};` `    ``// Function Call``    ``System.out.print(minPushes(N, K, arr));` `}``}` `// This code is contributed by shikhasingrajput`

## Python3

 `# Python program for the above approach` `# Function to find the minimum moves``# to reach K from N``def` `minPushes(N, K, arr):` `    ``# Initialization of dp vector``    ``dp ``=` `[``-``1``] ``*` `100000` `    ``# dp[i] = minimum pushes``    ``# required to reach i``    ``dp[N] ``=` `0` `    ``# Traversing through the buttons``    ``for` `i ``in` `range``(``len``(arr)):` `        ``# Iterating through all the positions``        ``for` `xx ``in` `range``(``100000``):``            ``x ``=` `xx` `            ``# If not visited``            ``if` `(dp[x] ``=``=` `-``1``) :``                ``continue` `            ``# Next status of lock``            ``next` `=` `(x ``+` `arr[i]) ``%` `100000``            ``while` `(dp[``next``] ``=``=` `-``1``                   ``or` `dp[``next``] > dp[x] ``+` `1``) :``                ``dp[``next``] ``=` `dp[x] ``+` `1` `                ``# Advance to next state``                ``x ``=` `next``                ``next` `=` `(``next` `+` `arr[i]) ``%` `100000``            ` `    ``# Return the final dp[target]``    ``return` `dp[K]` `# Driver function` `# Given Input``N ``=` `99880``K ``=` `89``arr ``=` `[ ``100``, ``3` `]` `# Function Call``print``(minPushes(N, K, arr))` `# This code is contributed by sanjoy_62.`

## C#

 `// C# implementation of the above approach``using` `System;` `public` `class` `GFG{` `// Function to find the minimum moves``// to reach K from N``static` `int` `minPushes(``int` `N, ``int` `K, ``int``[] arr)``{` `    ``// Initialization of dp vector``    ``int``[] dp = ``new` `int``[100000];``    ``for` `(``int` `i = 0; i < dp.Length; i++)``        ``dp[i] = -1;``  ` `    ``// dp[i] = minimum pushes``    ``// required to reach i``    ``dp[N] = 0;` `    ``// Traversing through the buttons``    ``for` `(``int` `i = 0; i < arr.Length; i++) {` `        ``// Iterating through all the positions``        ``for` `(``int` `xx = 0; xx < 100000; xx++) {``            ``int` `x = xx;` `            ``// If not visited``            ``if` `(dp[x] == -1)``                ``continue``;` `            ``// Next status of lock``            ``int` `next = (x + arr[i]) % 100000;``            ``while` `(dp[next] == -1``                   ``|| dp[next] > dp[x] + 1) {``                ``dp[next] = dp[x] + 1;` `                ``// Advance to next state``                ``x = next;``                ``next = (next + arr[i]) % 100000;``            ``}``        ``}``    ``}` `    ``// Return the readonly dp[target]``    ``return` `dp[K];``}` `// Driver function``public` `static` `void` `Main(String[] args)``{``    ``// Given Input``    ``int` `N = 99880, K = 89;``    ``int``[] arr = { 100, 3 };` `    ``// Function Call``    ``Console.Write(minPushes(N, K, arr));` `}``}` `// This code is contributed by 29AjayKumar`

## Javascript

 ``

Output

`5`

Time Complexity: O(N*M)
Auxiliary Space: O(N), since N extra space has been taken.

My Personal Notes arrow_drop_up