Found 38 Articles for Misc Algorithms

Tower Of Hanoi Problem

Chandu yadav
Updated on 17-Jun-2020 10:19:12

2K+ Views

Tower of Hanoi is a puzzle problem. Where we have three stands and n discs. Initially, Discs are placed in the first stand. We have to place discs into the third or destination stand, the second or auxiliary stand can be used as a helping stand.But there are some rules to follow. We can transfer only one disc for each movement.Only the topmost disc can be picked up from a stand.No bigger disc will be placed at the top of the smaller disc.This problem can be solved easily by recursion. At first, using recursion the top (n-1) discs are placed from ... Read More

Sort strings in Alphanumeric sequence

George John
Updated on 17-Jun-2020 09:48:30

4K+ Views

A list of given strings is sorted in alphanumeric order or Dictionary Order. Like for these words: Apple, Book, Aim, they will be sorted as Aim, Apple, Book.If there are some numbers, they can be placed before the alphabetic strings.Input and OutputInput: A list of strings: Ball Apple Data Area 517 April Man 506 Output: Strings after sort: 506 517 Apple April Area Ball Data ManAlgorithmsortStr(strArr, n)Input: The list of all strings, number of elements.Output − Strings in alphanumeric sorted order.Begin    for round := 1 to n-1, do       for i := 0 to n-round, do     ... Read More

Find Weekday using Zeller's Algorithm

Monica Mona
Updated on 17-Jun-2020 09:47:32

1K+ Views

Zeller’s Algorithm is used to find the weekday from a given date. The formula to find weekday using Zeller’s Algorithm is here:The formula is containing some variables; They are −d − The day of the date.m: It is the month code. From March to December it is 3 to 12, for January it is 13, and for February it is 14. When we consider January or February, then given year will be decreased by 1.y − Last two digits of the yearc − first two digits of the yearw − The weekday. When it is 0, it is Saturday, when ... Read More

Travelling Salesman Problem

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

1K+ Views

One sales-person is in a city, he has to visit all other cities those are listed, the cost of traveling from one city to another city is also provided. Find the route where the cost is minimum to visit all of the cities once and return back to his starting city.The graph must be complete for this case, so the sales-person can go from any city to any city directly.Here we have to find minimum weighted Hamiltonian Cycle.Input and OutputInput: Cost matrix of the matrix. 0  20 42 25 30 20  0 30 34 15 42 30  0 10 10 ... Read More

Reservoir Sampling

Chandu yadav
Updated on 17-Jun-2020 09:51:21

812 Views

The Reservoir sampling is a randomized algorithm. In this algorithm, k items are chosen from a list with n different items.We can solve it by creating an array as a reservoir of size k. Then randomly pick one element from the main list and placed that item in the reservoir list. When one item is selected once, it will not be selected for next time. But his approach is not effective, we can increase the complexity by this method.In the reservoir list, copy first k items from the list, now one by one from the (k+1)th number in the list, ... Read More

Parity Check of a Number

Monica Mona
Updated on 17-Jun-2020 09:54:17

5K+ Views

Parity of a number is based on the number of 1’s present in the binary equivalent of that number. When the count of present 1s is odd, it returns odd parity, for an even number of 1s it returns even parity.As we know that the numbers in computer memory are stored in binary numbers, so we can shift numbers easily. In this case, by shifting the bits, we will count a number of 1’s present in the binary equivalent of the given number.Input and OutputInput: A number: 5 Binary equivalent is (101) Output: Parity of 5 is Odd.AlgorithmfinParity(n)Input: The number ... Read More

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

609 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

Advertisements