Found 546 Articles for Algorithms

Print all permutations of a given string

Arjun Thakur
Updated on 17-Jun-2020 09:55:28

1K+ Views

Printing all permutations of a given string is an example of backtracking problem. We will reduce the size of the substring to solve the sub-problems, then again backtrack to get another permutation from that section.For an example, if the string is ABC, the all permutations will be ABC, ACB, BAC, BCA, CAB, CBA.The complexity of this algorithm is O(n!). It is a huge complexity. When the string size increases, it takes a longer time to finish the task.Input and OutputInput: A string “ABC” Output: All permutations of ABC is: ABC ACB BAC BCA CBA CABAlgorithmstringPermutation(str, left, right)Input: The string and left ... Read More

Lock & Key problem using Hash-map

Ankith Reddy
Updated on 17-Jun-2020 09:57:57

610 Views

A list of different locks and another list of keys are given. Our task is to find the correct match of lock and key from the given list, and assign that key with the lock when it is correct.In this approach we will traverse all of the locks and create a hash-map, after that, each key is searched in the hash-map. When the key is matches, then that is marked as a valid key and assigned with a lock.Input and OutputInput: The lists of locks and keys. lock = { ), @, *, ^, (, %, !, $, &, #} ... Read More

Nuts and Bolt Problem

Samual Sam
Updated on 17-Jun-2020 09:56:52

1K+ Views

A list of different nuts and another list of bolts are given. Our task is to find the correct match of nuts and bolts from the given list, and assign that nut with the Bolt, when it is matched.This problem is solved by the quick-sort technique. By taking the last element of the bolt as a pivot, rearrange the nuts list and get the final position of the nut whose bolt is the pivot element. After partitioning the nuts list, we can partition the bolts list using the selected nut. The same tasks are performed for left and right sub-lists ... Read More

Lexicographically minimum string rotation

Samual Sam
Updated on 17-Jun-2020 10:03:27

433 Views

Let us consider a string is given, we know that the string is a sequence of characters. The Lexicographical rotation is the rotation of string, to convert characters in lexicographical order.The solution is simple, we simply concatenate the given string with itself, then in another array, all rotation of strings are stored. After that sort the array in ascending order, the lowest value is the final result.Input and OutputInput: The String “BCAAFAABCD” Output: Rotated String: “AABCDBCAAF”AlgorithmminStrRotation(str)Input − The given string.Output − Minimum string rotation required.Begin    n := length of str    define strArr to store all rotations    tempStr := ... Read More

Kth Largest Element in an Array

Monica Mona
Updated on 04-Jan-2023 18:04:51

793 Views

From a set of data, this algorithm will find the largest element to kth largest element of the array.This problem can be solved easily by sorting the array. We can sort them either in ascending order or in descending order. Solving it in descending order, we can get first k elements to find our result.Input and OutputInput: The elements of an array: {1, 23, 12, 9, 30, 2, 50, 63, 87, 12, 45, 21}, K = 4 Output: 4 largest elements are 87 63 50 45AlgorithmkthLargestElement(array, n, k)Input: The array, number of elements in the array, place k.Output: Display largest ... Read More

Jarvis March Algorithm

Arjun Thakur
Updated on 17-Jun-2020 09:59:36

2K+ Views

Jarvis March algorithm is used to detect the corner points of a convex hull from a given set of data points.Starting from a leftmost point of the data set, we keep the points in the convex hull by anti-clockwise rotation. From a current point, we can choose the next point by checking the orientations of those points from the current point. When the angle is largest, the point is chosen. After completing all points, when the next point is the start point, stop the algorithm.Input and OutputInput: Set of points: {(-7, 8), (-4, 6), (2, 6), (6, 4), (8, 6), ... Read More

Graham Scan Algorithm

Samual Sam
Updated on 17-Jun-2020 09:26:16

5K+ Views

The convex hull is the minimum closed area which can cover all given data points.Graham’s Scan algorithm will find the corner points of the convex hull. In this algorithm, at first, the lowest point is chosen. That point is the starting point of the convex hull. Remaining n-1 vertices are sorted based on the anti-clockwise direction from the start point. If two or more points are forming the same angle, then remove all points of the same angle except the farthest point from start.From the remaining points, push them into the stack. And remove items from stack one by one, ... Read More

Even Number With Prime Sum

Ankith Reddy
Updated on 17-Jun-2020 09:27:18

947 Views

All even numbers from 4, can be expressed as a sum of two prime numbers. Sometimes a number can have more than one sum of the prime number combination.For an example the number 10 = (5 + 5) and (7 + 3)This algorithm will find all of the combinations of prime sums for a given number.  When one number x is prime, then only we will check whether (number - x) is prime or not, if yes, the sum of x and (number – x) represents the even number.Input and OutputInput: Even number: 70 Output: Prime sums 70 = 3 ... Read More

Flood fill Algorithm

Monica Mona
Updated on 17-Jun-2020 09:30:02

1K+ Views

One matrix is given; the matrix is representing the one screen. Each element (i, j) of the screen is denoted as a pixel, the color of that pixel is marked with different numbers. In this algorithm, the pixels will be filled with new color when it is already in selected previous color. If the previous color is not the previous color, that pixel will not be filled. After filling a pixel, it will check for its up, down, left and right pixels to do the same.The idea is really simple, first, we check whether the selected position is colored with ... Read More

Number to word conversion

George John
Updated on 17-Jun-2020 09:28:45

2K+ Views

This algorithm will convert a given number into English words. Like 564 will be Five Hundred and Sixty-Four. For this algorithm, some predefined strings are given, from that list, it will get the proper words to make into words.The lists are like Units: it will hold all words for (0 to 9) like Zero, One…Nine twoDigits: it will hold all numbers from (10 - 19), like Ten, eleven…NineteentenMul: For ten multiples, (20-90), like Twenty, Thirty, … Ninety.tenPower: It is for Hundred and Thousands as power 2 and 3 of 10Input and OutputInput: The number: 568 Output: Five Hundred And Sixty EightAlgorithmnumToWord(num)there are some ... Read More

Advertisements