Programming Articles

Page 1452 of 2547

Python Program for Extended Euclidean algorithms

Pavitra
Pavitra
Updated on 11-Mar-2026 2K+ Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − Given two numbers we need to calculate gcd of those two numbers and display them.GCD Greatest Common Divisor of two numbers is the largest number that can divide both of them. Here we follow the euclidean approach to compute the gcd i.e. to repeatedly divide the numbers and stop when the remainder becomes zero. Here we extend the algorithm based on previous values obtained in recursion.Now let’s observe the solution in the implementation below −Example# extended Euclidean Algorithm def gcdExtended(a, b, x, y):   ...

Read More

Python Program for Find the closest pair from two sorted arrays

Pavitra
Pavitra
Updated on 11-Mar-2026 297 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given two arrays, we need to find the closest pair from the two sorted arraysNow let’s observe the solution in the implementation below −Example# sys module import sys # pair def print_(ar1, ar2, m, n, x):    # difference    diff=sys.maxsize    # index    l = 0    r = n-1    while(l < m and r >= 0):    # closest pair       if abs(ar1[l] + ar2[r] - x) < diff:          res_l = l ...

Read More

Binary Search in C++ Standard Template Library (STL)

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 363 Views

A binary search known as logarithmic search is a search algorithm that searches for an element in a sorted array. The algorithm recursively divides the array into two halves, if the element is found at the mid position then return otherwise call the divide and check again until the element is found.WorkingThe algorithm works by comparing the middle element of the sorted array with the element that is to be searched.If the search element is equal to the middle element, then return the index of the element.If the search element is greater than the middle element, search in the left ...

Read More

Python Program for Gnome Sort

Pavitra
Pavitra
Updated on 11-Mar-2026 397 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given an array, we need to sort it using gnome sort.Algorithm1. Firstly we traverse the array from left to right. 2. Now, if the current element is larger or equal to the previous element then we traverse one step ahead 3. otherwise, if the current element is smaller than the previous element then swap these two elements and traverse one step back. 4. Repeat steps given above till we reach the end of the arrayNow let’s observe the solution in the ...

Read More

Nested list comprehension in python

Pradeep Elance
Pradeep Elance
Updated on 11-Mar-2026 2K+ Views

A nested list is a list within a list. Python provides features to handle nested list gracefully and apply common functions to manipulate the nested lists. In this article we will see how to use list comprehension to create and use nested lists in python.Creating a MatrixCreating a matrix involves creating series of rows and columns. We can use for loop for creating the matrix rows and columns by putting one python list with for loop inside another python list with for loop.Examplematrix = [[m for m in range(4)] for n in range(3)] print(matrix)Running the above code gives us the ...

Read More

Python Program for Heap Sort

Pavitra
Pavitra
Updated on 11-Mar-2026 2K+ Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given an array, we need to sort it using the concept of heapsort.Here we place the maximum element at the end. This is repeated until the array is sorted.Now let’s observe the solution in the implementation below−Example# heapify def heapify(arr, n, i):    largest = i # largest value    l = 2 * i + 1 # left    r = 2 * i + 2 # right    # if left child exists    if l < n and arr[i] ...

Read More

Java regular expression program to validate an email including blank field valid as well

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 348 Views

Following regular expression matches given e-mail id including the blank input −^([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2, 6})?$Where, ^ matches the starting of the sentence.[a-zA-Z0-9._%+-] matches one character from English alphabet (both cases), digits, "+", "_", ".", "" and, "-" before the @ symbol.+ indicates the repetition of the above mentioned set of characters one or more times.@ matches itself[a-zA-Z0-9.-] matches one character from English alphabet (both cases), digits, "." and "-" after the @ symbol\.[a-zA-Z]{2, 6} two to 6 letter for email domain after "."$ indicates the end of the sentenceExample 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class SampleTest {    public static ...

Read More

Find minimum of each index in list of lists in Python

Pradeep Elance
Pradeep Elance
Updated on 11-Mar-2026 2K+ Views

In some problems we need to identify the minimum of each element in a list. But in solving the matrix operations, we need to find the minimum of each column in the matrix. That needs us to find the minimum value from list of lists. Because each column of a matrix is a list of lists.Using min() and zip()In the below example we use the min() and zip(). Here the zip() function organizes the elements at the same index from multiple lists into a single list. Then we apply the min() function to the result of zip function using a ...

Read More

List.replaceAll(UnaryOperator operator) method in Java

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 2K+ Views

The replaceAll() method of the List interface accept an object of the UnaryOperator representing a particular operation, performs the specified operation on all the elements of the current list and replaces the existing values in the list with their respective results.Exampleimport java.util.ArrayList; import java.util.function.UnaryOperator; class Op implements UnaryOperator {    public String apply(String str) {       return str.toUpperCase();    } } public class Test {    public static void main(String[] args) throws CloneNotSupportedException {       ArrayList list = new ArrayList();       list.add("Java");       list.add("JavaScript");       list.add("CoffeeScript");       list.add("HBase"); ...

Read More

Print all the levels with odd and even number of nodes in it in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 270 Views

In this problem, we are given a tree. And we have to print all the levels with even number of nodes and odd number of nodes in it.Let’s take an example to understand the concept betterOutput −Levels with odd number of nodes: 1, 3, 4 Levels with even number of nodes: 2Explanation − The first level has only 1 element(odd), 2nd level contains two elements(even), 3rd level contains 3 elements(odd) and 4th level contains 1 element(even).Now, to solve this problem. We need to find the count of nodes at each level and print the even-odd levels accordingly.We will follow the ...

Read More
Showing 14511–14520 of 25,466 articles
Advertisements