Found 1082 Articles for Go Programming

Golang program to check if k’th bit is set for a given number or not.

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 10:58:33

1K+ Views

ExamplesConsider n = 20(00010100), k = 3So, the result after turning off the 3rd bit => 00010000 & (1

Golang Program to turn on the k’th bit in a number.

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 10:56:25

126 Views

ExampleFor example consider n = 20(00010100), k = 4. So result after turning on 4th bit => 00010000 | (1

Golang program to turn off the k’th bit in a number.

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 10:54:15

107 Views

ExampleConsider n = 20(00010100), k = 3 The result after turning off the 3rd bit => 00010000 & ^(1 16sApproach to solve this problemStep 1 − Define a method, where n and k would be the arguments, return type is int.Step 2 − Perform AND operation with n & ^(1

Golang program to define a binary tree

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 10:52:12

540 Views

Example − In the given tree, the root node is 1, the root of its left sub tree is 2, and the root of its right sub tree is 3, ... so on.Preorder Tree Traversal Output: 1, 2, 4, 5, 3, 6, 7.Approach to solve this problemStep 1 − First, we’ll define the node structure.Step 2 − In the main method, we would create the above tree.Step 3 − Finally, we will perform the Preorder Tree Traversal.Example Live Demopackage main import "fmt" type Node struct {    data int    left *Node    right *Node } func (root *Node)PreOrderTraversal(){    if ... Read More

Selection Sort in Go Lang

Dev Prakash Sharma
Updated on 23-Feb-2021 18:27:34

1K+ Views

Selection sort is a sorting algorithm that is used to sort the elements by repeatedly finding the minimum element and placing it at the first in the unsorted part of the array. After placing the minimum element in one end of the array, the array is then divided into two subparts which can be again sorted using the algorithm.For ExampleInputarr[ ] = {2, 9, 4, 3, 5, 1}Output1 2 3 4 5 9ExplanationAfter sorting the given array, it becomes 1, 2, 3, 4, 5, 9Algorithm:Take an array of integers as the input.Find the index of the minimum element by iterating ... Read More

Nth Catalan Number in Go Lang

Dev Prakash Sharma
Updated on 05-Feb-2021 12:45:36

134 Views

Catalan Numbers are sequences of natural numbers that gives the number of BST (Binary Search Tree) possible with n Values. So, the Catalan number is a full binary tree with n+1 leaves.Some applications of Catalan Numbers are counting the pairs of nested parentheses, valid mountain ranges etc.For n = 5, C = (C(0) * C(4)) + (C(1) * C(3)) + (C(2) * C(2)) + (C(3) * C(1)) + (C(4)* C(0))Thus, we can see that Catalan numbers are in the form of a recursive relation, i.e., for the nth term, the Catalan number Cn is,             ... Read More

Write a Golang program to find odd and even numbers using bit operation

Kiran P
Updated on 04-Feb-2021 11:31:15

461 Views

ExamplesInput num = 5 => 101 & 1 = 001 => True, i.e., Odd; else num would be Even.Approach to solve this problemStep 1: Define a method that accepts a number.Step 2: Perform & operation with that number.Step 3: If the & operator returns a non-zero value, then that number would be odd.Step 4: Else, the number would be even.ProgramLive Demopackage main import "fmt" func oddEven(num int){    if num & 1 != 0 {       fmt.Println("ODD")    } else {       fmt.Println("EVEN")    } } func main(){    oddEven(13)    oddEven(50)    oddEven(0) }OutputODD EVEN EVEN

Write a Golang program to find duplicate elements in a given range

Kiran P
Updated on 04-Feb-2021 11:29:49

495 Views

We can solve this problem in two different ways. Let’s check the first method.Method 1: ExamplesInput Array = [1, 2, 3, 4, 4] => Range is from 1 to 5 but 4 is a duplicate element in this range.Approach to solve this problemStep 1: Define a method that accepts an array.Step 2: Declare a visited map.Step 3: Iterate the given array. If the element exists in the visited map, then return that element.Step 4: Else, return -1.ProgramLive Demopackage main import "fmt" func duplicateInArray(arr []int) int{    visited := make(map[int]bool, 0)    for i:=0; i Range is from 1 to 5 but 4 ... Read More

Write a Golang program to find duplicate elements in a given array

Kiran P
Updated on 04-Feb-2021 11:30:03

5K+ Views

ExamplesInput Array = [1, 3, 5, 6, 1] => Duplicate element is 1;Input Array = [1, 3, 5, 6, 7] => return -1Approach to solve this problemStep 1: Define a method that accepts an array.Step 2: Declare a visited map.Step 3: Iterate the given array. If the element exists in the visited map, then return that element.Step 4: Else, return -1.ProgramLive Demopackage main import "fmt" func duplicateInArray(arr []int) int{    visited := make(map[int]bool, 0)    for i:=0; i

Write a Golang program to sort a binary array in linear time

Kiran P
Updated on 04-Feb-2021 11:30:28

245 Views

There are two methods in which we can solve this problem. Let’s check the first method.Method 1ExamplesInput Array = [1, 0, 1, 0, 1, 0, 0, 1] => [0, 0, 0, 0, 1, 1, 1, 1]Approach to solve this problemStep 1: Define a method that accepts an array.Step 2: Count number of 0.Step 3: Store 0 till count becomes 0 and store 1 at the remaining indexes.Step 4: At the end, return the array.ProgramLive Demopackage main import "fmt" func binarySort(arr []int) []int{    count := 0    for i:=0; i

Advertisements