Wednesday, 1 Dec 2021
Category: binary-representation

#include using namespace std;  int binaryToDecimal(string n){    string num = n;    int dec_value = 0;          int base = 1;      int len = num.length();    for (int i = len – 1; i >= 0; i–) {        if (num[i] == ‘1’)            dec_value += base;        base = base * 2;    }      return dec_value;}  string decimalToBinary(int n){        string binstr = “”;      while (n > 0) {                          binstr += (n % 2 + 48);        n = […]

Given an array arr[] consisting of N integers, the task is to find the sum of all subsets from an array, whose sum is a Perfect Number. Examples: Input: arr[] = {5, 4, 6}Output: 6Explanation:All possible subsets from the array arr[] are:{5} → Sum = 5{4} → Sum = 4.{6} → Sum = 6.{5, 4} […]

Sum of numbers obtained by the count of set and non-set bits in diagonal matrix elements Given a square matrix mat[][] of dimension N*N, convert the elements present in both the diagonals to their respective binary representations and perform the following operations: For every position of bits, count the number of set bits and non-set […]

Generate a circular permutation with number of mismatching bits between pairs of adjacent elements exactly 1 Given two integers N and S, the task is to find a circular permutation of numbers from the range [0, 2(N – 1)], starting with S such that the count of mismatching bits between any pair of adjacent numbers […]