Get All Combinations of Keypad Values in Mobile Using Backtracking

Nizamuddin Siddiqui
Updated on 27-Aug-2021 13:19:16

331 Views

The problem can be broken down into smaller and simple "subproblems", which can be further divided into yet simpler and smaller subproblems. We take each and every digit one by one and count all ndigits reachable from any digit, use a map to store the mapping of digits reachable from every digit. When the digit becomes n-digit, update the count.Example Live Demousing System; using System.Collections.Generic; namespace ConsoleApplication{    public class BackTracking{       private string GetKeyPadValueBasedOnInput(string digit){          Dictionary keypad = new Dictionary();          keypad.Add("2", "abc");          keypad.Add("3", "def");     ... Read More

Find All Permutations of a String Using Backtracking

Nizamuddin Siddiqui
Updated on 27-Aug-2021 13:15:22

917 Views

Find the character in the first position and swap the rest of the character with the first character. Like in ABC, in the first iteration three strings are formed: ABC, BAC, and CBA by swapping A with A, B and C respectively. Repeat step for the rest of the characters like fixing second character B and so on. Now swap again to go back to the previous position. from ABC, we formed ABC by fixing B again, and we backtrack to the previous position and swap B with C. So, now we got ABC and ACB.Example Live Demousing System; namespace ConsoleApplication{ ... Read More

Find Power of Any Given Number by Backtracking Using C#

Nizamuddin Siddiqui
Updated on 27-Aug-2021 13:13:10

332 Views

Create a function to Find Power which takes the number x and n, where x is the 2 and n is how many times, we have to do the power. If the number is even then we have to do x*x and if the number is odd multiply the result with x*x. Continue the recursive call until the n becomes 0.Suppose if we have a number 2 and 8, then 2*2*2*2*2*2*2*2 =256.Example Live Demousing System; namespace ConsoleApplication{    public class BackTracking{       public int FindPower(int x, int n){          int result;          if ... Read More

Print Number of Islands in a Given Matrix Using Chash

Nizamuddin Siddiqui
Updated on 27-Aug-2021 13:11:51

713 Views

Linear scan the 2d grid map, if a node contains a '1', then it is a root node that triggers a Depth First Search. During DFS, every visited node should be set as '0' to mark as visited node. Count the number of root nodes that trigger DFS, this number would be the number of islands since each DFS starting at some root identifies an island.Example Live Demousing System; namespace ConsoleApplication{    public class Matrix{       public int PrintNumberOfIslands(char[, ] grid){          bool[, ] visited = new bool[grid.GetLength(0), grid.GetLength(1)];          int res = ... Read More

Print Matrix of Size n in Spiral Order Using C#

Nizamuddin Siddiqui
Updated on 27-Aug-2021 13:09:07

996 Views

To rotate a matrix in spiral order, we need to do following until all the inner matrix and the outer matrix are covered −Step1 − Move elements of top rowStep2 − Move elements of last columnStep3 − Move elements of bottom rowStep4 − Move elements of first columnStep5 − Repeat above steps for inner ring while there is an inner matrixExample Live Demousing System; namespace ConsoleApplication{    public class Matrix{       public void PrintMatrixInSpiralOrder(int m, int n, int[, ] a){          int i, k = 0, l = 0;          while (k < ... Read More

Rotate Matrix of Size N x N by 90 Degrees K Times Using C#

Nizamuddin Siddiqui
Updated on 27-Aug-2021 13:06:14

847 Views

The entire matrix needs to be rotated k number of times. In a matrix there is a total of n/2 squares in n*n matrix and we can process each square one at a time using nested loop. In each square, elements are moving in a cycle of 4 elements then we swap the elements involved in an anticlockwise direction for each cycle.Element at position (n-1-j, i) will go to position(i, j)Element at position (i, j) will go to position(j, n-1-i)Element at position (j, n-1-i) will go to position(n-1-i, n-1-j)Element at position (n-1-i, n-1-j) will go to position(n-1-j, i)Example Live Demousing System; ... Read More

Rotate Matrix of Size N x N to 90 Degrees Using C++

Nizamuddin Siddiqui
Updated on 27-Aug-2021 13:01:28

386 Views

In a Matrix, there is a total of n/2 squares in n*n matrix and we can process each square one at a time using nested loop. In each square elements are moving in a cycle of 4 elements. we swap the elements involved in an anticlockwise direction for each cycleElement at position (n-1-j, i) will go to position(i, j)Element at position (i, j) will go to position(j, n-1-i)Element at position (j, n-1-i) will go to position(n-1-i, n-1-j)Element at position (n-1-i, n-1-j) will go to position(n-1-j, i)Example Live Demousing System; using System.Text; namespace ConsoleApplication{    public class Matrix{       public ... Read More

Find Minimum Number of Jumps to Reach End of Array Using Chash

Nizamuddin Siddiqui
Updated on 27-Aug-2021 12:57:50

379 Views

We can simply start from the first element and repeatedly call for all the elements reachable from first element. The minimum number of jumps to reach end from first can be calculated using minimum number of jumps needed to reach end from the elements reachable from first.Array == {1, 3, 6, 3, 2, 3, 6, 8, 9, 5};Number of steps required is 4Example Live Demousing System; namespace ConsoleApplication{    public class Arrays{       public int MinJumps(int[] arr, int l, int h){          if (h == l)             return 0;          if (arr[l] == 0)             return int.MaxValue;          int min = int.MaxValue;          for (int i = l + 1; i

Find Missing and Repeated Number in Sorted Array

Nizamuddin Siddiqui
Updated on 27-Aug-2021 12:55:06

307 Views

To find the missing numberCreate a new array and traverse through the entire array and make the number true in the new array if the number is found Traverse through the entire array and return the first false element as the missing element.To find the repeating elementThe first true element from the new array will be the repeated element.Example Live Demousing System; namespace ConsoleApplication{    public class Arrays{       public void MissingNumberAndRepeatedNumber(int[] arr){          bool[] tempArray = new bool[arr.Length + 1];          int missingelement = -1;          int repeatingelement = ... Read More

Find Missing Numbers in a Sorted Array Without Inbuilt Functions

Nizamuddin Siddiqui
Updated on 27-Aug-2021 12:47:53

2K+ Views

There are three methods as written below −In the first methodUse the formula n(n+1)/2 that counts the number of elements and then need to subtract from the elements in the array.In the second methodCreate a new array and traverse through the entire array and make the number false whichever is found.In the third methodUse Xor operation. which gives the missing number.Example Live Demousing System; namespace ConsoleApplication{    public class Arrays{       public int MissingNumber1(int[] arr){          int totalcount = 0;          for (int i = 0; i < arr.Length; i++){       ... Read More

Advertisements