Found 1082 Articles for Go Programming

Write a Golang program to find pairs with the given sum in an array(O(n))

Kiran P
Updated on 04-Feb-2021 11:23:55

443 Views

ExamplesInput Array = [1, 3, 5, 7, 8, 9], sum = 11 => (3, 8)Approach to solve this problemStep 1: Define a method that accepts an array and sum.Step 2: Define a mapping variable, type map[int]int.Step 3: Iterate the given array as i.Step 4: If key in mapping sum-arr[i] is not present, then mapping[arr[i]]=i.Step 5: If present, then print “pair is found”.Step 6: At the end, print that “pair not found”.ProgramLive Demopackage main import "fmt" func findSumPair(arr []int, sum int){    mapping := make(map[int]int)    for i:=0; i

Write a Golang program to find pairs with given sum in an array(O(nlogn))

Kiran P
Updated on 04-Feb-2021 11:23:38

185 Views

ExamplesInput Array = [1, 3, 5, 7, 8, 9], sum = 11 => (3, 8)Approach to solve this problemStep 1: Define a method that accepts an array and sum.Step 2: Sort the given array, declare low:=0 and high:=size-1 variables.Step 3: Iterate a for loop until low sum, then high--.Step 5: At the end, print “pair not found”.ProgramLive Demopackage main import (    "fmt"    "sort" ) func findSumPair(arr []int, sum int){    sort.Ints(arr)    low := 0    high := len(arr) - 1    for low

Write a Golang program to find pairs with the given sum in an array(O(n2))

Kiran P
Updated on 04-Feb-2021 11:23:24

388 Views

ExamplesInput Array = [4, 1, 6, 8, 7, 2, 3], sum = 11 => (4, 7) or (8, 3)Approach to solve this problemStep 1: Define a method that accepts an array and sum.Step 2: Iterate from 0 to n as i.Step 3: Again, iterate a for loop from i+1 to n-1 as j.Step 4: If arr[i] + arr[j] == sum, then return arr[i] and arr[j].Step 5: At the end, print that pair not found.ProgramLive Demopackage main import (    "fmt" ) func findSumPair(arr []int, sum int){    for i:=0; i

Write a Golang program to find the frequency of each element in an array

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

442 Views

ExamplesInput Array = [1, 3, 4, 3, 2, 3, 4, 0, 2]Element13420Frequency13221Approach to solve this problemStep 1: Define a method that accepts an array.Step 2: Define a map, where key would be the array’s elements and the starting value is 0.Step 3: Start iterating the input array. If an element is present in the map, then increment the count.Step 4: If the element is not present in the map, then store in map and make its value 1.ProgramLive Demopackage main import (    "fmt" ) func findFrequencyOfArray(arr []int){    frequency := make(map[int]int)    for _, item := range arr{     ... Read More

Write a Golang program to check whether a given array is sorted or not (Using Bubble Sort Technique)

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

273 Views

ExamplesInput arr = [7, 15, 21, 26, 33] => Array is already sorted.Input arr = [7, 5, 1, 6, 3] => Array is not sorted.Approach to solve this problemStep 1: Iterate the array from the 0th index to n-1.Step 2: Iterate the array from the 0th index to n-1-i, where i is the index of the above loop.Step 3: If swap is not taking place in the first iteration, then print that “array is already sorted”.Step 4: If swap occurs, then print “array is not sorted”.ProgramLive Demopackage main import "fmt" func checkSortedArray(arr []int){    sortedArray := true    for i:=0; i

Write a Golang program to search an element in a sorted array

Kiran P
Updated on 04-Feb-2021 11:14:46

449 Views

Approach to solve this problemStep 1: Iterate the array from the 0th index to n-1, where n is the size of the given array.Step 2: Declare low=0th index and high=n-1. Start a for loop till low is lesser than high.Step 3: Find mid=(low+high)/2, if the element at the middle is equal to key, then return mid index.Step 4: If the element at mid is greater than the key, then make high = mid.Step 5: If the element at mid is lesser than the key, then make low = mid + 1.Step 6: If key is not present in the given ... Read More

Write a Golang program to sort an array using Bubble Sort

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

434 Views

Definition: Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order.ExamplesInput arr = [7, 5, 1, 6, 3]1st iteration => swap(7, 5) => swap(7, 1) => swap(7, 6) => swap(7, 3) => [5, 1, 6, 3, 7]2nd iteration => [1, 5, 3, 6, 7]3rd iteration => [1, 3, 5, 6, 7]4th iteration => [1, 3, 5, 6, 7]5th iteration => [1, 3, 5, 6, 7]Approach to solve this problemStep 1: Iterate the array from 0th index to n-1.Step 2: Iterate the array from the 0th index to n-1-i, ... Read More

Write a Golang program to find prime numbers in a given range

Kiran P
Updated on 04-Feb-2021 11:14:14

2K+ Views

ExamplesInput num1=3 and num2=8 => Prime numbers are: 3, 5, 7Input num1=8 and num2=23 => Prime numbers are: 11, 13, 17, 19, 23Approach to solve this problemStep 1: Define a function that accepts two numbers, num1 and num2, type is int.Step 2: Iterate between num1 and num2.Step 3: If the number is prime, then print that number, else break.ProgramLive Demopackage main import (    "fmt"    "math" ) func printPrimeNumbers(num1, num2 int){    if num1

Write a Golang program to find the frequency of an element in an array

Kiran P
Updated on 04-Feb-2021 11:13:59

189 Views

ExamplesIn the Input array, arr = [2, 4, 6, 7, 8, 1, 2]Frequency of 2 in given array is 2Frequency of 7 is 1Frequency of 3 is 0.Approach to solve this problemStep 1: Define a function that accepts an array and a numStep 2: Declare a variable count = 0.Step 3: Iterate the given array and increase the count by 1 if num occurs in the array.Step 4: Print count for the given num.ProgramLive Demopackage main import "fmt" func findFrequency(arr []int, num int){    count := 0    for _, item := range arr{       if item == ... Read More

Write a Golang program to convert a decimal number to its binary form

Kiran P
Updated on 04-Feb-2021 11:10:17

2K+ Views

ExamplesInput decimal_num = 13 => Output = 8+4+1 => 1101Input decimal_num = 11 => Output = 8+2+1 => 1011Approach to solve this problemStep 1: Define a function that accepts a decimal number, decimal_num, type is int.Step 2: Define an array to store the remainder while dividing decimal number by 2.Step 3: Start a for loop until the decimal number becomes 0.Step 4: Print the binary array in reverse order.ProgramLive Demopackage main import (    "fmt" ) func decimalToBinary(num int){    var binary []int    for num !=0 {       binary = append(binary, num%2)       ... Read More

Advertisements