Programming Articles - Page 2418 of 3366

Python Program for Binary Search

Farhan Muhamed
Updated on 22-Aug-2025 15:53:46

6K+ Views

The binary search algorithm is the fastest algorithm to search for an element in a sorted array. It works by dividing the array into two halves and checking if the target element is in the left or right half, thus reducing the search space by half with each iteration. Implementing Binary Search Algorithm Given a sorted array of integers and a target element, and our task is to implement a python program to search for the target element in the array using the binary search algorithm. Here are some example scenarios: Scenario 1 Input: arr[] = {1, ... Read More

Maximum Length Chain of Pairs in C++

Arnab Chakraborty
Updated on 25-Sep-2019 12:32:39

219 Views

There is a chain of pairs is given. In each pair, there are two integers and the first integer is always smaller, and second one is greater, the same rule can also be applied for the chain construction. A pair (x, y) can be added after a pair (p, q), only if q < x.To solve this problem, at first we have to sort given pairs in increasing order of first element. After that we will compare the second element of a pair, with the first element of next pair.Input − A chain of number pairs. {(5, 24), (15, 25), ... Read More

Python Program to Print Matrix in Z form

Pavitra
Updated on 25-Sep-2019 12:26:13

390 Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statement −Given a square matrix of order n*n, we need to display elements of the matrix in Z form.Z form is traversing the matrix in the following steps −Traverse the first rowNow, traverse the second principal diagonalFinally, traverse the last row.We will take an input matrix here implicitly taken to demonstrate the flow of code.demostrateExample Live Demoarr = [[1, 2, 6, 9],    [1, 2, 3, 1],    [7, 1, 3, 5],    [1, 8, 7, 5]] n = len(arr[0]) i = 0 ... Read More

Python Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +…….+ n/n!

Pavitra
Updated on 25-Sep-2019 12:21:44

3K+ Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statement −Given an integer input n, we need to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +…….+ n/n!Here we are implementing for loop, therefore, we get O(n) as the time complexity.Here to reach the efficiency we calculate factorial within the same loop.Here we frame a sumofseries function as described below −Example Live Demodef sumOfSeries(num):    res = 0    fact = 1    for i in range(1, num+1):       fact *= i       res ... Read More

Matrix Chain Multiplication (A O(N^3) Solution) in C++

Arnab Chakraborty
Updated on 02-Dec-2024 11:54:42

3K+ Views

If a chain of matrices is given, we have to find a minimum number of correct sequences of matrices to multiply. We know that the matrix multiplication is associative, so for four matrices ABCD, we can multiply A(BCD), (AB)(CD), (ABC)D, and A(BC)D, in these sequences. Like these sequences, our task is to find which ordering is efficient to multiply.ExampleIn the given input there is an array say arr, which contains arr[] = {1, 2, 3, 4}. It means the matrices are of the order (1 x 2), (2 x 3), (3 x 4). Input − The ... Read More

Python Program to find the area of a circle

Pavitra
Updated on 25-Sep-2019 12:18:21

768 Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statement −Given the radius of a circle, we need to find a circle.The area of a circle can simply be evaluated using the following formula.Area = Pi*r*rLet’s see the implementation below −Example Live Demodef findArea(r):    PI = 3.142    return PI * (r*r); # Driver method print("Area is %.6f" % findArea(5));OutputArea is 78.550000All variables and functions are declared in the global scope as shown in the figure below.ConclusionIn this article, we learned about the approach to find whether it is possible to make ... Read More

Tribonacci Word in C++

Arnab Chakraborty
Updated on 25-Sep-2019 12:17:14

229 Views

The Tribonacci Word is a sequence of digits. This is similar to the Fibonacci Words. Tribonacci Word is constructed by repeated concatenation of three previous stringsT(n) = T(n - 1) + T(n - 2) + T(n - 3)The first few strings to start, are {1, 12, 1213} So the next one will be 1213 + 12 + 1 = 1213121Algorithmtribonacci_word(n): Begin    first := 1, second := 12, third := 1213    print first, second, third    for i in range 3 to n, do       temp := third       third := third + second + ... Read More

Python Program to calculate the area of a Tetrahedron

Pavitra
Updated on 25-Sep-2019 12:37:59

410 Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statement −Given the side of a tetrahedron, we need to find a tetrahedron.A Tetrahedron is a geometric figure which looks like a pyramid with a triangular base. It is a solid object with four triangular faces, three on the sides, one on the bottom of the base and four vertices or corners.Here we frame an area function as shown below −Exampleimport math def areatetrahedron(side):    return (math.sqrt(3) * (side * side)) # Driver Code side = 20 print("Area of Tetrahedron = ", area_of_tetrahedron(side))OutputArea ... Read More

Tribonacci Numbers in C++

Arnab Chakraborty
Updated on 25-Sep-2019 12:13:29

2K+ Views

Here we will see how to generate the Tribonacci numbers using C++. The Tribonacci numbers are similar to the Fibonacci numbers, but here we are generating a term by adding three previous terms. Suppose we want to generate T(n), then the formula will be like below −T(n) = T(n - 1) + T(n - 2) + T(n - 3)The first few numbers to start, are {0, 1, 1}Algorithmtribonacci(n): Begin    first := 0, second := 1, third := 1    print first, second, third    for i in range n – 3, do       next := first + ... Read More

Tetranacci Numbers in C++

Arnab Chakraborty
Updated on 25-Sep-2019 12:10:21

217 Views

Here we will see how to generate the Tetranacci numbers using C++. The Tetranacci numbers are similar to the Fibonacci numbers, but here we are generating a term by adding four previous terms. Suppose we want to generate T(n), then the formula will be like below −T(n) = T(n - 1) + T(n - 2) + T(n - 3) + T(n - 4)The first few numbers to start, are {0, 1, 1, 2}Algorithmtetranacci(n): Begin    first := 0, second := 1, third := 1, fourth := 2    print first, second, third, fourth    for i in range n – ... Read More

Advertisements