Find Closest Pair from Two Sorted Arrays in Python

Pavitra
Updated on 20-Dec-2019 06:10:25

255 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 Live Demo# 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 = ... Read More

Python Program for Extended Euclidean Algorithms

Pavitra
Updated on 20-Dec-2019 06:07:43

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 Live Demo# extended Euclidean Algorithm def gcdExtended(a, b, x, y): ... Read More

Python Program for Egg Dropping Puzzle

Pavitra
Updated on 20-Dec-2019 06:03:11

328 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − Suppose that we want to know which stories in a 40-story building are safe to drop the eggs from, and which of those will cause the eggs to get damaged on landing with the help of eggs. We need to display a minimum number of trails to check the stories.Now let’s observe the solution in the implementation below −Example Live Demo# dynamic programming INT_MAX = 32767 # to get minimum trials def eggDrop(n, k):    # intialization    eggFloor = [[0 for x in range(k ... Read More

Python Program for Cycle Sort

Pavitra
Updated on 20-Dec-2019 05:56:13

287 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 cycle sort.It is an in-place algorithm and swapping takes place by the formation of cycles.Now let’s observe the solution in the implementation below −Example Live Demodef cycleSort(array):    writes = 0    # cycles to be rotated    for cycleStart in range(0, len(array) - 1):       item = array[cycleStart]       #position to place the item       pos = cycleStart       for i in ... Read More

Python Program for Counting Sort

Pavitra
Updated on 20-Dec-2019 05:45:41

230 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 the array using the concept of counting sort.Counting sort is a technique in which we work on keys between a specific range. It involves counting the number of objects which have distinct key & values. Finally, we do arithmetic calculations to obtain the position of each object and display the output.Now let’s observe the solution in the implementation below −Example Live Demodef countSort(arr):    # The output character array that will have sorted arr    output ... Read More

Python Program for Array Rotation

Pavitra
Updated on 20-Dec-2019 05:23:17

201 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − Given a text and a pattern, we need to print all occurrences of pattern and its permutations (or anagrams) in text.Now let’s observe the solution in the implementation below −Example Live Demo# maximum value MAX = 300 # compare def compare(arr1, arr2):    for i in range(MAX):       if arr1[i] != arr2[i]:          return False    return True # search def search(pat, txt):    M = len(pat)    N = len(txt)    # countP pattern account    # countTW ... Read More

Pass Lambda Expression as Method Parameter in Java

raja
Updated on 19-Dec-2019 13:09:20

7K+ Views

A lambda expression is an anonymous or unnamed method in Java. It doesn't execute on its own and used to implement methods that are declared in a functional interface. If we want to pass a lambda expression as a method parameter in java, the type of method parameter that receives must be of functional interface type.Exampleinterface Algebra { int operate(int a, int b); } enum Operation { ADD, SUB, MUL, DIV } public class LambdaMethodArgTest { public static void main(String[] args) { print((a, b) -> ... Read More

Find the Second Last Node of a Linked List in Single Traversal in C++

Arnab Chakraborty
Updated on 19-Dec-2019 13:02:06

683 Views

Now we will see how to get the second last element in the linked list. Suppose there are few elements like [10, 52, 41, 32, 69, 58, 41], second last element is 58.To solve this problem, we will use two pointers one will point to current node, and another will point to previous node of the current position, then we will move until the next of current is null, then simply return the previous node.Example Live Demo#include using namespace std; class Node {    public:       int data;       Node *next; }; void prepend(Node** start, int new_data) ... Read More

Find the Second Largest Element in a Linked List in C++

Arnab Chakraborty
Updated on 19-Dec-2019 13:00:00

491 Views

Here we will see the second largest element in the linked list. Suppose there are n different nodes with numerical values. So if the list is like [12, 35, 1, 10, 34, 1], then second largest element will be 34.This process is similar to the finding of second largest element in an array, we will traverse through the list and find second largest element by comparing.Example#include using namespace std; class Node {    public:       int data;       Node *next; }; void prepend(Node** start, int new_data) {    Node* new_node = new Node;    new_node->data = ... Read More

Find the Next Identical Calendar Year in C++

Arnab Chakraborty
Updated on 19-Dec-2019 12:54:41

252 Views

Suppose we have an year Y. Find next identical calendar year to Y. So the calendar of 2017 is identical with 2023.A year X is identical to given previous year Y if it matches these two conditions.x starts with the same day as year, If y is leap year, then x also, if y is normal year, then x also normal year.The idea is to check all years one by one from next year. We will keep track of number of days moved ahead. If there are total 7 moved days, then current year begins with same day. We also ... Read More

Advertisements