Given a positive integer N, the task for any permutation of size N having elements over the range [0, N – 1], is to calculate the sum of Bitwise XOR of all elements with their respective position.
For Example: For the permutation {3, 4, 2, 1, 0}, sum = (0^3 + 1^4 + 2^2 + 3^1 + 4^0) = 2.
Examples:
Input: N = 3
Output: 6
Explanation: For the permutations {1, 2, 0} and {2, 0, 1}, the sum is 6.
Input: N = 2
Output: 2
Approach: To solve this problem, the idea is to recursion to generate all possible permutations of the integers [0, N – 1] and calculate the score for each one of them and then find the maximum score among all the permutations formed.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
int calcScr(vector< int >arr){
int ans = 0;
for ( int i = 0; i < arr.size(); i++)
ans += (i ^ arr[i]);
return ans;
}
int getMax(vector< int > arr, int ans, vector< bool > chosen, int N)
{
if (arr.size() == N){
ans = max(ans, calcScr(arr));
return ans;
}
for ( int i = 0; i < N; i++)
{
if (chosen[i])
continue ;
chosen[i] = true ;
arr.push_back(i);
ans = getMax(arr, ans, chosen, N);
chosen[i] = false ;
arr.pop_back();
}
return ans;
}
int main()
{
int N = 2;
vector< int > arr;
int ans = -1;
vector< bool >chosen(N, false );
ans = getMax(arr, ans, chosen, N);
cout << ans << endl;
}
|
Java
import java.util.*;
class GFG
{
static int calcScr(ArrayList<Integer>arr)
{
int ans = 0 ;
for ( int i = 0 ; i < arr.size(); i++)
ans += (i ^ arr.get(i));
return ans;
}
static int getMax(ArrayList<Integer> arr, int ans, ArrayList<Boolean> chosen, int N)
{
if (arr.size() == N)
{
ans = Math.max(ans, calcScr(arr));
return ans;
}
for ( int i = 0 ; i < N; i++)
{
if (chosen.get(i))
continue ;
chosen.set(i, true );
arr.add(i);
ans = getMax(arr, ans, chosen, N);
chosen.set(i, false );
arr.remove(arr.size()- 1 );
}
return ans;
}
public static void main(String[] args)
{
int N = 2 ;
ArrayList<Integer> arr = new ArrayList<Integer>();
int ans = - 1 ;
ArrayList<Boolean> chosen = new ArrayList<Boolean>(Collections.nCopies(N, false ));
ans = getMax(arr, ans, chosen, N);
System.out.print(ans + "\n" );
}
}
|
Python3
def getMax(arr, ans, chosen, N):
if len (arr) = = N:
ans = max (ans, calcScr(arr))
return ans
for i in range (N):
if chosen[i]:
continue
chosen[i] = True
arr.append(i)
ans = getMax(arr, ans, chosen, N)
chosen[i] = False
arr.pop()
return ans
def calcScr(arr):
ans = 0
for i in range ( len (arr)):
ans + = (i ^ arr[i])
return ans
N = 2
arr = []
ans = - 1
chosen = [ False for i in range (N)]
ans = getMax(arr, ans, chosen, N)
print (ans)
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
static int calcScr(List< int >arr)
{
int ans = 0;
for ( int i = 0; i < arr.Count; i++)
ans += (i ^ arr[i]);
return ans;
}
static int getMax(List< int > arr, int ans, List<Boolean> chosen, int N)
{
if (arr.Count == N)
{
ans = Math.Max(ans, calcScr(arr));
return ans;
}
for ( int i = 0; i < N; i++)
{
if (chosen[i])
continue ;
chosen[i] = true ;
arr.Add(i);
ans = getMax(arr, ans, chosen, N);
chosen[i] = false ;
arr.Remove(arr.Count-1);
}
return ans;
}
public static void Main(String[] args)
{
int N = 2;
List< int > arr = new List< int >();
int ans = -1;
List< bool > chosen = new List< bool >(N);
for ( int i = 0; i < N; i++)
chosen.Add( false );
ans = getMax(arr, ans, chosen, N);
Console.Write(ans + "\n" );
}
}
|
Javascript
<script>
function calcScr(arr) {
let ans = 0;
for (let i = 0; i < arr.length; i++)
ans += (i ^ arr[i]);
return ans;
}
function getMax(arr, ans, chosen, N) {
if (arr.length == N) {
ans = Math.max(ans, calcScr(arr));
return ans;
}
for (let i = 0; i < N; i++) {
if (chosen[i])
continue ;
chosen[i] = true ;
arr.push(i);
ans = getMax(arr, ans, chosen, N);
chosen[i] = false ;
arr.pop();
}
return ans;
}
let N = 2;
let arr = [];
let ans = -1;
let chosen = new Array(N).fill( false );
ans = getMax(arr, ans, chosen, N);
document.write(ans + "<br>" );
</script>
|
Time Complexity: O(N*N!)
Auxiliary Space: O(N)