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
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
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
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
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
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
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
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
ExamplesInput arr = [1, 3, 4, 5, 6] => 1+3+4+5+6 = 19Input arr = [5, 7, 8, 9, 1, 0, 6] => 5+7+8+9+1+0+6 = 36Approach to solve this problemStep 1: Define a function that accepts an array.Step 2: Declare a variable, res = 0.Step 3: Iterate the array and add elements to res.Step 4: Return res.ProgramLive Demopackage main import "fmt" func findArraySum(arr []int) int{ res := 0 for i:=0; i
ExamplesInput binary_num = 1010111 => decimal number = 64+0+16+4+2+1 = 87Input binary_num = 10101 => decimal number = 21Approach to solve this problemStep 1: Define a function that accepts a binary number, binary_num, declare decimalNum = 0, index = 0Step 2: Start for loop until binary_num becomes 0.Step 3: Find the remainder of binary_num and divide by 10.Step 4: Calculate the decimal number using decimalNum and remainder*pow(2, index).Step 5: Return decimalNum.ProgramLive Demopackage main import ( "fmt" "math" ) func binaryToDecimal(num int) int { var remainder int index := 0 decimalNum := 0 for num ... Read More