Check if permutation of number is power of 2 or not

Here given code implementation process.

// C program
// Check if permutation of number is power of 2 or not
#include <stdio.h>

// Check the both number have frequency is same or not
int isSameDigit(int num, int power)
{
	// Use to count number digit frequency
	// Digit [0 - 9]
	int a[10];
	int b[10];
	// Loop controlling variable
	int i = 0;
	// Set the initial frequency
	for (i = 0; i < 10; ++i)
	{
		a[i] = 0;
		b[i] = 0;
	}
	// Count digit frequency of num
	while (num > 0)
	{
		a[num % 10] += 1;
		// Remove last digit
		num = num / 10;
	}
	// Count digit frequency of power value
	while (power > 0)
	{
		b[power % 10]++;
		// Remove last digit
		power = power / 10;
	}
	// Compare frequency of both number
	for (i = 0; i < 10; ++i)
	{
		if (a[i] != b[i])
		{
			// When frequency are not same
			return 0;
		}
	}
	return 1;
}
void isPowerof2(int num)
{
	int result = 0;
	printf("\n Given num : %d", num);
	if (num > 0)
	{
		// Check all power of 2 in 32 bit integer
		for (int i = 0; i < 31 && result == 0; ++i)
		{
			if (num == (1 << i) || isSameDigit(num, 1 << i) == 1)
			{
				// Get resultant power
				result = (1 << i);
			}
		}
	}
	if (result != 0)
	{
		printf("\n Permutation %d is power of 2\n", result);
	}
	else
	{
		printf("\n No permutation of number %d is power of 2\n", num);
	}
}
int main(int argc, char
	const *argv[])
{
	// Test cases
	isPowerof2(76328);
	isPowerof2(1234);
	isPowerof2(8240);
	isPowerof2(23);
	isPowerof2(21);
	return 0;
}

input

 Given num : 76328
 Permutation 32768 is power of 2

 Given num : 1234
 No permutation of number 1234 is power of 2

 Given num : 8240
 Permutation 2048 is power of 2

 Given num : 23
 Permutation 32 is power of 2

 Given num : 21
 No permutation of number 21 is power of 2
// Java program for
// Check if permutation of number is power of 2 or not
public class PermutationPower
{
	// Check the both number have frequency is same or not
	public int isSameDigit(int num, int power)
	{
		// Use to count number digit frequency
		// Digit [0 - 9]
		int[] a = new int[10];
		int[] b = new int[10];
		// Loop controlling variable
		int i = 0;
		// Set the initial frequency
		for (i = 0; i < 10; ++i)
		{
			a[i] = 0;
			b[i] = 0;
		}
		// Count digit frequency of num
		while (num > 0)
		{
			a[num % 10] += 1;
			// Remove last digit
			num = num / 10;
		}
		// Count digit frequency of power value
		while (power > 0)
		{
			b[power % 10]++;
			// Remove last digit
			power = power / 10;
		}
		// Compare frequency of both number
		for (i = 0; i < 10; ++i)
		{
			if (a[i] != b[i])
			{
				// When frequency are not same
				return 0;
			}
		}
		return 1;
	}
	public void isPowerof2(int num)
	{
		int result = 0;
		System.out.print("\n Given num : " + num);
		if (num > 0)
		{
			// Check all power of 2 in 32 bit integer
			for (int i = 0; i < 31 && result == 0; ++i)
			{
				if (num == (1 << i) || isSameDigit(num, 1 << i) == 1)
				{
					// Get resultant power
					result = (1 << i);
				}
			}
		}
		if (result != 0)
		{
			System.out.print("\n Permutation " + result + " is power of 2\n");
		}
		else
		{
			System.out.print("\n No permutation of number " + num + " is power of 2\n");
		}
	}
	public static void main(String[] args)
	{
		PermutationPower task = new PermutationPower();
		// Test cases
		task.isPowerof2(76328);
		task.isPowerof2(1234);
		task.isPowerof2(8240);
		task.isPowerof2(23);
		task.isPowerof2(21);
	}
}

input

 Given num : 76328
 Permutation 32768 is power of 2

 Given num : 1234
 No permutation of number 1234 is power of 2

 Given num : 8240
 Permutation 2048 is power of 2

 Given num : 23
 Permutation 32 is power of 2

 Given num : 21
 No permutation of number 21 is power of 2
// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Check if permutation of number is power of 2 or not

class PermutationPower
{
	public:
		// Check the both number have frequency is same or not
		int isSameDigit(int num, int power)
		{
			// Use to count number digit frequency
			// Digit [0 - 9]
			int a[10];
			int b[10];
			// Loop controlling variable
			int i = 0;
			// Set the initial frequency
			for (i = 0; i < 10; ++i)
			{
				a[i] = 0;
				b[i] = 0;
			}
			// Count digit frequency of num
			while (num > 0)
			{
				a[num % 10] += 1;
				// Remove last digit
				num = num / 10;
			}
			// Count digit frequency of power value
			while (power > 0)
			{
				b[power % 10]++;
				// Remove last digit
				power = power / 10;
			}
			// Compare frequency of both number
			for (i = 0; i < 10; ++i)
			{
				if (a[i] != b[i])
				{
					// When frequency are not same
					return 0;
				}
			}
			return 1;
		}
	void isPowerof2(int num)
	{
		int result = 0;
		cout << "\n Given num : " << num;
		if (num > 0)
		{
			// Check all power of 2 in 32 bit integer
			for (int i = 0; i < 31 && result == 0; ++i)
			{
				if (num == (1 << i) || this->isSameDigit(num, 1 << i) == 1)
				{
					// Get resultant power
					result = (1 << i);
				}
			}
		}
		if (result != 0)
		{
			cout << "\n Permutation " << result << " is power of 2\n";
		}
		else
		{
			cout << "\n No permutation of number " << num << " is power of 2\n";
		}
	}
};
int main()
{
	PermutationPower *task = new PermutationPower();
	// Test cases
	task->isPowerof2(76328);
	task->isPowerof2(1234);
	task->isPowerof2(8240);
	task->isPowerof2(23);
	task->isPowerof2(21);
	return 0;
}

input

 Given num : 76328
 Permutation 32768 is power of 2

 Given num : 1234
 No permutation of number 1234 is power of 2

 Given num : 8240
 Permutation 2048 is power of 2

 Given num : 23
 Permutation 32 is power of 2

 Given num : 21
 No permutation of number 21 is power of 2
// Include namespace system
using System;
// Csharp program for
// Check if permutation of number is power of 2 or not
public class PermutationPower
{
	// Check the both number have frequency is same or not
	public int isSameDigit(int num, int power)
	{
		// Use to count number digit frequency
		// Digit [0 - 9]
		int[] a = new int[10];
		int[] b = new int[10];
		// Loop controlling variable
		int i = 0;
		// Set the initial frequency
		for (i = 0; i < 10; ++i)
		{
			a[i] = 0;
			b[i] = 0;
		}
		// Count digit frequency of num
		while (num > 0)
		{
			a[num % 10] += 1;
			// Remove last digit
			num = num / 10;
		}
		// Count digit frequency of power value
		while (power > 0)
		{
			b[power % 10]++;
			// Remove last digit
			power = power / 10;
		}
		// Compare frequency of both number
		for (i = 0; i < 10; ++i)
		{
			if (a[i] != b[i])
			{
				// When frequency are not same
				return 0;
			}
		}
		return 1;
	}
	public void isPowerof2(int num)
	{
		int result = 0;
		Console.Write("\n Given num : " + num);
		if (num > 0)
		{
			// Check all power of 2 in 32 bit integer
			for (int i = 0; i < 31 && result == 0; ++i)
			{
				if (num == (1 << i) || this.isSameDigit(num, 1 << i) == 1)
				{
					// Get resultant power
					result = (1 << i);
				}
			}
		}
		if (result != 0)
		{
			Console.Write("\n Permutation " + result + " is power of 2\n");
		}
		else
		{
			Console.Write("\n No permutation of number " + num + " is power of 2\n");
		}
	}
	public static void Main(String[] args)
	{
		PermutationPower task = new PermutationPower();
		// Test cases
		task.isPowerof2(76328);
		task.isPowerof2(1234);
		task.isPowerof2(8240);
		task.isPowerof2(23);
		task.isPowerof2(21);
	}
}

input

 Given num : 76328
 Permutation 32768 is power of 2

 Given num : 1234
 No permutation of number 1234 is power of 2

 Given num : 8240
 Permutation 2048 is power of 2

 Given num : 23
 Permutation 32 is power of 2

 Given num : 21
 No permutation of number 21 is power of 2
<?php
// Php program for
// Check if permutation of number is power of 2 or not
class PermutationPower
{
	// Check the both number have frequency is same or not
	public	function isSameDigit($num, $power)
	{
		// Use to count number digit frequency
		// Digit [0 - 9]
		// Set the initial frequency
		$a = array_fill(0, 10, 0);
		$b = array_fill(0, 10, 0);
		// Loop controlling variable
		$i = 0;
		// Count digit frequency of num
		while ($num > 0)
		{
			$a[$num % 10] += 1;
			// Remove last digit
			$num = (int)($num / 10);
		}
		// Count digit frequency of power value
		while ($power > 0)
		{
			$b[$power % 10]++;
			// Remove last digit
			$power = (int)($power / 10);
		}
		// Compare frequency of both number
		for ($i = 0; $i < 10; ++$i)
		{
			if ($a[$i] != $b[$i])
			{
				// When frequency are not same
				return 0;
			}
		}
		return 1;
	}
	public	function isPowerof2($num)
	{
		$result = 0;
		echo("\n Given num : ".$num);
		if ($num > 0)
		{
			// Check all power of 2 in 32 bit integer
			for ($i = 0; $i < 31 && $result == 0; ++$i)
			{
				if ($num == (1 << $i) || $this->isSameDigit($num, 1 << $i) == 1)
				{
					// Get resultant power
					$result = (1 << $i);
				}
			}
		}
		if ($result != 0)
		{
			echo("\n Permutation ".$result.
				" is power of 2\n");
		}
		else
		{
			echo("\n No permutation of number ".$num.
				" is power of 2\n");
		}
	}
}

function main()
{
	$task = new PermutationPower();
	// Test cases
	$task->isPowerof2(76328);
	$task->isPowerof2(1234);
	$task->isPowerof2(8240);
	$task->isPowerof2(23);
	$task->isPowerof2(21);
}
main();

input

 Given num : 76328
 Permutation 32768 is power of 2

 Given num : 1234
 No permutation of number 1234 is power of 2

 Given num : 8240
 Permutation 2048 is power of 2

 Given num : 23
 Permutation 32 is power of 2

 Given num : 21
 No permutation of number 21 is power of 2
// Node JS program for
// Check if permutation of number is power of 2 or not
class PermutationPower
{
	// Check the both number have frequency is same or not
	isSameDigit(num, power)
	{
		// Use to count number digit frequency
		// Digit [0 - 9]
		// Set the initial frequency
		var a = Array(10).fill(0);
		var b = Array(10).fill(0);
		// Loop controlling variable
		var i = 0;
		// Count digit frequency of num
		while (num > 0)
		{
			a[num % 10] += 1;
			// Remove last digit
			num = parseInt(num / 10);
		}
		// Count digit frequency of power value
		while (power > 0)
		{
			b[power % 10]++;
			// Remove last digit
			power = parseInt(power / 10);
		}
		// Compare frequency of both number
		for (i = 0; i < 10; ++i)
		{
			if (a[i] != b[i])
			{
				// When frequency are not same
				return 0;
			}
		}
		return 1;
	}
	isPowerof2(num)
	{
		var result = 0;
		process.stdout.write("\n Given num : " + num);
		if (num > 0)
		{
			// Check all power of 2 in 32 bit integer
			for (var i = 0; i < 31 && result == 0; ++i)
			{
				if (num == (1 << i) || this.isSameDigit(num, 1 << i) == 1)
				{
					// Get resultant power
					result = (1 << i);
				}
			}
		}
		if (result != 0)
		{
			process.stdout.write("\n Permutation " + result + " is power of 2\n");
		}
		else
		{
			process.stdout.write("\n No permutation of number " + num + " is power of 2\n");
		}
	}
}

function main()
{
	var task = new PermutationPower();
	// Test cases
	task.isPowerof2(76328);
	task.isPowerof2(1234);
	task.isPowerof2(8240);
	task.isPowerof2(23);
	task.isPowerof2(21);
}
main();

input

 Given num : 76328
 Permutation 32768 is power of 2

 Given num : 1234
 No permutation of number 1234 is power of 2

 Given num : 8240
 Permutation 2048 is power of 2

 Given num : 23
 Permutation 32 is power of 2

 Given num : 21
 No permutation of number 21 is power of 2
#  Python 3 program for
#  Check if permutation of number is power of 2 or not
class PermutationPower :
	#  Check the both number have frequency is same or not
	def isSameDigit(self, num, power) :
		#  Use to count number digit frequency
		#  Digit [0 - 9]
		#  Set the initial frequency
		a = [0] * (10)
		b = [0] * (10)
		#  Loop controlling variable
		i = 0
		#  Count digit frequency of num
		while (num > 0) :
			a[num % 10] += 1
			#  Remove last digit
			num = int(num / 10)
		
		#  Count digit frequency of power value
		while (power > 0) :
			b[power % 10] += 1
			#  Remove last digit
			power = int(power / 10)
		
		#  Compare frequency of both number
		i = 0
		while (i < 10) :
			if (a[i] != b[i]) :
				#  When frequency are not same
				return 0
			
			i += 1
		
		return 1
	
	def isPowerof2(self, num) :
		result = 0
		print("\n Given num : ", num, end = "")
		if (num > 0) :
			#  Check all power of 2 in 32 bit integer
			i = 0
			while (i < 31 and result == 0) :
				if (num == (1 << i) or self.isSameDigit(num, 1 << i) == 1) :
					#  Get resultant power
					result = (1 << i)
				
				i += 1
			
		
		if (result != 0) :
			print("\n Permutation", result ,"is power of 2")
		else :
			print("\n No permutation of number", num ,"is power of 2")
		
	

def main() :
	task = PermutationPower()
	#  Test cases
	task.isPowerof2(76328)
	task.isPowerof2(1234)
	task.isPowerof2(8240)
	task.isPowerof2(23)
	task.isPowerof2(21)

if __name__ == "__main__": main()

input

 Given num :  76328
 Permutation 32768 is power of 2

 Given num :  1234
 No permutation of number 1234 is power of 2

 Given num :  8240
 Permutation 2048 is power of 2

 Given num :  23
 Permutation 32 is power of 2

 Given num :  21
 No permutation of number 21 is power of 2
#  Ruby program for
#  Check if permutation of number is power of 2 or not
class PermutationPower 
	#  Check the both number have frequency is same or not
	def isSameDigit(num, power) 
		#  Use to count number digit frequency
		#  Digit [0 - 9]
		#  Set the initial frequency
		a = Array.new(10) {0}
		b = Array.new(10) {0}
		#  Loop controlling variable
		i = 0
		#  Count digit frequency of num
		while (num > 0) 
			a[num % 10] += 1
			#  Remove last digit
			num = num / 10
		end

		#  Count digit frequency of power value
		while (power > 0) 
			b[power % 10] += 1
			#  Remove last digit
			power = power / 10
		end

		#  Compare frequency of both number
		i = 0
		while (i < 10) 
			if (a[i] != b[i]) 
				#  When frequency are not same
				return 0
			end

			i += 1
		end

		return 1
	end

	def isPowerof2(num) 
		result = 0
		print("\n Given num : ", num)
		if (num > 0) 
			#  Check all power of 2 in 32 bit integer
			i = 0
			while (i < 31 && result == 0) 
				if (num == (1 << i) || self.isSameDigit(num, 1 << i) == 1) 
					#  Get resultant power
					result = (1 << i)
				end

				i += 1
			end

		end

		if (result != 0) 
			print("\n Permutation ", result ," is power of 2\n")
		else 
			print("\n No permutation of number ", num ," is power of 2\n")
		end

	end

end

def main() 
	task = PermutationPower.new()
	#  Test cases
	task.isPowerof2(76328)
	task.isPowerof2(1234)
	task.isPowerof2(8240)
	task.isPowerof2(23)
	task.isPowerof2(21)
end

main()

input

 Given num : 76328
 Permutation 32768 is power of 2

 Given num : 1234
 No permutation of number 1234 is power of 2

 Given num : 8240
 Permutation 2048 is power of 2

 Given num : 23
 Permutation 32 is power of 2

 Given num : 21
 No permutation of number 21 is power of 2
// Scala program for
// Check if permutation of number is power of 2 or not
class PermutationPower()
{
	// Check the both number have frequency is same or not
	def isSameDigit(n: Int, p: Int): Int = {
      	var num = n;
      	var power = p;
		// Use to count number digit frequency
		// Digit [0 - 9]
		// Set the initial frequency
		var a: Array[Int] = Array.fill[Int](10)(0);
		var b: Array[Int] = Array.fill[Int](10)(0);
		// Loop controlling variable
		var i: Int = 0;
		// Count digit frequency of num
		while (num > 0)
		{
			a(num % 10) += 1;
			// Remove last digit
			num = num / 10;
		}
		// Count digit frequency of power value
		while (power > 0)
		{
			b(power % 10) += 1;
			// Remove last digit
			power = power / 10;
		}
		// Compare frequency of both number
		while (i < 10)
		{
			if (a(i) != b(i))
			{
				// When frequency are not same
				return 0;
			}
			i += 1;
		}
		return 1;
	}
	def isPowerof2(num: Int): Unit = {
		var result: Int = 0;
		print("\n Given num : " + num);
		if (num > 0)
		{
			// Check all power of 2 in 32 bit integer
			var i: Int = 0;
			while (i < 31 && result == 0)
			{
				if (num == (1 << i) || isSameDigit(num, 1 << i) == 1)
				{
					// Get resultant power
					result = (1 << i);
				}
				i += 1;
			}
		}
		if (result != 0)
		{
			print("\n Permutation " + result + " is power of 2\n");
		}
		else
		{
			print("\n No permutation of number " + num + " is power of 2\n");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: PermutationPower = new PermutationPower();
		// Test cases
		task.isPowerof2(76328);
		task.isPowerof2(1234);
		task.isPowerof2(8240);
		task.isPowerof2(23);
		task.isPowerof2(21);
	}
}

input

 Given num : 76328
 Permutation 32768 is power of 2

 Given num : 1234
 No permutation of number 1234 is power of 2

 Given num : 8240
 Permutation 2048 is power of 2

 Given num : 23
 Permutation 32 is power of 2

 Given num : 21
 No permutation of number 21 is power of 2
// Swift 4 program for
// Check if permutation of number is power of 2 or not
class PermutationPower
{
	// Check the both number have frequency is same or not
	func isSameDigit(_ n: Int, _ p: Int) -> Int
	{
      	var num = n;
      	var power = p;
		// Use to count number digit frequency
		// Digit [0 - 9]
		// Set the initial frequency
		var a: [Int] = Array(repeating: 0, count: 10);
		var b: [Int] = Array(repeating: 0, count: 10);
		// Loop controlling variable
		var i: Int = 0;
		// Count digit frequency of num
		while (num > 0)
		{
			a[num % 10] += 1;
			// Remove last digit
			num = num / 10;
		}
		// Count digit frequency of power value
		while (power > 0)
		{
			b[power % 10] += 1;
			// Remove last digit
			power = power / 10;
		}
		// Compare frequency of both number
		i = 0;
		while (i < 10)
		{
			if (a[i] != b[i])
			{
				// When frequency are not same
				return 0;
			}
			i += 1;
		}
		return 1;
	}
	func isPowerof2(_ num: Int)
	{
		var result: Int = 0;
		print("\n Given num : ", num, terminator: "");
		if (num > 0)
		{
			// Check all power of 2 in 32 bit integer
			var i: Int = 0;
			while (i < 31 && result == 0)
			{
				if (num == (1 << i) || self.isSameDigit(num, 1 << i) == 1)
				{
					// Get resultant power
					result = (1 << i);
				}
				i += 1;
			}
		}
		if (result != 0)
		{
			print("\n Permutation ", result ," is power of 2");
		}
		else
		{
			print("\n No permutation of number ", num ," is power of 2");
		}
	}
}
func main()
{
	let task: PermutationPower = PermutationPower();
	// Test cases
	task.isPowerof2(76328);
	task.isPowerof2(1234);
	task.isPowerof2(8240);
	task.isPowerof2(23);
	task.isPowerof2(21);
}
main();

input

 Given num :  76328
 Permutation  32768  is power of 2

 Given num :  1234
 No permutation of number  1234  is power of 2

 Given num :  8240
 Permutation  2048  is power of 2

 Given num :  23
 Permutation  32  is power of 2

 Given num :  21
 No permutation of number  21  is power of 2


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







© 2021, kalkicode.com, All rights reserved