Found 2745 Articles for Csharp

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

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

174 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

How to remove duplicates from the sorted array and return the length using C#?

Nizamuddin Siddiqui
Updated on 27-Aug-2021 12:27:29

150 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.Time complexity − O(N)Example Live Demousing System; namespace ConsoleApplication{    public class Arrays{       public int RemoveDuplicatesFromSortedArrayAndReturnLength(int[] arr){          int index = 1; ... Read More

How to rotate an array k time using C#?

Nizamuddin Siddiqui
Updated on 27-Aug-2021 12:22:28

778 Views

Given an array and number k, the problem states that we have to rotate the array k times.If the given number is 3 then the array must be rotated 3 times.Create a function reverse which takes the array, start and end as a parameter.In the 1st step call reverse method from 0 to array length.In the 2nd step call the reverse method from 0 to k-1.In the 3rd step call the reverse method from k+1 to array length.Example Live Demousing System; namespace ConsoleApplication{    public class Arrays{       public void ReverseArrayKTimes(int[] arr, int k){          Reverse(arr, ... Read More

How to sort 0,1 in an Array without using any extra space using C#?

Nizamuddin Siddiqui
Updated on 27-Aug-2021 12:24:44

322 Views

Take two-pointers, low, high. We will use low pointers at the start, and the high pointer will point at the end of the given array.If array [low] =0, then no swapping requiredIf array [low] = 1, then swapping is required. Decrement high pointer once.Time complexity − O(N)Example Live Demousing System; namespace ConsoleApplication{    public class Arrays{       public void SwapZerosOnes(int[] arr){          int low = 0;          int high = arr.Length - 1;          while (low < high){             if (arr[low] == 1){     ... Read More

How to sort 0,1,2 in an Array (Dutch National Flag) without extra space using C#?

Nizamuddin Siddiqui
Updated on 27-Aug-2021 12:25:16

168 Views

We need to take three-pointers, low, mid, high. We will use low and mid pointers at the start, and the high pointer will point at the end of the given array.If array [mid] =0, then swap array [mid] with array [low] and increment both pointers once.If array [mid] = 1, then no swapping is required. Increment mid pointer once.If array [mid] = 2, then we swap array [mid] with array [high] and decrement the high pointer once.Time complexity − O(N)Example Live Demousing System; namespace ConsoleApplication{    public class Arrays{       private void Swap(int[] arr, int pos1, int pos2){   ... Read More

How to check whether a binary tree has the given path sum in C#?

Nizamuddin Siddiqui
Updated on 17-Aug-2021 08:46:21

245 Views

HasPathsum takes 2 parameters one is the tree node and other is the sum value, initially we check whether the node is null or not, if the node is null then we return false. If the node is not null then we call HasPathSum recursive method, in each and every recursive step we keep on subtracting the sum value from the node value. If the value of the sum reaches 0 then we come to conclusion that the given tree has the path that is equal to sum and return true.Examplepublic class TreesPgm{    public class Node{       ... Read More

How to check whether a binary tree is a valid binary search tree using recursion in C#?

Nizamuddin Siddiqui
Updated on 17-Aug-2021 08:42:20

306 Views

A tree is a binary search tree if it has all the left child lesser than the node elements and all the right child greater than the node elements. Initially we check whether the node has any value, if the node is null then we consider as valid binary search tree and return true. After checking the node null result, we call the recursive method isValidBST by passing the node, min value and max value. If the root value is lesser than min and the root value is greater than max we consider as not a binary search tree and ... Read More

How to invert a binary search tree using recursion in C#?

Nizamuddin Siddiqui
Updated on 17-Aug-2021 08:39:48

1K+ Views

To invert a binary search tree, we call a method InvertABinarySearchTree which takes node as a parameter. If the node is null then return null, if the node is not null, we call the InvertABinarySearchTree recursively by passing the left and right child values. and assign the right child value to the left child and left child value to the right child. The final output will consist of the tree which will be its own mirror image.Examplepublic class TreesPgm{    public class Node{       public int Value;       public Node LeftChild;       public Node ... Read More

How to check whether the tree is symmetric or not using recursion in C#?

Nizamuddin Siddiqui
Updated on 17-Aug-2021 08:37:33

139 Views

In the recursive approach we to find a tree is symmetric or not we initially check whether the tree is null or not, if the tree is null then its symmetric, if the tree is not not null we call amethod issymmetricmirror .In isSymmetricMirror we get the value of the left child and right child, if both left and right child are null we consider as symmetric, if either of the value is null then we consider and not symmetric and at last we call the issymmetric method recursively by passing the left and right child values.Examplepublic class TreesPgm{   ... Read More

Advertisements