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 :



title-img


                            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[1000],i,j,B[1000],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[100], b[100], 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){
				int b = is.read();
				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()))
answer = -1
for i in range(32):
    if Counter(x >> i for x in a).most_common(1)[0][1] <= len(a) // 2:
        answer = i
    else:
        break
print(answer)
                    


View More Similar Problems

Tree: Preorder Traversal

Complete the preorder function in the editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's preorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the preOrder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the tree's

View Solution →

Tree: Postorder Traversal

Complete the postorder function in the editor below. It received 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's postorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the postorder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the

View Solution →

Tree: Inorder Traversal

In this challenge, you are required to implement inorder traversal of a tree. Complete the inorder function in your editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's inorder traversal as a single line of space-separated values. Input Format Our hidden tester code passes the root node of a binary tree to your $inOrder* func

View Solution →

Tree: Height of a Binary Tree

The height of a binary tree is the number of edges between the tree's root and its furthest leaf. For example, the following binary tree is of height : image Function Description Complete the getHeight or height function in the editor. It must return the height of a binary tree as an integer. getHeight or height has the following parameter(s): root: a reference to the root of a binary

View Solution →

Tree : Top View

Given a pointer to the root of a binary tree, print the top view of the binary tree. The tree as seen from the top the nodes, is called the top view of the tree. For example : 1 \ 2 \ 5 / \ 3 6 \ 4 Top View : 1 -> 2 -> 5 -> 6 Complete the function topView and print the resulting values on a single line separated by space.

View Solution →

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

View Solution →