Found 33676 Articles for Programming

Bubble Sort in Go Lang

Dev Prakash Sharma
Updated on 05-Feb-2021 11:53:00

9K+ Views

Bubble Sort is a sorting algorithm that works by swapping the elements that are in the wrong order. In multiple passes, it checks if the adjacent elements are in the right order (increasing) or not.The Time Complexity of the Bubble Sort is O(n^2) since it takes two nested loops to check the adjacent element.For example, let’s take the following unsorted array −22 15 11 45 13Bubble Sort Algorithm first traverses the whole array and then in another loop checks if the adjacent elements are in order or not.Thus, after sorting the elements will be, 11 13 15 22 45AlgorithmIn two ... Read More

Birthday Paradox in Python

Dev Prakash Sharma
Updated on 05-Feb-2021 11:50:17

1K+ Views

The birthday paradox is a very famous problem in the section of probability.Problem Statement − There are several people at a birthday party, some are having the same birthday collision. We need to find the approximate number of people at a birthday party on the basis of having the same birthday.In the probability, we know that the chance of getting ahead is 1/2, same as if we have some coins, the chance of getting 10 heads is 1/100 or 0.001.Let us understand the concept.The chance of two people having the different birthday is $$\frac{364}{365}$$ which is $$\lgroup1-\frac{1}{365}\rgroup$$ in a Non-leap ... Read More

Birthday Paradox in C++

Dev Prakash Sharma
Updated on 05-Feb-2021 11:47:09

754 Views

The birthday paradox is a very famous problem in the section of probability. The problem statement of this problem is stated as, There are several people at a birthday party, some are having the same birthday collision. We need to find the approximate no. of people at a birthday party on the basis of having the same birthday.In the probability we know that the chance of getting ahead is 1/2 same as if we have some coins, the chance of getting 10 heads is 1/100 or 0.001.Let us understand the concept, The chance of two people having the different birthday ... Read More

Write a Golang program to check whether a given number is prime number or not

Kiran P
Updated on 04-Feb-2021 10:52:02

5K+ Views

Definition: A number is that is greater than 2 and divisible only by itself and 1.Examples: Prime numbers are 2, 3, 5, 7, 11, 13, 113, 119, ..., etc.Approach to solve this problemStep 1: Find square root of the given number, sq_root = √numStep 2: If the given number is divisible by a number that belongs to [2, sq_root], then print “Non Prime Number”Step 3: If not divisible by any number, then print “Prime Number”ProgramLive Demopackage main import (    "fmt"    "math" ) func checkPrimeNumber(num int) {    if num < 2 {       fmt.Println("Number must be greater than 2.")       return    }    sq_root := int(math.Sqrt(float64(num)))    for i:=2; i

Write a Golang program to find the element with the minimum value in an array

Kiran P
Updated on 04-Feb-2021 10:52:24

880 Views

ExamplesA1 = [2, 4, 6, 7, 8, 10, 3, 6, 0, 1]; Minimum number is 0;A2 = [12, 14, 16, 17, 18, 110, 13, 16, 10, 11]; Minimum number is 10;Approach to solve this problemStep 1: Consider the number at the 0th index as the minimum number, min_num = A[0].Step 2: Compare min_num with every number in the given array, while iterating.Step 3: If a number is smaller than min_num, then assign that number to min_num.Step 4: At the end of iteration, return min_num;ProgramLive Demopackage main import "fmt" func findMinElement(arr []int) int {    min_num := arr[0]    for i:=0; i

Write a program in Go language to find the element with the maximum value in an array

Kiran P
Updated on 04-Feb-2021 10:52:45

642 Views

ExamplesA1 = [2, 4, 6, 7, 8, 10, 3, 6, 0, 1]; Maximum number is 10A2 = [12, 14, 16, 17, 18, 110, 13, 16, 10, 11]; Maximum number is 110Approach to solve this problemStep 1: Consider the number at the 0th index as the maximum number, max_num = A[0]Step 2: Compare max_num with every number in the given array, while iterating.Step 3: If a number is greater than max_num, then assign that number to max_num;Step 4: At the end of iteration, return max_num;ProgramLive Demopackage main import "fmt" func findMaxElement(arr []int) int {    max_num := arr[0]    for i:=0; i ... Read More

Find any one of the multiple repeating elements in read only array in C++

Hafeezul Kareem
Updated on 01-Feb-2021 12:22:06

95 Views

In this tutorial, we are going to write a program that finds the repeating element in the given array.Let's see the steps to solve the problem.Initialize the array.Initialize a counter map to store the frequency of each element in the array.Iterate over the array.Count each element.Print the element whose frequency is greater than 1.ExampleLet's see the code. Live Demo#include using namespace std; int findRepeatingElement(int arr[], int n) {    map frequencies;    for (int i = 0; i < n; i++) {       map::iterator itr = frequencies.find(arr[i]);       if (itr != frequencies.end()) {       ... Read More

Find amount of water wasted after filling the tank in C++

Hafeezul Kareem
Updated on 01-Feb-2021 12:18:23

308 Views

In this tutorial, we are going to solve the following problem.Given a tank with a capacity of N liters and a pump that fill the tank with S speed per minute. Unfortunately, there is a hole in the tank. And water is wasting at a speed of WS per minute while filling it.We need to calculate the amount of water wasted for a full tank.The amount of water filled per minute is equal to the difference between the water filling water and wasting water speed.Hence we can get the total time to fill the water tank by dividing the capacity ... Read More

Find all the pairs with given sum in a BST in C++

Hafeezul Kareem
Updated on 01-Feb-2021 12:18:07

388 Views

In this tutorial, we are going to write a program that finds all the pairs whose sum is equal to the given number in the binary search tree.We are going to store and values of trees in two different lists to find the pairs. Let's see the steps to solve the problem.Create a struct node for a binary tree.Write a function to insert a new node into the binary search tree.Remember, in the binary search tree all the elements that are less than root are smaller, and right are larger.Initialize two empty lists to store the left and right nodes ... Read More

Find all divisors of a natural number - Set 2 in C++

Hafeezul Kareem
Updated on 01-Feb-2021 12:13:53

2K+ Views

In this tutorial, we are going to write a program that finds all the divisors of a natural number. It's a straightforward problem. Let's see the steps to solve it.Initialize the number.Write a loop that iterates from 1 to the square root of the given number.Check whether the given number is divisible by the current number or not.If the above condition satisfies, then print the current number and given_number/current_number.ExampleLet's see the code. Live Demo#include using namespace std; void findDivisors(int n) {    for (int i = 1; i

Advertisements