Found 26504 Articles for Server Side Programming

How to print a matrix of size n*n in spiral order using C#?

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

989 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

How to rotate a matrix of size n*n to 90-degree k times using C#?

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

830 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

How to rotate a matrix of size n*n to 90 degree using C#?

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

378 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

How to find the minimum number of jumps required to reach the end of the array using C#?

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

365 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

How to find the missing number and the repeated number in a sorted array without using any inbuilt functions using C#?

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

297 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

What are the different ways to find missing numbers in a sorted array without any inbuilt functions using C#?

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

How to return the first unique character without using inbuilt functions using C#?

Nizamuddin Siddiqui
Updated on 27-Aug-2021 12:40:21

230 Views

Create an empty new array of length 256, traverse through the entire string character by character and increment the value in the new array. At the end traverse the entire array and return the first character that has value 1.Example 1aabccd -→2 1 2 1 → Return the first character which is having count 1. That is b by subtracting with the ascii values.Example 2 Live Demousing System; namespace ConsoleApplication{    public class Arrays{       public char ReturnCharacterOfFirstUniqueCharachter(string s){          int index = -1;          int[] arrayValues = new int[256];       ... Read More

How to return the index of first unique character without inbuilt functions using C#?

Nizamuddin Siddiqui
Updated on 27-Aug-2021 12:38:10

520 Views

Create an empty new array of length 256, traverse through the entire string character by character and increment the value in the new array. At the end traverse the entire array and return the first character that has value 1.Example 1aabccd -→2 1 2 1 → Return the first character which is having count 1. That is b.Example 2 Live Demousing System; namespace ConsoleApplication{    public class Arrays{       public int ReturnIndexOfFirstUniqueCharachter(string s){          int index = -1;          int[] arrayValues = new int[256];          for (int i = 0; ... Read More

How to reverse a given string word by word instead of letters using C#?

Nizamuddin Siddiqui
Updated on 27-Aug-2021 12:36:03

259 Views

Create a method reverse Words that takes char array as an input and for each and every character until the empty space is not reached reverse the word. At the last step reverse the entire string from length 0 to n-1 length. In the first step the string “This is my book” will be turned into “koob ym si siht”. At the end of the second step the string words will be reversed to “book my is This”Time complexity − O(N)Example Live Demousing System; namespace ConsoleApplication{    public class Arrays{       static void reverse(char[] str, int start, int end){ ... Read More

How to remove duplicates from the sorted array and return the non-duplicated array using C#?

Nizamuddin Siddiqui
Updated on 27-Aug-2021 12:31:54

1K+ Views

The array is already sorted, we can keep two pointers ii and jj, where ii is the slow-runner while jj is the fast-runner. As long as nums[i] = nums[j]nums[i]=nums[j], we increment jj to skip the duplicate.When we encounter nums[j] != nums[i] the duplicate run has ended so we must copy its value to nums[i + 1]nums[i+1]. ii is then incremented and we repeat the same process again until jj reaches the end of array.Create an new array copy all the elements from the filtered array till the index and return the new array.Time complexity − O(N)Example Live Demousing System; namespace ConsoleApplication{ ... Read More

Advertisements