# Iterate It

### Problem Statement :

```Consider the following pseudocode, run on an array  of length :

rep := 0
while A not empty:
B := []
for x in A, y in A:
if x != y: append absolute_value(x - y) to B
A := B
rep := rep + 1
Given the values of  and array , compute and print the final value of  after the pseudocode above terminates; if the loop will never terminate, print -1 instead.

Input Format

The first line contains a single integer, , denoting the length of array .
The second line contains  space-separated integers describing the respective values of .

Output Format

Print the final value of  after the pseudocode terminates; if the loop will never terminate, print -1 instead.```

### Solution :

```                            ```Solution in C :

In  C  :

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>

typedef unsigned int uint;

#define MAX_N 100000
#define MAX_VALUE 50000

uint a[MAX_N];
bool b[MAX_VALUE+1];

int main() {
uint n;
scanf("%u", &n);
assert(n <= MAX_N);
for (int i = 0; i < n; i++) {
uint v;
scanf("%u", &v);
assert(v);
assert(v <= MAX_VALUE);
b[v] = true;
}

// start grinding
uint rep = 0;
while (true) {
// transfer from b (presence array) back to a (sorted list)
uint stride = 0;
bool in_stride = true;
n = 0;
for (uint i = 1; i <= MAX_VALUE; i++) {
if (b[i]) {
b[i] = false;
if (!n) {
stride = i;
} else if (in_stride && i - a[n-1] != stride) {
in_stride = false;
}
a[n++] = i;
}
}
if (!n) {
break;
}
if (in_stride) {
// shortcut
assert(a[n-1]/stride == n);
rep += n;
break;
}
rep++;
for (uint ai = 0; ai < n-1; ai++) {
for (uint aj = ai+1; aj < n; aj++) {
// no unnecessary code here, performance-critical
b[a[aj] - a[ai]] = true;
}
}
}
printf("%u\n", rep);
return 0;
}```
```

```                        ```Solution in C++ :

In  C  ++  :

#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <sstream>
#include <cmath>
#include <ctime>
#include <bitset>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector< vector<int> > vvi;
typedef vector<ll> vl;
typedef vector< vector<ll> > vvl;

#define forn(i, n) for (int i = 0; i < (int)(n); i++)
#define forv(i, v) forn(i, v.size())
#define all(v) v.begin(), v.end()
#define mp make_pair
#define pb push_back

const int N = 50005;

int gcd(int x, int y) {
if (!y) return x;
return gcd(y, x % y);
}

int main() {
#ifdef NEREVAR_PROJECT
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int n; cin >> n;
vi a(n);
forn(i, n) {
scanf("%d", &a[i]);
}
sort(all(a));
a.erase(unique(all(a)), a.end());
n = (int)a.size();
for (int i = n - 1; i >= 0; i--) {
diffs |= present >> a[i];
present.set(a[i]);
}
vi s;
forn(i, N) {
if (diffs.test(i)) {
s.pb(i);
}
}
if (s.empty()) {
cout << 1 << endl;
return 0;
}
int g = 0;
forv(i, s) g = gcd(g, s[i]);
forv(i, s) s[i] /= g;

diffs.reset();
present.reset();
for (int i = (int)s.size() - 1; i >= 0; i--) {
diffs |= present >> (s[i] + 1);
present.set(s[i]);
}

int steps = 2, m = s.back() - s;
while (m && !diffs.test(0)) {
int mNext = m;
forn(i, m) {
if (diffs.test(i)) {
if (mNext == m) {
mNext = m - i - 1;
}
next |= diffs >> (i + 1);
}
}
m = mNext;
steps++;
diffs = next;
}

cout << steps + m << endl;

return 0;
}```
```

```                        ```Solution in Java :

In  Java :

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

public class Solution {
private static final int l = 60000;

private static int gcd(int a, int b){
if (a < b) return gcd(b, a);
if (b == 0) return a;
return gcd(b, a % b);
}

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
boolean[] list = new boolean[l+1];
Set<Integer> set = new HashSet<Integer>();
for (int i = 0; i < n; i++){
int a = in.nextInt();
list[a] = true;
}
boolean[] nList = new boolean[l+1];
for (int e : set){
for (int i = 1; i + e < l; i++){
nList[i] |= list[i + e];
/*
if (i < 10){
if ((list[i + e / b] >> (e % b)) > 0 || (list[i + e / b + 1] << (b - (e % b))) > 0){
System.out.println(bits(list[i + e / b] >> (e % b)));
System.out.println(bits(list[i + e / b + 1] << (b - (e % b))));
}
else{
System.out.println(bits(list[i + e / b]) + " " + (e % b));
System.out.println(bits(list[i + e / b + 1]) + " " + (b - (e % b)));
}
System.out.println(e + " " + i);
}*/
}
}
list = nList;
int g = 0;
int min = -1;
int max = 0;
//for (int a : set)
//System.out.println(a);
//System.out.println("-----");
set.clear();
for (int i = 0; i < l+1; i++){
if (list[i]){
//System.out.println(a);
if (min < 0) min = i;
max = i;
g = gcd(i, g);
}
}
//System.out.println("-----");
//System.out.println(min);
//System.out.println(max);
//System.out.println(g);
int o = 1;
if (set.size() == 0){
System.out.println(o);
return;
}
Set<Integer> nSet = new HashSet<Integer>();
for (int a : set)
set = nSet;
min /= g;
max /= g;
list = new boolean[l+1];
for (int a : set)
list[a] = true;
while (min > 1){
nList = new boolean[l+1];
for (int a = min; a <= max; a++){
if (list[a]){
for (int k = 1; k + a < l; k++){
nList[k] |= list[k + a];
}
}
}
list = nList;
max -= min;
for (int a = 1; a <= max; a++){
if (list[a]){
min = a;
break;
}
}
o++;
}
System.out.println(o + max);
}
/*
private static String bits(int i){
String s = "";
for (int j = b-1; j >= 0; j--)
s += (i & (1 << j)) > 0 ? 1 : 0;
return s;
}*/
}//[]{}```
```

```                        ```Solution in Python :

In  Python3  :

input()
A = set([int(m) for m in input().strip().split()])

def DeltaBasis2(AList,P=False):
if type(AList) == set:
AList = sorted(list(AList))
if len(AList) == 1:
return 1
Count = 0

while len(AList)>0:
if len(AList) == 1:
return Count + 1
LCM = True
for i1 in AList[1:]:
if i1 % AList != 0:
LCM = False
break
if LCM:
AList = [int(m/AList) for m in AList]

if (AList == 1 and AList == 2):
return Count + AList[-1]

Delta = set()
if len(AList) < 100:
MaxWidth = len(AList) - 1
else:
MaxWidth = int(len(AList)**0.75//1)
for W in range(1,MaxWidth+1):
for i1 in range(len(AList)-W):
Delta = sorted(list(Delta))

AList = sorted(list(set([m for m in Delta] + [AList[-1] - m for m in AList[:-1]])))
if P:
print(AList2)
Count +=  1
return Count

print(DeltaBasis2(A))```
```

## Tree: Level Order Traversal

Given a pointer to the root of a binary tree, you need to print the level order traversal of this tree. In level-order traversal, nodes are visited level by level from left to right. Complete the function levelOrder and print the values in a single line separated by a space. For example: 1 \ 2 \ 5 / \ 3 6 \ 4 F

## Binary Search Tree : Insertion

You are given a pointer to the root of a binary search tree and values to be inserted into the tree. Insert the values into their appropriate position in the binary search tree and return the root of the updated binary tree. You just have to complete the function. Input Format You are given a function, Node * insert (Node * root ,int data) { } Constraints No. of nodes in the tree <

## Tree: Huffman Decoding

Huffman coding assigns variable length codewords to fixed length input characters based on their frequencies. More frequent characters are assigned shorter codewords and less frequent characters are assigned longer codewords. All edges along the path to a character contain a code digit. If they are on the left side of the tree, they will be a 0 (zero). If on the right, they'll be a 1 (one). Only t

## Binary Search Tree : Lowest Common Ancestor

You are given pointer to the root of the binary search tree and two values v1 and v2. You need to return the lowest common ancestor (LCA) of v1 and v2 in the binary search tree. In the diagram above, the lowest common ancestor of the nodes 4 and 6 is the node 3. Node 3 is the lowest node which has nodes and as descendants. Function Description Complete the function lca in the editor b

## Swap Nodes [Algo]

A binary tree is a tree which is characterized by one of the following properties: It can be empty (null). It contains a root node only. It contains a root node with a left subtree, a right subtree, or both. These subtrees are also binary trees. In-order traversal is performed as Traverse the left subtree. Visit root. Traverse the right subtree. For this in-order traversal, start from

## Kitty's Calculations on a Tree

Kitty has a tree, T , consisting of n nodes where each node is uniquely labeled from 1 to n . Her friend Alex gave her q sets, where each set contains k distinct nodes. Kitty needs to calculate the following expression on each set: where: { u ,v } denotes an unordered pair of nodes belonging to the set. dist(u , v) denotes the number of edges on the unique (shortest) path between nodes a