# Manipulative Numbers

### Problem Statement :

```Suppose that  is a list of  numbers  and  is a permutation of these numbers, we say B is K-Manipulative if and only if:

is not less than , where  represents the XOR operator.

You are given . Find the largest  such that there exists a K-manipulative permutation .

Input:

The first line is an integer . The second line contains  space separated integers - .

Output:
The largest possible , or  if there is no solution.```

### Solution :

```                            ```Solution in C :

In  C :

#include<stdio.h>
#include<string.h>
int compare (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}
int isMajority(int a[], int size, int cand)
{
int i, count = 0;
for (i = 0; i < size; i++)
if(a[i] == cand)
count++;
if (count > size/2)
return 1;
else
return 0;
}

int findCandidate(int a[], int size)
{
int maj_index = 0, count = 1;
int i;
for(i = 1; i < size; i++)
{
if(a[maj_index] == a[i])
count++;
else
count--;
if(count == 0)
{
maj_index = i;
count = 1;
}
}
return a[maj_index];
}

int printMajority(int a[], int size)
{
int cand = findCandidate(a, size);
if(isMajority(a, size, cand))
return -1;
return 0;
}

/*void print(int *A,int n)
{
int i;
for(i=0;i<n;i++)
printf("%d ",A[i]);
printf("\n");
}*/
int main()
{
int n,A,i,j,B,flag=-1;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&A[i]);
//print(A,n);
qsort (A,n,sizeof(int),compare);
//print(A,n);
for(i=31;i>=1;i--)
{
memcpy(&B,A,n* sizeof(int));
//print(B,n);
for(j=0;j<n;j++)
B[j]=B[j]>>i;
//print(B,n);
if(printMajority(B,n)==0)
{
printf("%d\n",i);
flag=0;
break;
}
}
if(flag==-1)
printf("-1\n");
return 0;
}```
```

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

In  C ++  :

#include <iostream>
#include <algorithm>
using namespace std;

int highbit(int x)
{
int k = -1;
while (x != 0)
{
x >>= 1;
k++;
}
return k;
}

int main()
{
int a, b, n;
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
int *p = a, *q = b;
int m = n / 2;
sort(p, p + n);
int k = highbit(p[m]);
while (k >= 0)
{
int x = 1 << k;
for (int i = 0; i < n; i++)
q[i] = p[i] ^ x;
sort(q, q + n);
int s = highbit(q[m]);
if (s == k) break;
swap(p, q);
k = s;
}
cout << k << endl;
return 0;
}```
```

```                        ```Solution in Java :

In  Java :

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;

public class Solution {
static InputStream is;
static PrintWriter out;
static String INPUT = "";

static void solve()
{
int n = ni();
int[] a = new int[n];
int[] b = new int[n];
for(int i = 0;i < n;i++)a[i] = ni();

for(int k = 30;k >= 0;k--){
for(int i = 0;i < n;i++)b[i] = a[i] >>> k;
if(iskmul(b)){
out.println(k);
return;
}
}
out.println(-1);
}

static boolean iskmul(int[] a)
{
int n = a.length;
Arrays.sort(a);
int ct = 0;
int max = 1;
for(int i = 0;i < n;i++){
if(i == 0 || a[i] != a[i-1]){
ct = 1;
}else{
ct++;
max = Math.max(max, ct);
}
}
return max <= n/2;
}

public static void main(String[] args) throws Exception
{
long S = System.currentTimeMillis();
is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);

solve();
out.flush();
long G = System.currentTimeMillis();
tr(G-S+"ms");
}

static boolean eof()
{
try {
is.mark(1000);
int b;
while((b = is.read()) != -1 && !(b >= 33 && b <= 126));
is.reset();
return b == -1;
} catch (IOException e) {
return true;
}
}

static int ni()
{
try {
int num = 0;
boolean minus = false;
while((num = is.read()) != -1 && !((num >= '0' && num <= '9') || num == '-'));
if(num == '-'){
num = 0;
minus = true;
}else{
num -= '0';
}

while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
}
} catch (IOException e) {
}
return -1;
}

static void tr(Object... o) { if(INPUT.length() != 0)System.out.println(Arrays.deepToString(o)); }
}```
```

```                        ```Solution in Python :

In  Python3 :

from collections import Counter
input()
a = list(map(int, input().split()))
for i in range(32):
if Counter(x >> i for x in a).most_common(1) <= len(a) // 2:
else:
break
```

## Balanced Forest

Greg has a tree of nodes containing integer data. He wants to insert a node with some non-zero integer value somewhere into the tree. His goal is to be able to cut two edges and have the values of each of the three new trees sum to the same amount. This is called a balanced forest. Being frugal, the data value he inserts should be minimal. Determine the minimal amount that a new node can have to a

## Jenny's Subtrees

Jenny loves experimenting with trees. Her favorite tree has n nodes connected by n - 1 edges, and each edge is ` unit in length. She wants to cut a subtree (i.e., a connected part of the original tree) of radius r from this tree by performing the following two steps: 1. Choose a node, x , from the tree. 2. Cut a subtree consisting of all nodes which are not further than r units from node x .

## Tree Coordinates

We consider metric space to be a pair, , where is a set and such that the following conditions hold: where is the distance between points and . Let's define the product of two metric spaces, , to be such that: , where , . So, it follows logically that is also a metric space. We then define squared metric space, , to be the product of a metric space multiplied with itself: . For

## Array Pairs

Consider an array of n integers, A = [ a1, a2, . . . . an] . Find and print the total number of (i , j) pairs such that ai * aj <= max(ai, ai+1, . . . aj) where i < j. Input Format The first line contains an integer, n , denoting the number of elements in the array. The second line consists of n space-separated integers describing the respective values of a1, a2 , . . . an .

## Self Balancing Tree

An AVL tree (Georgy Adelson-Velsky and Landis' tree, named after the inventors) is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. We define balance factor for each node as : balanceFactor = height(left subtree) - height(righ

## Array and simple queries

Given two numbers N and M. N indicates the number of elements in the array A[](1-indexed) and M indicates number of queries. You need to perform two types of queries on the array A[] . You are given queries. Queries can be of two types, type 1 and type 2. Type 1 queries are represented as 1 i j : Modify the given array by removing elements from i to j and adding them to the front. Ty