# Minimise moves to reduce N to 0 using given operations

• Last Updated : 03 Dec, 2021

Given a number N, and some operations that can be performed, the task is to find the minimum number of moves to convert N to 0. In one move operation, one of the following can be performed:

• Increment or decrement the value of N by 1.
• Multiply the value of N by -1.
• Divide the value of N by 2 if N is even.
• Reduce the value of N to âˆšN if N is a perfect square.

Example:

Input: N = 50
Output: 6
Explanation: The moves performed are: 50 (/2) -> 25 (âˆš) -> 5 (- 1) -> 4 (/2) -> 2 (-1) -> 1 (-1) -> 0. Therefore, the required number of moves is 6 which is the minimum possible.

Input: N = 75
Output: 8

Approach: The given problem can be solved efficiently by using dynamic programming. The idea is to use hashing and breadth-first search on 0 till N is reached. Hashing is used so that the same number is not visited twice. The below steps can be followed to solve the problem:

• Use BFS by adding all the possible numbers that can be reached from 0 into a queue and also in a hashmap so that they are not visited again
• Return the number of moves calculated after reaching N.

Below is the implementation of the above approach:

## C++

 `// C++ code for the above approach``#include ``using` `namespace` `std;` `    ``class` `Node {``     ``public``:``        ``int` `val, moves;` `        ``// Constructor``        ``Node(``int` `v, ``int` `m)``        ``{` `            ``val = v;``            ``moves = m;``        ``}``    ``};` `    ``// Function to calculate``    ``// minimum number of moves``    ``// required to convert N to 0``    ``int` `minMoves(``int` `N)``    ``{` `        ``// Initialize a hashset``        ``// to mark the visited numbers``        ``set<``int``> set;` `        ``// Initialize a queue``        ``queue q ;` `        ``// Mark 0 as visited``        ``set.insert(0);` `        ``// Add 0 into the queue``        ``q.push(``new` `Node(0, 0));` `        ``// while N is not reached``        ``while` `(!q.empty()) {` `            ``// poll out current node``            ``Node *curr = q.front();``            ``q.pop();` `            ``// If N is reached``            ``if` `(curr->val == N) {` `                ``// Return the number of moves used``                ``return` `curr->moves;``            ``}` `            ``if` `(set.find(curr->val - 1)==set.end()) {` `                ``// Mark the number as visited``                ``set.insert(curr->val - 1);` `                ``// Add the number in the queue``                ``q.push(``new` `Node(curr->val - 1,``                               ``curr->moves + 1));``            ``}``            ``if` `(set.find(curr->val + 1)==set.end()) {` `                ``// Mark the number as visited``                ``set.insert(curr->val + 1);` `                ``// Add the number in the queue``                ``q.push(``new` `Node(curr->val + 1,``                               ``curr->moves + 1));``            ``}``            ``if` `(set.find(curr->val * 2)==set.end()) {` `                ``// Mark the number as visited``                ``set.insert(curr->val * 2);` `                ``// Add the number in the queue``                ``q.push(``new` `Node(curr->val * 2,``                               ``curr->moves + 1));``            ``}``            ``int` `sqr = curr->val * curr->val;``            ``if` `(set.find(sqr)==set.end()) {` `                ``// Mark the number as visited``                ``set.insert(sqr);` `                ``// Add the number in the queue``                ``q.push(``new` `Node(sqr,``                               ``curr->moves + 1));``            ``}``            ``if` `(set.find(-curr->val)==set.end()) {` `                ``// Mark the number as visited``                ``set.insert(-curr->val);` `                ``// Add the number in the queue``                ``q.push(``new` `Node(-curr->val,``                               ``curr->moves + 1));``            ``}``        ``}` `        ``return` `-1;``    ``}` `    ``// Driver code``     ``int` `main()``    ``{` `        ``int` `N = 50;` `        ``// Call the function``        ``// and print the answer``        ``cout<<(minMoves(N));``    ``}` `// This code is contributed by Potta Lokesh`

## Java

 `// Java implementation for the above approach` `import` `java.io.*;``import` `java.util.*;` `class` `GFG {` `    ``static` `class` `Node {` `        ``int` `val, moves;` `        ``// Constructor``        ``public` `Node(``int` `val, ``int` `moves)``        ``{` `            ``this``.val = val;``            ``this``.moves = moves;``        ``}``    ``}` `    ``// Function to calculate``    ``// minimum number of moves``    ``// required to convert N to 0``    ``public` `static` `int` `minMoves(``int` `N)``    ``{` `        ``// Initialize a hashset``        ``// to mark the visited numbers``        ``Set set = ``new` `HashSet<>();` `        ``// Initialize a queue``        ``Queue q = ``new` `LinkedList<>();` `        ``// Mark 0 as visited``        ``set.add(``0``);` `        ``// Add 0 into the queue``        ``q.add(``new` `Node(``0``, ``0``));` `        ``// while N is not reached``        ``while` `(!q.isEmpty()) {` `            ``// poll out current node``            ``Node curr = q.poll();` `            ``// If N is reached``            ``if` `(curr.val == N) {` `                ``// Return the number of moves used``                ``return` `curr.moves;``            ``}` `            ``if` `(!set.contains(curr.val - ``1``)) {` `                ``// Mark the number as visited``                ``set.add(curr.val - ``1``);` `                ``// Add the number in the queue``                ``q.add(``new` `Node(curr.val - ``1``,``                               ``curr.moves + ``1``));``            ``}``            ``if` `(!set.contains(curr.val + ``1``)) {` `                ``// Mark the number as visited``                ``set.add(curr.val + ``1``);` `                ``// Add the number in the queue``                ``q.add(``new` `Node(curr.val + ``1``,``                               ``curr.moves + ``1``));``            ``}``            ``if` `(!set.contains(curr.val * ``2``)) {` `                ``// Mark the number as visited``                ``set.add(curr.val * ``2``);` `                ``// Add the number in the queue``                ``q.add(``new` `Node(curr.val * ``2``,``                               ``curr.moves + ``1``));``            ``}``            ``int` `sqr = curr.val * curr.val;``            ``if` `(!set.contains(sqr)) {` `                ``// Mark the number as visited``                ``set.add(sqr);` `                ``// Add the number in the queue``                ``q.add(``new` `Node(sqr,``                               ``curr.moves + ``1``));``            ``}``            ``if` `(!set.contains(-curr.val)) {` `                ``// Mark the number as visited``                ``set.add(-curr.val);` `                ``// Add the number in the queue``                ``q.add(``new` `Node(-curr.val,``                               ``curr.moves + ``1``));``            ``}``        ``}` `        ``return` `-``1``;``    ``}` `    ``// Driver code``    ``public` `static` `void` `main(String[] args)``    ``{` `        ``int` `N = ``50``;` `        ``// Call the function``        ``// and print the answer``        ``System.out.println(minMoves(N));``    ``}``}`

## Python3

 `# Python code for the above approach``from` `queue ``import` `Queue` `class` `Node:` `    ``# Constructor``    ``def` `__init__(``self``, v, m):``        ``self``.val ``=` `v``        ``self``.moves ``=` `m` `# Function to calculate``# minimum number of moves``# required to convert N to 0``def` `minMoves(N):` `    ``# Initialize a hashset``    ``# to mark the visited numbers``    ``_set ``=` `set``()` `    ``# Initialize a queue``    ``q ``=` `Queue()` `    ``# Mark 0 as visited``    ``_set.add(``0``)` `    ``# Add 0 into the queue``    ``q.put(Node(``0``, ``0``))` `    ``# while N is not reached``    ``while` `(q.qsize):` `        ``# poll out current node``        ``curr ``=` `q.queue[``0``]``        ``q.get()` `        ``# If N is reached``        ``if` `(curr.val ``=``=` `N):` `            ``# Return the number of moves used``            ``return` `curr.moves` `        ``if` `(``not` `(curr.val ``-` `1``) ``in` `_set):` `            ``# Mark the number as visited``            ``_set.add(curr.val ``-` `1``)` `            ``# Add the number in the queue``            ``q.put(Node(curr.val ``-` `1``, curr.moves ``+` `1``))` `        ``if` `(``not` `(curr.val ``+` `1``) ``in` `_set):` `            ``# Mark the number as visited``            ``_set.add(curr.val ``+` `1``)` `            ``# Add the number in the queue``            ``q.put(Node(curr.val ``+` `1``, curr.moves ``+` `1``))` `        ``if` `(``not` `(curr.val ``*` `2``) ``in` `_set):` `            ``# Mark the number as visited``            ``_set.add(curr.val ``*` `2``)` `            ``# Add the number in the queue``            ``q.put(Node(curr.val ``*` `2``, curr.moves ``+` `1``))` `        ``sqr ``=` `curr.val ``*` `curr.val``        ``if` `(``not` `sqr ``in` `_set):` `            ``# Mark the number as visited``            ``_set.add(sqr)` `            ``# Add the number in the queue``            ``q.put(Node(sqr, curr.moves ``+` `1``))` `        ``if` `(``not` `(``-``curr.val) ``in` `_set):` `            ``# Mark the number as visited``            ``_set.add(``-``curr.val)` `            ``# Add the number in the queue``            ``q.put(Node(``-``curr.val, curr.moves ``+` `1``))` `    ``return` `-``1` `# Driver code``N ``=` `50` `# Call the function``# and print the answer``print``((minMoves(N)))` `# This code is contributed by gfgking`

## Javascript

 ``
Output
`6`

Time Complexity: O(log N)
Auxiliary Space: O(K*log N), where K is the possible operations allowed

My Personal Notes arrow_drop_up