Print Matrix in Spiral Way

Ankith Reddy
Updated on 17-Jun-2020 10:14:06

1K+ Views

This algorithm is used to print the array elements in a spiral way. At first starting from the first row, print the whole content and then follow the last column to print, then the last row and so on, thus it prints the elements in spiral fashion. The time complexity of this algorithm is O(MN), M is the number of rows and N is the number of columns.Input and OutputInput: The matrix:  1   2   3   4   5   6  7   8   9  10  11  12 13  14  15  16  17  18 Output: Contents of ... Read More

What is Space Complexity

Monica Mona
Updated on 17-Jun-2020 10:08:53

6K+ Views

Space ComplexitySpace complexity is an amount of memory used by the algorithm (including the input values of the algorithm), to execute it completely and produce the result.We know that to execute an algorithm it must be loaded in the main memory. The memory can be used in different forms:Variables (This includes the constant values and temporary values)Program InstructionExecutionAuxiliary SpaceAuxiliary space is extra space or temporary space used by the algorithms during its execution.Memory Usage during program executionInstruction Space is used to save compiled instruction in the memory.Environmental Stack is used to storing the addresses while a module calls another module ... Read More

Polynomial-Time Approximation Scheme

Samual Sam
Updated on 17-Jun-2020 10:07:44

1K+ Views

Polynomial Time Approximation schemeWe can find some polynomial time solution for NP-Complete problems like 0-1 Knapsack problem or Subset sum problem. These problems are very popular in the real world, so there must be some ways to handle these problems.The Polynomial Time Approximation Scheme (PTAS) is a type to approximate algorithms for optimization problems. For the 0-1 Knapsack problem, there is a Pseudo Polynomial Solution, but when the values are large, the solution is not feasible. Then we need a PTAS solution.Some NP-complete problems like Graph Coloring, K-Center problem etc. they have no known polynomial time solution. PTAS used to approximate ... Read More

Amortized Analysis

Ankith Reddy
Updated on 17-Jun-2020 10:07:00

20K+ Views

Amortize AnalysisThis analysis is used when the occasional operation is very slow, but most of the operations which are executing very frequently are faster. Data structures we need amortized analysis for Hash Tables, Disjoint Sets etc.In the Hash-table, the most of the time the searching time complexity is O(1), but sometimes it executes O(n) operations. When we want to search or insert an element in a hash table for most of the cases it is constant time taking the task, but when a collision occurs, it needs O(n) times operations for collision resolution.Aggregate MethodThe aggregate method is used to find ... Read More

Lexicographically Minimum String Rotation

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

705 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

Jarvis March Algorithm

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

4K+ 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

Lock and Key Problem Using HashMap

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

930 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

2K+ 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

Print All Permutations of a Given String

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

2K+ 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

Parity Check of a Number

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

7K+ 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

Advertisements