Rishikesh Kumar Rishi has Published 1156 Articles

Golang Program to find the odd-occurring elements in a given array

Rishikesh Kumar Rishi

Rishikesh Kumar Rishi

Updated on 18-Mar-2021 05:37:15

313 Views

ExamplesFor example, arr = [1, 4, 5, 1, 4, 5, 1] => Odd-occurring element in the array is: 1Approach to solve this problemStep 1 − Define method that accepts an array.Step 2 − Declare a xor variable, i.e., xor := 0.Step 3 − Iterate input array and perform xor operation ... Read More

Golang Program to check the power of 4 of a given number

Rishikesh Kumar Rishi

Rishikesh Kumar Rishi

Updated on 18-Mar-2021 05:35:10

205 Views

ExamplesFor example, n = 12 => 12 is not the power of 4.For example, n = 64 => 64 is the power of 4.Approach to solve this problemStep 1 − Define a method that accepts a number n.Step 2 − Divide log(n) by log(4), store in res.Step 3 − If ... Read More

Golang Program to round up the next previous power of 2.

Rishikesh Kumar Rishi

Rishikesh Kumar Rishi

Updated on 18-Mar-2021 05:31:42

241 Views

ExamplesFor example, n = 12 => Previous number power of 2 is 8.For example, n = 20 => Previous number power of 2 is 16.Approach to solve this problemStep 1 − Define a method that accepts a number n.Step 2 − Perform n | (n >> k), where k is ... Read More

Golang Program to round up the next highest power of 2.

Rishikesh Kumar Rishi

Rishikesh Kumar Rishi

Updated on 18-Mar-2021 05:29:55

569 Views

ExamplesFor example, n = 12 => Next number power of 2 is 16.For example, n = 20 => Next number power of 2 is 32.Approach to solve this problemStep 1 − Define method, that accepts a number n.Step 2 − Iterate k := 1 until k < n.Step 3 − In a loop, calculate k

Golang Program to find the minimum and maximum number, using binary operations.

Rishikesh Kumar Rishi

Rishikesh Kumar Rishi

Updated on 17-Mar-2021 11:39:20

232 Views

ExamplesFor example, x = 12, y = 15 => Maximum number is 15.For example, x = 13, y = 17 => Minimum number is 13.Approach to solve this problemStep 1 − Define method, findMax and findMin, that accept two integer x and y.Step 2 − Return integer according to defined ... Read More

Golang Program to count the set bits in an integer.

Rishikesh Kumar Rishi

Rishikesh Kumar Rishi

Updated on 17-Mar-2021 11:38:50

2K+ Views

ExamplesFor example, 101, 11, 11011 and 1001001 set bits count 2, 2, 4 and 3 respectively.Approach to solve this problemStep 1 − Convert number into binary representation.Step 2 − Count the number of 1s; return count.Example Live Demopackage main import (    "fmt"    "strconv" ) func NumOfSetBits(n int) int{   ... Read More

Golang Program to check if the binary representation of a number is palindrome or not

Rishikesh Kumar Rishi

Rishikesh Kumar Rishi

Updated on 17-Mar-2021 11:26:31

260 Views

ExamplesFor example, 101, 11, 11011, 1001001 are Palindrome. 100, 10010 are not Palindrome.Approach to solve this problemStep 1 − Convert number into binary representation.Step 2 − Traverse the converted binary representation from both side and check whether representation is palindrome or not.Example Live Demopackage main import (    "fmt"    "strconv" ... Read More

Golang Program to count the number of flips to convert a given integer to another.

Rishikesh Kumar Rishi

Rishikesh Kumar Rishi

Updated on 17-Mar-2021 11:20:53

152 Views

ExamplesConsider two numbers m = 65 => 01000001 and n = 80 => 01010000Number of bits flipped is 2.Approach to solve this problemStep 1 − Convert both numbers into bits.Step 2 − Count number of bits are flipped.Example Live Demopackage main import (    "fmt"    "strconv" ) func FindBits(x, y ... Read More

Golang Program to find the parity of a given number.

Rishikesh Kumar Rishi

Rishikesh Kumar Rishi

Updated on 17-Mar-2021 11:17:40

906 Views

Definition − Parity refers to the count of 1s. If count of 1s is even, then it’s even parity; and if the count of 1s is odd, then the parity is Odd.ExamplesConsider n = 20(00010100)Parity of the given number 20 is even.Approach to solve this problemStep 1 − Define a ... Read More

Golang Program to find the position of the rightmost set bit

Rishikesh Kumar Rishi

Rishikesh Kumar Rishi

Updated on 17-Mar-2021 11:11:57

317 Views

ExamplesConsider n = 20(00010100)Now return log2(20 & -20) => 2+1 => 3Approach to solve this problemStep 1 − Define a method, where n and is an argument, return type is int.Step 2 − Return log2(n & -n)+1.Examplepackage main import (    "fmt"    "math"    "strconv" ) func FindRightMostSetBit(n int) ... Read More

Advertisements