


For all possible binary numbers of length n, the sum of the two halves is equal?
Here we will see all possible binary numbers of n bit (n is given by the user) where the sum of each half is same. For example, if the number is 10001 here 10 and 01 are same because their sum is same, and they are in the different halves. Here we will generate all numbers of that type.
Algorithm
genAllBinEqualSumHalf(n, left, right, diff)
left and right are initially empty, diff is holding difference between left and right
Begin if n is 0, then if diff is 0, then print left + right end if return end if if n is 1, then if diff is 0, then print left + 0 + right print left + 1 + right end if return end if if 2* |diff| <= n, then if left is not blank, then genAllBinEqualSumHalf(n-2, left + 0, right + 0, diff) genAllBinEqualSumHalf(n-2, left + 0, right + 1, diff-1) end if genAllBinEqualSumHalf(n-2, left + 1, right + 0, diff + 1) genAllBinEqualSumHalf(n-2, left + 1, right + 1, diff) end if End
Example
#include <bits/stdc++.h> using namespace std; //left and right strings will be filled up, di will hold the difference between left and right void genAllBinEqualSumHalf(int n, string left="", string right="", int di=0) { if (n == 0) { //when the n is 0 if (di == 0) //if diff is 0, then concatenate left and right cout << left + right << " "; return; } if (n == 1) {//if 1 bit number is their if (di == 0) { //when difference is 0, generate two numbers one with 0 after left, another with 1 after left, then add right cout << left + "0" + right << " "; cout << left + "1" + right << " "; } return; } if ((2 * abs(di) <= n)) { if (left != ""){ //numbers will not start with 0 genAllBinEqualSumHalf(n-2, left+"0", right+"0", di); //add 0 after left and right genAllBinEqualSumHalf(n-2, left+"0", right+"1", di-1); //add 0 after left, and 1 after right, so difference is 1 less } genAllBinEqualSumHalf(n-2, left+"1", right+"0", di+1); //add 1 after left, and 0 after right, so difference is 1 greater genAllBinEqualSumHalf(n-2, left+"1", right+"1", di); //add 1 after left and right } } main() { int n = 5; genAllBinEqualSumHalf(n); }
输出
100001 100010 101011 110011 100100 101101 101110 110101 110110 111111
The above is the detailed content of For all possible binary numbers of length n, the sum of the two halves is equal?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



An IP address is composed of 32 or 128-bit binary numbers. IP address is a unified address format provided by the IP protocol. There are two types of IP addresses: 1. IPv4 address, consisting of 32-bit binary numbers, expressed in dotted decimal notation, divided into eight bits, that is, four 0~255 Decimal number; 2. IPv6 address consists of 128-bit binary numbers, expressed in dotted hexadecimal, divided into eight bits, that is, sixteen hexadecimal numbers from 0x00 to 0xff.

Consider the example given below - The example input is as follows: Input binary number: 10010001 The output is as follows: 1's complement of 10010001 is 011011102 The complement of 10010001 is 01101111 Algorithm Reference to an algorithm to find the 2'c's complement of a given binary number number. Step 1 - Get started. Step 2 - Read the binary number at runtime. Step 3 - Copy the binary number to strdp. Step 4-len:=strlen(str) Step 5-For i=0 to len-1, execute Step 5.1-If str[i]=='1', then Step 5.1.1-str[i]==' 0' Step 5.2-Otherwise Step 5.2.1

There is no fixed limit to the length of an array in PHP, it can be dynamically adjusted according to the system's memory size. In PHP, an array is a very flexible data structure that can store any number of elements, and each element can be a value of any type, or even another array. The length limit of PHP arrays mainly depends on the memory size of the system and the memory limit of PHP configuration. Generally speaking, if the system's memory is large enough and PHP's memory limit is high enough, the length of the array can be very large. However, if your system is low on memory or

Is there a limit to PHP array length? Need specific code examples In PHP, the array length is not subject to a fixed limit, and the array size can be dynamically adjusted according to the actual limit of the system memory. Arrays in PHP are dynamic arrays, so they can grow or shrink dynamically as needed. In PHP, an array is an ordered mapped data structure, and array elements can be accessed using array subscripts or associative array key values. Let's look at a specific code example to demonstrate whether the PHP array length is limited. First, we can pass the following code

Binary numbers are represented in base 2. It only uses the two digits "0" and "1". Each digit in a binary number is a bit. Example The two's complement number of the binary number -01000101111 The two's complement number is obtained by reversing the digits of the binary number, that is, converting 1 to 0 and 0 to 1. The complement of example 1’sComplementof101100=0100112 is the complement of the binary number plus 1, that is, the complement of 1 + 1. Example 2’scomplementof101101is010011. Example code to find one and two complement codes - #include<iostr

The purpose of this article is to implement a program that maximizes the sum of the lengths of a pair of strings that have no common characters in a given array. By definition, a string is a collection of characters. Problem Statement Implement a program to maximize the sum of lengths of a pair of strings that have no common characters in a given array. Example 1LetusconsidertheInputarray:a[]=["efgh","hat","fto","car","wxyz","fan"]Outputobtained:8 Description There are no common characters in the strings "abcd" and "wxyz". As a result, the combined length of the two strings is 4+4, which is equal to 8, which is the longest length among all feasible pairs. Example 2Letu

IPv6 addresses are composed of 128-bit binary numbers. IPv6 addresses are binary numbers represented in hexadecimal and have a 128-bit address length. An IPv6 IP address consists of 8 address sections, each section contains 16 address bits, and the total length is 16x8=128 bits.

The role and meaning of the len function is interpreted from different angles. The len function is one of the commonly used functions in the Python programming language. It is mainly used to return the length or number of elements of a container object (such as string, list, tuple, etc.). This simple function plays a very important role when writing programs, and its function and meaning can be interpreted from many angles. This article will explain the len function from the perspectives of performance, readability, and container type, and provide specific code examples. 1. Performance perspective When processing large-scale data, the performance of the program
