Found 33676 Articles for Programming

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

516 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

Find the Kth Node from the end in a given singly Linked List using C++

Dev Prakash Sharma
Updated on 05-Feb-2021 12:08:35

476 Views

A linked list is a linear data structure that has multiple nodes that are connected with each other. Each node consists of two fields Data Field and address of the next node. Let us assume we have given a singly linked list the task is to find the kth node from the end in a given singly linked list. For example, Input −1→2→3→4→7→8→9 K= 4Output −Node from the 4th Position is − 4Explanation − Since in the given singly linked list ‘4th’ node from the end is ‘4’, we will return the output as ‘4’.Approach to solve this problemInitially, we ... Read More

Find the index of the first unique character in a given string using C++

Dev Prakash Sharma
Updated on 05-Feb-2021 12:07:52

2K+ Views

Given a string ‘s’, the task is to find the first unique character which is not repeating in the given string of characters and return its index as output. If there are no such characters present in the given string, we will return ‘-1’ as output. For example, Input-1 −s = “tutorialspoint”Output −1Explanation − In the given string “tutorialspoint”, the first unique character which is not repeating is ‘u’ which is having the index ‘1’. Thus we will return ‘1’ as output.Input-2 −s = “aaasttarrs”Output −-1Explanation − In the given string “aaasttarrs’, there are no unique characters. So, we will ... Read More

Detect Capital in a given string using C++

Dev Prakash Sharma
Updated on 05-Feb-2021 12:07:22

578 Views

Let's suppose we have a string ‘str’ which consists of some character in it. The task is to check whether the given string has all its characters capitalized or not and return True or False respectively. For example, Input-1 −str = “INDIA”Output −TrueExplanation − Since all the character of the input string is capital, we will return true in this case.Input-2 −str = “Programmer”Output −FalseExplanation − Since all the characters of the input string are not in the capital except the first letter, we will return false in this case.The approach used to solve this problemIn the given string, we ... Read More

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

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

677 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

417 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

619 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

587 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

3K+ 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

301 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

Advertisements