# Down to Zero II

### Problem Statement :

```You are given Q queries. Each query consists of a single number N. You can perform any of the  2 operations N on  in each move:

1: If we take 2 integers  a and b  where , N = a * b , then we can change  N = max( a, b )

2: Decrease the value of N by 1.

Determine the minimum number of moves required to reduce the value of  N to 0.

Input Format

The first line contains the integer Q.
The next Q lines each contain an integer, N.

Output Format

Output Q lines. Each line containing the minimum number of moves required to reduce the value of N to 0 .```

### Solution :

```                            ```Solution in C :

In C ++ :

#include <bits/stdc++.h>
using namespace std;

int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int test;
cin >> test;
while (test--){
int n ;
cin >> n ;
int steps = 0;
if (n==0){
cout << 0 << endl;
continue;
}
if (n==1){
cout << 1 << endl;
continue;
}
vector<int> dist(n+1,0);
queue<int> q;
q.push(n) ;
dist[n] = 1 ;
while (1){
int element = q.front();
q.pop();
if(element == 2){
cout << dist + 1 << endl;
break ;
}
if (dist[element-1] == 0 ){
dist [element-1] = dist[element]+1;
q.push(element-1);
}
for (int i=2; i*i<=element; i++){
if (element%i == 0){
int maxfrac = element/i;
if (dist[maxfrac] == 0) dist [maxfrac] = dist[element] + 1, q.push(maxfrac);
}
}
}
}
return 0;
}

In Java :

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {
private static int MAXSZ = (int)(1E6+1);
private static int[] dp;

private static int minimumMove(int n) {
if(n == 0) return 0;
if(dp[n] != -1) return dp[n];

int minMove = Integer.MAX_VALUE;
int sq = (int) Math.sqrt(n);

for(int i = 2; i <= sq; i++) {
if(n % i == 0) {
minMove = Math.min(minMove, 1+minimumMove(n/i));
}
}
minMove = Math.min(minMove, 1+ minimumMove(n-1));
return (dp[n] = minMove);
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

dp = new int[MAXSZ];
Arrays.fill(dp, -1);
dp = 0; dp = 1; dp = 2;

int Q = sc.nextInt();
for(int q = 1; q <= Q; q++) {
int n = sc.nextInt();
System.out.println(minimumMove(n));
}
}
}

In C :

#include <stdio.h>
#include <stdio.h>
#include <string.h>
unsigned long long min(unsigned long long x, unsigned long long y);
unsigned long long ans;

int main(){
int Q,N,i,j;
memset(ans,-1,sizeof(ans));
ans=0;
for(i=0;i<1000000;i++){
ans[i+1]=min(ans[i+1],ans[i]+1);
for(j=2;j<=i && i*(unsigned long long)j<1000001;j++)
ans[i*j]=min(ans[i*j],ans[i]+1);
}
scanf("%d",&Q);
while(Q--){
scanf("%d",&N);
printf("%llu\n",ans[N]);
}
return 0;
}
unsigned long long min(unsigned long long x, unsigned long long y){
return (x>y)?y:x;
}

In Python3 :

import math

import time
start = time.time()

def Sol( N ):
if N == 0: return 0
Q = [ (N,0) ]
setQ = [ 0 ] * N
while Q:
N, steps = Q.pop(0)
if N == 1: return steps+1
div = int(math.sqrt( N ))
while div > 1:
if N % div == 0 and not setQ[N // div]:
Q.append( (N // div, steps+1) )
setQ[ N // div ] = 1
div -= 1
if not setQ[N-1]:
Q.append( (N-1, steps+1) )
setQ[ N-1 ] = 1

Q = int(input())
for _ in range(Q):
N = int(input())
print(Sol(N))```
```

## Merge two sorted linked lists

This challenge is part of a tutorial track by MyCodeSchool Given pointers to the heads of two sorted linked lists, merge them into a single, sorted linked list. Either head pointer may be null meaning that the corresponding list is empty. Example headA refers to 1 -> 3 -> 7 -> NULL headB refers to 1 -> 2 -> NULL The new list is 1 -> 1 -> 2 -> 3 -> 7 -> NULL. Function Description C

## Get Node Value

This challenge is part of a tutorial track by MyCodeSchool Given a pointer to the head of a linked list and a specific position, determine the data value at that position. Count backwards from the tail node. The tail is at postion 0, its parent is at 1 and so on. Example head refers to 3 -> 2 -> 1 -> 0 -> NULL positionFromTail = 2 Each of the data values matches its distance from the t

## Delete duplicate-value nodes from a sorted linked list

This challenge is part of a tutorial track by MyCodeSchool You are given the pointer to the head node of a sorted linked list, where the data in the nodes is in ascending order. Delete nodes and return a sorted list with each distinct value in the original list. The given head pointer may be null indicating that the list is empty. Example head refers to the first node in the list 1 -> 2 -

## Cycle Detection

A linked list is said to contain a cycle if any node is visited more than once while traversing the list. Given a pointer to the head of a linked list, determine if it contains a cycle. If it does, return 1. Otherwise, return 0. Example head refers 1 -> 2 -> 3 -> NUL The numbers shown are the node numbers, not their data values. There is no cycle in this list so return 0. head refer

## Find Merge Point of Two Lists

This challenge is part of a tutorial track by MyCodeSchool Given pointers to the head nodes of 2 linked lists that merge together at some point, find the node where the two lists merge. The merge point is where both lists point to the same node, i.e. they reference the same memory location. It is guaranteed that the two head nodes will be different, and neither will be NULL. If the lists share

## Inserting a Node Into a Sorted Doubly Linked List

Given a reference to the head of a doubly-linked list and an integer ,data , create a new DoublyLinkedListNode object having data value data and insert it at the proper location to maintain the sort. Example head refers to the list 1 <-> 2 <-> 4 - > NULL. data = 3 Return a reference to the new list: 1 <-> 2 <-> 4 - > NULL , Function Description Complete the sortedInsert function