# 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[2] + 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] = 0; dp[1] = 1; dp[2] = 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[1000001];

int main(){
int Q,N,i,j;
memset(ans,-1,sizeof(ans));
ans[0]=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))```
```

## Find the Running Median

The median of a set of integers is the midpoint value of the data set for which an equal number of integers are less than and greater than the value. To find the median, you must first sort your set of integers in non-decreasing order, then: If your set contains an odd number of elements, the median is the middle element of the sorted sample. In the sorted set { 1, 2, 3 } , 2 is the median.

## Minimum Average Waiting Time

Tieu owns a pizza restaurant and he manages it in his own way. While in a normal restaurant, a customer is served by following the first-come, first-served rule, Tieu simply minimizes the average waiting time of his customers. So he gets to decide who is served first, regardless of how sooner or later a person comes. Different kinds of pizzas take different amounts of time to cook. Also, once h

## Merging Communities

People connect with each other in a social network. A connection between Person I and Person J is represented as . When two persons belonging to different communities connect, the net effect is the merger of both communities which I and J belongs to. At the beginning, there are N people representing N communities. Suppose person 1 and 2 connected and later 2 and 3 connected, then ,1 , 2 and 3 w

## Components in a graph

There are 2 * N nodes in an undirected graph, and a number of edges connecting some nodes. In each edge, the first value will be between 1 and N, inclusive. The second node will be between N + 1 and , 2 * N inclusive. Given a list of edges, determine the size of the smallest and largest connected components that have or more nodes. A node can have any number of connections. The highest node valu

## Kundu and Tree

Kundu is true tree lover. Tree is a connected graph having N vertices and N-1 edges. Today when he got a tree, he colored each edge with one of either red(r) or black(b) color. He is interested in knowing how many triplets(a,b,c) of vertices are there , such that, there is atleast one edge having red color on all the three paths i.e. from vertex a to b, vertex b to c and vertex c to a . Note that

## Super Maximum Cost Queries

Victoria has a tree, T , consisting of N nodes numbered from 1 to N. Each edge from node Ui to Vi in tree T has an integer weight, Wi. Let's define the cost, C, of a path from some node X to some other node Y as the maximum weight ( W ) for any edge in the unique path from node X to Y node . Victoria wants your help processing Q queries on tree T, where each query contains 2 integers, L and