Skip to main content

Find a rotation with maximum hamming distance

Here given code implementation process.

// C Program
// Find a rotation with maximum hamming distance
#include <stdio.h>

#include <string.h>

// Calculates the maximum hamming distance of a rotating array
void hammingDistance(int arr[], int size)
{
	// Define resultant variables
	int result = 0;
	int counter = 0;
	// Loop controlling variables
	int i = 0;
	int j = 0;
	int k = 0;
	// Outer loop execute 1..(n-1) times
	for (i = 1; i < size && counter < size; i++)
	{
		counter = 0;
		// Perform [i] right side rotation and calculates the new hamming distance
		for (j = i, k = 0; j < size + i; j++, k++)
		{
			if (arr[k] != arr[j % size])
			{
				// When rotating element are different
				counter++;
			}
		}
		if (result < counter)
		{
			// Get a new result
			result = counter;
		}
	}
	// Display calculated result
	printf(" Max Hamming Distance : %d \n", result);
}
int main()
{
	int arr[] = {
		1 , 3 , 3 , 3 , 3 , 7
	};
	// Get the size
	int size = sizeof(arr) / sizeof(arr[0]);
	/*
	array = {1, 3, 3, 3, 3, 7}

	1-st rotation
	==================
	{3, 3, 3, 3, 7, 1} Rotated array
	{1, 3, 3, 3, 3, 7} Actual array
	---------------------
	 1  0  0  0  1   1 = 3 Hamming distance
	--------------------- 
	result = 3


	2-nd rotation
	==================
	{3, 3, 3, 7, 1, 3} Rotated array
	{1, 3, 3, 3, 3, 7} Actual array
	---------------------
	 1  0  0  1  1   1 = 4 Hamming distance
	--------------------- 
	result = 4


	3-rd rotation
	==================
	{3, 3, 7, 1, 3, 3} Rotated array
	{1, 3, 3, 3, 3, 7} Actual array
	---------------------
	 1  0  1  1  0  1 = 4 Hamming distance
	--------------------- 
	result = 4 
	
	4-th rotation
	==================
	{3, 7, 1, 3, 3, 3} Rotated array
	{1, 3, 3, 3, 3, 7} Actual array
	---------------------
	 1  1  1  0  0  1 = 4 Hamming distance
	--------------------- 
	result = 4 


	5-th rotation
	{1, 1, 3, 3, 3, 3} Rotated array
	{1, 3, 3, 3, 3, 7} Actual array
	---------------------
	 0  1  0  0  0  1 = 2 Hamming distance
	--------------------- 
	result = 4 
	*/
	hammingDistance(arr, size);
	return 0;
}

Output

 Max Hamming Distance : 4
/*
  Java Program
  Find a rotation with maximum hamming distance
*/
public class Calculation
{
	// Calculates the maximum hamming distance of a rotating array
	public void hammingDistance(int[] arr, int size)
	{
		// Define resultant variables
		int result = 0;
		int counter = 0;
		// Loop controlling variables
		int i = 0;
		int j = 0;
		int k = 0;
		// Outer loop execute 1..(n-1) times
		for (i = 1; i < size && counter < size; i++)
		{
			counter = 0;
			// Perform [i] right side rotation and calculates the new hamming distance
			for (j = i, k = 0; j < size + i; j++, k++)
			{
				if (arr[k] != arr[j % size])
				{
					// When rotating element are different
					counter++;
				}
			}
			if (result < counter)
			{
				// Get a new result
				result = counter;
			}
		}
		// Display calculated result
		System.out.print(" Max Hamming Distance : " + result + " \n");
	}
	public static void main(String[] args)
	{
		Calculation op = new Calculation();
		int[] arr = {
			1 , 3 , 3 , 3 , 3 , 7
		};
		// Get the size
		int size = arr.length;
		/*
		array = {1, 3, 3, 3, 3, 7}

		1-st rotation
		==================
		{3, 3, 3, 3, 7, 1} Rotated array
		{1, 3, 3, 3, 3, 7} Actual array
		---------------------
		 1  0  0  0  1   1 = 3 Hamming distance
		--------------------- 
		result = 3


		2-nd rotation
		==================
		{3, 3, 3, 7, 1, 3} Rotated array
		{1, 3, 3, 3, 3, 7} Actual array
		---------------------
		 1  0  0  1  1   1 = 4 Hamming distance
		--------------------- 
		result = 4


		3-rd rotation
		==================
		{3, 3, 7, 1, 3, 3} Rotated array
		{1, 3, 3, 3, 3, 7} Actual array
		---------------------
		 1  0  1  1  0  1 = 4 Hamming distance
		--------------------- 
		result = 4 
		
		4-th rotation
		==================
		{3, 7, 1, 3, 3, 3} Rotated array
		{1, 3, 3, 3, 3, 7} Actual array
		---------------------
		 1  1  1  0  0  1 = 4 Hamming distance
		--------------------- 
		result = 4 


		5-th rotation
		{1, 1, 3, 3, 3, 3} Rotated array
		{1, 3, 3, 3, 3, 7} Actual array
		---------------------
		 0  1  0  0  0  1 = 2 Hamming distance
		--------------------- 
		result = 4 
		
		*/
		op.hammingDistance(arr, size);
	}
}

Output

 Max Hamming Distance : 4
// Include header file
#include <iostream>

using namespace std;
/*
  C++ Program
  Find a rotation with maximum hamming distance
*/
class Calculation
{
	public:
		// Calculates the maximum hamming distance of a rotating array
		void hammingDistance(int arr[], int size)
		{
			// Define resultant variables
			int result = 0;
			int counter = 0;
			// Loop controlling variables
			int i = 0;
			int j = 0;
			int k = 0;
			// Outer loop execute 1..(n-1) times
			for (i = 1; i < size && counter < size; i++)
			{
				counter = 0;
				// Perform [i] right side rotation and calculates the new hamming distance
				for (j = i, k = 0; j < size + i; j++, k++)
				{
					if (arr[k] != arr[j % size])
					{
						// When rotating element are different
						counter++;
					}
				}
				if (result < counter)
				{
					// Get a new result
					result = counter;
				}
			}
			// Display calculated result
			cout << " Max Hamming Distance : " << result << " \n";
		}
};
int main()
{
	Calculation op = Calculation();
	int arr[] = {
		1 , 3 , 3 , 3 , 3 , 7
	};
	// Get the size
	int size = sizeof(arr) / sizeof(arr[0]);
	/*
	array = {1, 3, 3, 3, 3, 7}

	1-st rotation
	==================
	{3, 3, 3, 3, 7, 1} Rotated array
	{1, 3, 3, 3, 3, 7} Actual array
	---------------------
	 1  0  0  0  1   1 = 3 Hamming distance
	--------------------- 
	result = 3


	2-nd rotation
	==================
	{3, 3, 3, 7, 1, 3} Rotated array
	{1, 3, 3, 3, 3, 7} Actual array
	---------------------
	 1  0  0  1  1   1 = 4 Hamming distance
	--------------------- 
	result = 4


	3-rd rotation
	==================
	{3, 3, 7, 1, 3, 3} Rotated array
	{1, 3, 3, 3, 3, 7} Actual array
	---------------------
	 1  0  1  1  0  1 = 4 Hamming distance
	--------------------- 
	result = 4 

	4-th rotation
	==================
	{3, 7, 1, 3, 3, 3} Rotated array
	{1, 3, 3, 3, 3, 7} Actual array
	---------------------
	 1  1  1  0  0  1 = 4 Hamming distance
	--------------------- 
	result = 4 


	5-th rotation
	{1, 1, 3, 3, 3, 3} Rotated array
	{1, 3, 3, 3, 3, 7} Actual array
	---------------------
	 0  1  0  0  0  1 = 2 Hamming distance
	--------------------- 
	result = 4 

	*/
	op.hammingDistance(arr, size);
	return 0;
}

Output

 Max Hamming Distance : 4
// Include namespace system
using System;
/*
  C# Program
  Find a rotation with maximum hamming distance
*/
public class Calculation
{
	// Calculates the maximum hamming distance of a rotating array
	public void hammingDistance(int[] arr, int size)
	{
		// Define resultant variables
		int result = 0;
		int counter = 0;
		// Loop controlling variables
		int i = 0;
		int j = 0;
		int k = 0;
		// Outer loop execute 1..(n-1) times
		for (i = 1; i < size && counter < size; i++)
		{
			counter = 0;
			// Perform [i] right side rotation and calculates the new hamming distance
			for (j = i, k = 0; j < size + i; j++, k++)
			{
				if (arr[k] != arr[j % size])
				{
					// When rotating element are different
					counter++;
				}
			}
			if (result < counter)
			{
				// Get a new result
				result = counter;
			}
		}
		// Display calculated result
		Console.Write(" Max Hamming Distance : " + result + " \n");
	}
	public static void Main(String[] args)
	{
		Calculation op = new Calculation();
		int[] arr = {
			1 , 3 , 3 , 3 , 3 , 7
		};
		// Get the size
		int size = arr.Length;
		/*
				array = {1, 3, 3, 3, 3, 7}

				1-st rotation
				==================
				{3, 3, 3, 3, 7, 1} Rotated array
				{1, 3, 3, 3, 3, 7} Actual array
				---------------------
				 1  0  0  0  1   1 = 3 Hamming distance
				--------------------- 
				result = 3


				2-nd rotation
				==================
				{3, 3, 3, 7, 1, 3} Rotated array
				{1, 3, 3, 3, 3, 7} Actual array
				---------------------
				 1  0  0  1  1   1 = 4 Hamming distance
				--------------------- 
				result = 4


				3-rd rotation
				==================
				{3, 3, 7, 1, 3, 3} Rotated array
				{1, 3, 3, 3, 3, 7} Actual array
				---------------------
				 1  0  1  1  0  1 = 4 Hamming distance
				--------------------- 
				result = 4 
				
				4-th rotation
				==================
				{3, 7, 1, 3, 3, 3} Rotated array
				{1, 3, 3, 3, 3, 7} Actual array
				---------------------
				 1  1  1  0  0  1 = 4 Hamming distance
				--------------------- 
				result = 4 


				5-th rotation
				{1, 1, 3, 3, 3, 3} Rotated array
				{1, 3, 3, 3, 3, 7} Actual array
				---------------------
				 0  1  0  0  0  1 = 2 Hamming distance
				--------------------- 
				result = 4 
				
				*/
		op.hammingDistance(arr, size);
	}
}

Output

 Max Hamming Distance : 4
<?php
/*
  Php Program
  Find a rotation with maximum hamming distance
*/
class Calculation
{
	// Calculates the maximum hamming distance of a rotating array
	public	function hammingDistance( & $arr, $size)
	{
		// Define resultant variables
		$result = 0;
		$counter = 0;
		// Loop controlling variables
		$i = 0;
		$j = 0;
		$k = 0;
		// Outer loop execute 1..(n-1) times
		for ($i = 1; $i < $size && $counter < $size; $i++)
		{
			$counter = 0;
			// Perform [i] right side rotation and calculates the new hamming distance
			for ($j = $i, $k = 0; $j < $size + $i; $j++, $k++)
			{
				if ($arr[$k] != $arr[$j % $size])
				{
					// When rotating element are different
					$counter++;
				}
			}
			if ($result < $counter)
			{
				// Get a new result
				$result = $counter;
			}
		}
		// Display calculated result
		echo " Max Hamming Distance : ". $result ." \n";
	}
}

function main()
{
	$op = new Calculation();
	$arr = array(1, 3, 3, 3, 3, 7);
	// Get the size
	$size = count($arr);
	/*
			array = {1, 3, 3, 3, 3, 7}

			1-st rotation
			==================
			{3, 3, 3, 3, 7, 1} Rotated array
			{1, 3, 3, 3, 3, 7} Actual array
			---------------------
			 1  0  0  0  1   1 = 3 Hamming distance
			--------------------- 
			result = 3


			2-nd rotation
			==================
			{3, 3, 3, 7, 1, 3} Rotated array
			{1, 3, 3, 3, 3, 7} Actual array
			---------------------
			 1  0  0  1  1   1 = 4 Hamming distance
			--------------------- 
			result = 4


			3-rd rotation
			==================
			{3, 3, 7, 1, 3, 3} Rotated array
			{1, 3, 3, 3, 3, 7} Actual array
			---------------------
			 1  0  1  1  0  1 = 4 Hamming distance
			--------------------- 
			result = 4 
			
			4-th rotation
			==================
			{3, 7, 1, 3, 3, 3} Rotated array
			{1, 3, 3, 3, 3, 7} Actual array
			---------------------
			 1  1  1  0  0  1 = 4 Hamming distance
			--------------------- 
			result = 4 


			5-th rotation
			{1, 1, 3, 3, 3, 3} Rotated array
			{1, 3, 3, 3, 3, 7} Actual array
			---------------------
			 0  1  0  0  0  1 = 2 Hamming distance
			--------------------- 
			result = 4 
			
			*/
	$op->hammingDistance($arr, $size);
}
main();

Output

 Max Hamming Distance : 4
/*
  Node Js Program
  Find a rotation with maximum hamming distance
*/
class Calculation
{
	// Calculates the maximum hamming distance of a rotating array
	hammingDistance(arr, size)
	{
		// Define resultant variables
		var result = 0;
		var counter = 0;
		// Loop controlling variables
		var i = 0;
		var j = 0;
		var k = 0;
		// Outer loop execute 1..(n-1) times
		for (i = 1; i < size && counter < size; i++)
		{
			counter = 0;
			// Perform [i] right side rotation and calculates the new hamming distance
			for (j = i, k = 0; j < size + i; j++, k++)
			{
				if (arr[k] != arr[j % size])
				{
					// When rotating element are different
					counter++;
				}
			}
			if (result < counter)
			{
				// Get a new result
				result = counter;
			}
		}
		// Display calculated result
		process.stdout.write(" Max Hamming Distance : " + result + " \n");
	}
}

function main()
{
	var op = new Calculation();
	var arr = [1, 3, 3, 3, 3, 7];
	// Get the size
	var size = arr.length;
	/*
			array = {1, 3, 3, 3, 3, 7}

			1-st rotation
			==================
			{3, 3, 3, 3, 7, 1} Rotated array
			{1, 3, 3, 3, 3, 7} Actual array
			---------------------
			 1  0  0  0  1   1 = 3 Hamming distance
			--------------------- 
			result = 3


			2-nd rotation
			==================
			{3, 3, 3, 7, 1, 3} Rotated array
			{1, 3, 3, 3, 3, 7} Actual array
			---------------------
			 1  0  0  1  1   1 = 4 Hamming distance
			--------------------- 
			result = 4


			3-rd rotation
			==================
			{3, 3, 7, 1, 3, 3} Rotated array
			{1, 3, 3, 3, 3, 7} Actual array
			---------------------
			 1  0  1  1  0  1 = 4 Hamming distance
			--------------------- 
			result = 4 
			
			4-th rotation
			==================
			{3, 7, 1, 3, 3, 3} Rotated array
			{1, 3, 3, 3, 3, 7} Actual array
			---------------------
			 1  1  1  0  0  1 = 4 Hamming distance
			--------------------- 
			result = 4 


			5-th rotation
			{1, 1, 3, 3, 3, 3} Rotated array
			{1, 3, 3, 3, 3, 7} Actual array
			---------------------
			 0  1  0  0  0  1 = 2 Hamming distance
			--------------------- 
			result = 4 
			
			*/
	op.hammingDistance(arr, size);
}
main();

Output

 Max Hamming Distance : 4
#  Python 3 Program
#  Find a rotation with maximum hamming distance

class Calculation :
	#  Calculates the maximum hamming distance of a rotating array
	def hammingDistance(self, arr, size) :
		#  Define resultant variables
		result = 0
		counter = 0
		#  Loop controlling variables
		i = 1
		j = 0
		k = 0
		#  Outer loop execute 1..(n-1) times
		while (i < size and counter < size) :
			counter = 0
			#  Perform [i] right side rotation and calculates the new hamming distance
			j = i
			k = 0
			while (j < size + i) :
				if (arr[k] != arr[j % size]) :
					#  When rotating element are different
					counter += 1
				
				j += 1
				k += 1
			
			if (result < counter) :
				#  Get a new result
				result = counter
			
			i += 1
		
		#  Display calculated result
		print(" Max Hamming Distance : ", result ," ")
	

def main() :
	op = Calculation()
	arr = [1, 3, 3, 3, 3, 7]
	#  Get the size
	size = len(arr)
	# 
	# 		array = 1, 3, 3, 3, 3, 7
	# 		1-st rotation
	# 		==================
	# 		3, 3, 3, 3, 7, 1 Rotated array
	# 		1, 3, 3, 3, 3, 7 Actual array
	# 		---------------------
	# 		1  0  0  0  1   1 = 3 Hamming distance
	# 		--------------------- 
	# 		result = 3
	# 		2-nd rotation
	# 		==================
	# 		3, 3, 3, 7, 1, 3 Rotated array
	# 		1, 3, 3, 3, 3, 7 Actual array
	# 		---------------------
	# 		1  0  0  1  1   1 = 4 Hamming distance
	# 		--------------------- 
	# 		result = 4
	# 		3-rd rotation
	# 		==================
	# 		3, 3, 7, 1, 3, 3 Rotated array
	# 		1, 3, 3, 3, 3, 7 Actual array
	# 		---------------------
	# 		1  0  1  1  0  1 = 4 Hamming distance
	# 		--------------------- 
	# 		result = 4 
	# 		
	# 		4-th rotation
	# 		==================
	# 		3, 7, 1, 3, 3, 3 Rotated array
	# 		1, 3, 3, 3, 3, 7 Actual array
	# 		---------------------
	# 		1  1  1  0  0  1 = 4 Hamming distance
	# 		--------------------- 
	# 		result = 4 
	# 		5-th rotation
	# 		1, 1, 3, 3, 3, 3 Rotated array
	# 		1, 3, 3, 3, 3, 7 Actual array
	# 		---------------------
	# 		0  1  0  0  0  1 = 2 Hamming distance
	# 		--------------------- 
	# 		result = 4 
		
	
	op.hammingDistance(arr, size)

if __name__ == "__main__": main()

Output

 Max Hamming Distance :  4
#   Ruby Program
#   Find a rotation with maximum hamming distance

class Calculation 
	#  Calculates the maximum hamming distance of a rotating array
	def hammingDistance(arr, size) 
		#  Define resultant variables
		result = 0
		counter = 0
		#  Loop controlling variables
		i = 1
		j = 0
		k = 0
		#  Outer loop execute 1..(n-1) times
		while (i < size && counter < size) 
			counter = 0
			#  Perform [i] right side rotation and calculates the new hamming distance
			j = i
			k = 0
			while (j < size + i) 
				if (arr[k] != arr[j % size]) 
					#  When rotating element are different
					counter += 1
				end

				j += 1
				k += 1
			end

			if (result < counter) 
				#  Get a new result
				result = counter
			end

			i += 1
		end

		#  Display calculated result
		print(" Max Hamming Distance : ", result ," \n")
	end

end

def main() 
	op = Calculation.new()
	arr = [1, 3, 3, 3, 3, 7]
	#  Get the size
	size = arr.length
	# 
	# 		array = 1, 3, 3, 3, 3, 7
	# 		1-st rotation
	# 		==================
	# 		3, 3, 3, 3, 7, 1 Rotated array
	# 		1, 3, 3, 3, 3, 7 Actual array
	# 		---------------------
	# 		1  0  0  0  1   1 = 3 Hamming distance
	# 		--------------------- 
	# 		result = 3
	# 		2-nd rotation
	# 		==================
	# 		3, 3, 3, 7, 1, 3 Rotated array
	# 		1, 3, 3, 3, 3, 7 Actual array
	# 		---------------------
	# 		1  0  0  1  1   1 = 4 Hamming distance
	# 		--------------------- 
	# 		result = 4
	# 		3-rd rotation
	# 		==================
	# 		3, 3, 7, 1, 3, 3 Rotated array
	# 		1, 3, 3, 3, 3, 7 Actual array
	# 		---------------------
	# 		1  0  1  1  0  1 = 4 Hamming distance
	# 		--------------------- 
	# 		result = 4 
	# 		
	# 		4-th rotation
	# 		==================
	# 		3, 7, 1, 3, 3, 3 Rotated array
	# 		1, 3, 3, 3, 3, 7 Actual array
	# 		---------------------
	# 		1  1  1  0  0  1 = 4 Hamming distance
	# 		--------------------- 
	# 		result = 4 
	# 		5-th rotation
	# 		1, 1, 3, 3, 3, 3 Rotated array
	# 		1, 3, 3, 3, 3, 7 Actual array
	# 		---------------------
	# 		0  1  0  0  0  1 = 2 Hamming distance
	# 		--------------------- 
	# 		result = 4 
			
	
	op.hammingDistance(arr, size)
end

main()

Output

 Max Hamming Distance : 4 
/*
  Scala Program
  Find a rotation with maximum hamming distance
*/
class Calculation
{
    // Calculates the maximum hamming distance of a rotating array
    def hammingDistance(arr: Array[Int], size: Int): Unit = {
        // Define resultant variables
        var result: Int = 0;
        var counter: Int = 0;
        // Loop controlling variables
        var i: Int = 1;
        var j: Int = 0;
        var k: Int = 0;
        // Outer loop execute 1..(n-1) times
        while (i < size && counter < size)
        {
            counter = 0;
            // Perform [i] right side rotation and calculates the new hamming distance
            j = i;
            k = 0;
            while (j < size + i)
            {
                if (arr(k) != arr(j % size))
                {
                    // When rotating element are different
                    counter += 1;
                }
                j += 1;
                k += 1;
            }
            if (result < counter)
            {
                // Get a new result
                result = counter;
            }
            i += 1;
        }
        // Display calculated result
        print(" Max Hamming Distance : " + result + " \n");
    }
}
object Main
{
    def main(args: Array[String]): Unit = {
        var op: Calculation = new Calculation();
        var arr: Array[Int] = Array(1, 3, 3, 3, 3, 7);
        // Get the size
        var size: Int = arr.length;
        /*
        array = {1, 3, 3, 3, 3, 7}

        1-st rotation
        ==================
        {3, 3, 3, 3, 7, 1} Rotated array
        {1, 3, 3, 3, 3, 7} Actual array
        ---------------------
         1  0  0  0  1   1 = 3 Hamming distance
        --------------------- 
        result = 3


        2-nd rotation
        ==================
        {3, 3, 3, 7, 1, 3} Rotated array
        {1, 3, 3, 3, 3, 7} Actual array
        ---------------------
         1  0  0  1  1   1 = 4 Hamming distance
        --------------------- 
        result = 4


        3-rd rotation
        ==================
        {3, 3, 7, 1, 3, 3} Rotated array
        {1, 3, 3, 3, 3, 7} Actual array
        ---------------------
         1  0  1  1  0  1 = 4 Hamming distance
        --------------------- 
        result = 4 
        
        4-th rotation
        ==================
        {3, 7, 1, 3, 3, 3} Rotated array
        {1, 3, 3, 3, 3, 7} Actual array
        ---------------------
         1  1  1  0  0  1 = 4 Hamming distance
        --------------------- 
        result = 4 


        5-th rotation
        {1, 1, 3, 3, 3, 3} Rotated array
        {1, 3, 3, 3, 3, 7} Actual array
        ---------------------
         0  1  0  0  0  1 = 2 Hamming distance
        --------------------- 
        result = 4 
        
        */
        op.hammingDistance(arr, size);
    }
}

Output

 Max Hamming Distance : 4
/*
  Swift 4 Program
  Find a rotation with maximum hamming distance
*/
class Calculation
{
	// Calculates the maximum hamming distance of a rotating array
	func hammingDistance(_ arr: [Int], _ size: Int)
	{
		// Define resultant variables
		var result: Int = 0;
		var counter: Int = 0;
		// Loop controlling variables
		var i: Int = 1;
		var j: Int = 0;
		var k: Int = 0;
		// Outer loop execute 1..(n-1) times
		while (i < size && counter < size)
		{
			counter = 0;
			// Perform [i]right side rotation and calculates the new hamming distance
			j = i;
			k = 0;
			while (j < size + i)
			{
				if (arr[k] != arr[j % size])
				{
					// When rotating element are different
					counter += 1;
				}
				j += 1;
				k += 1;
			}
			if (result < counter)
			{
				// Get a new result
				result = counter;
			}
			i += 1;
		}
		// Display calculated result
		print(" Max Hamming Distance : ", result ," ");
	}
}
func main()
{
	let op: Calculation = Calculation();
	let arr: [Int] = [1, 3, 3, 3, 3, 7];
	// Get the size
	let size: Int = arr.count;
	/*
			array = {1, 3, 3, 3, 3, 7}

			1-st rotation
			==================
			{3, 3, 3, 3, 7, 1} Rotated array
			{1, 3, 3, 3, 3, 7} Actual array
			---------------------
			 1  0  0  0  1   1 = 3 Hamming distance
			--------------------- 
			result = 3


			2-nd rotation
			==================
			{3, 3, 3, 7, 1, 3} Rotated array
			{1, 3, 3, 3, 3, 7} Actual array
			---------------------
			 1  0  0  1  1   1 = 4 Hamming distance
			--------------------- 
			result = 4


			3-rd rotation
			==================
			{3, 3, 7, 1, 3, 3} Rotated array
			{1, 3, 3, 3, 3, 7} Actual array
			---------------------
			 1  0  1  1  0  1 = 4 Hamming distance
			--------------------- 
			result = 4 
			
			4-th rotation
			==================
			{3, 7, 1, 3, 3, 3} Rotated array
			{1, 3, 3, 3, 3, 7} Actual array
			---------------------
			 1  1  1  0  0  1 = 4 Hamming distance
			--------------------- 
			result = 4 


			5-th rotation
			{1, 1, 3, 3, 3, 3} Rotated array
			{1, 3, 3, 3, 3, 7} Actual array
			---------------------
			 0  1  0  0  0  1 = 2 Hamming distance
			--------------------- 
			result = 4 
			
			*/
	op.hammingDistance(arr, size);
}
main();

Output

 Max Hamming Distance :  4
/*
  Kotlin Program
  Find a rotation with maximum hamming distance
*/
class Calculation
{
    // Calculates the maximum hamming distance of a rotating array
    fun hammingDistance(arr: Array<Int>, size: Int): Unit
    {
        // Define resultant variables
        var result: Int = 0;
        var counter: Int = 0;
        // Loop controlling variables
        var i: Int = 1;
        var j: Int ;
        var k: Int ;
        // Outer loop execute 1..(n-1) times
        while (i<size && counter<size)
        {
            counter = 0;
            // Perform [i] right side rotation and calculates the new hamming distance
            j = i;
            k = 0;
            while (j < size + i)
            {
                if (arr[k] != arr[j % size])
                {
                    // When rotating element are different
                    counter += 1;
                }
                j += 1;
                k += 1;
            }
            if (result<counter)
            {
                // Get a new result
                result = counter;
            }
            i += 1;
        }
        // Display calculated result
        print(" Max Hamming Distance : " + result + " \n");
    }
}
fun main(args: Array<String>): Unit
{
    var op: Calculation = Calculation();
    var arr: Array<Int> = arrayOf(1, 3, 3, 3, 3, 7);
    // Get the size
    var size: Int = arr.count();
    /*
    array = {1, 3, 3, 3, 3, 7}

    1-st rotation
    ==================
    {3, 3, 3, 3, 7, 1} Rotated array
    {1, 3, 3, 3, 3, 7} Actual array
    ---------------------
     1  0  0  0  1   1 = 3 Hamming distance
    --------------------- 
    result = 3


    2-nd rotation
    ==================
    {3, 3, 3, 7, 1, 3} Rotated array
    {1, 3, 3, 3, 3, 7} Actual array
    ---------------------
     1  0  0  1  1   1 = 4 Hamming distance
    --------------------- 
    result = 4


    3-rd rotation
    ==================
    {3, 3, 7, 1, 3, 3} Rotated array
    {1, 3, 3, 3, 3, 7} Actual array
    ---------------------
     1  0  1  1  0  1 = 4 Hamming distance
    --------------------- 
    result = 4 
    
    4-th rotation
    ==================
    {3, 7, 1, 3, 3, 3} Rotated array
    {1, 3, 3, 3, 3, 7} Actual array
    ---------------------
     1  1  1  0  0  1 = 4 Hamming distance
    --------------------- 
    result = 4 


    5-th rotation
    {1, 1, 3, 3, 3, 3} Rotated array
    {1, 3, 3, 3, 3, 7} Actual array
    ---------------------
     0  1  0  0  0  1 = 2 Hamming distance
    --------------------- 
    result = 4 
    
    */
    op.hammingDistance(arr, size);
}

Output

 Max Hamming Distance : 4




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







Jatin Bhardwaj     394 Day ago
I Am In Urgent Need For Placemet Opportunities To Resolve My Financial Issues Guide Me In That To Understand My Need And Help Me To Guide Me So That My Doubts About Coding Questions Unsolved On HackerEarth And Other Platforms And Reply My Query Now On Above Given Email Address Now.
Jatin Bhardwaj     394 Day ago
Please Help Me To Solve My Doubt. First Of All My First Doubt Is https://www.hackerearth.com/ Maximum Inequalities. Please Provide Help As Soon As Possible And Solutions Of Unsolved New Difficult Coding Questions For Placements In Java As Soon As Possible. Please Reply My Query.
Jatin Bhardwaj     394 Day ago
I Want To Have One Query About Some Topic.I Want To Inquire About Paid Courses Free Enrollment.I Want Coding Questions Solutions Of HackerEarth CodeWith Sagar And Other Hiring Platforms.My EmailID Is jatinbhardwaaj18@gmail.com And Whats App Contact Is 9557041140.