Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Csharp Articles
Page 8 of 196
How to check whether the given matrix is a Toeplitz matrix using C#?
A Toeplitz matrix is a special matrix where every diagonal running from top-left to bottom-right contains identical elements. In other words, for any element at position matrix[i][j], it should be equal to matrix[i-1][j-1]. This property makes Toeplitz matrices useful in signal processing, time series analysis, and various mathematical applications. Toeplitz Matrix Diagonals 1 2 3 4 5 1 2 3 9 5 1 2 ...
Read MoreHow to search in a row wise increased matrix using C#?
Searching in a row-wise sorted matrix is a common algorithmic problem where each row and column is sorted in ascending order. The naive approach of scanning all elements takes O(M×N) time complexity, but we can optimize this using a smart traversal strategy. The key insight is to start from either the top-right corner or bottom-left corner of the matrix. From the top-right position, if the target is smaller than the current element, move left; if larger, move down. This approach eliminates either a row or column in each step. Algorithm The search algorithm works as follows − ...
Read MoreHow to find the shortest distance to a character in a given string using C#?
Finding the shortest distance to a character in a string requires calculating the minimum distance from each position to the nearest occurrence of the target character. This can be efficiently solved using a two-pass approach that scans the string from left to right and then from right to left. The algorithm maintains two arrays to track distances from both directions, then combines them to find the minimum distance at each position. Syntax Following is the method signature for finding shortest distances − public int[] ShortestDistanceToCharacter(string s, char c) Parameters s − ...
Read MoreHow to generate pascals triangle for the given number using C#?
Pascal's Triangle is a triangular number pattern where each number is the sum of the two numbers directly above it. The triangle starts with 1 at the top, and each row begins and ends with 1. Pascal's Triangle has many applications in mathematics, statistics, and combinatorics, particularly for calculating binomial coefficients and combinations. Each row represents the coefficients of the binomial expansion (a + b)^n, where n is the row number starting from 0. The triangle demonstrates beautiful mathematical properties and patterns that make it useful in various computational problems. Pascal's Triangle (5 rows) ...
Read MoreHow to move all the zeros to the end of the array from the given array of integer numbers using C#?
Moving zeros to the end of an array is a common programming problem that can be solved efficiently using a two-pointer approach. The algorithm maintains the relative order of non-zero elements while shifting all zeros to the end of the array. Algorithm Overview The solution uses a single pass through the array with two pointers − Write Pointer − tracks the position where the next non-zero element should be placed Read Pointer − traverses through all elements in the array Time Complexity − O(N) where N is the array length Space Complexity − O(1) ...
Read MoreHow to find the length of the longest substring from the given string without repeating the characters using C#?
The longest substring without repeating characters problem can be efficiently solved using the sliding window technique with two pointers. This approach maintains a window of unique characters and expands or contracts it based on whether duplicates are found. Algorithm Overview The sliding window technique uses two pointers i (left) and j (right) to maintain a window of unique characters. When a duplicate character is encountered, the left pointer moves forward to exclude the duplicate, while the right pointer continues expanding the window. Sliding Window Technique ...
Read MoreHow to invert a binary search tree using recursion in C#?
To invert a binary search tree, we swap the left and right child nodes for every node in the tree recursively. This operation creates a mirror image of the original tree. The algorithm uses a recursive approach where we process each node by first inverting its left and right subtrees, then swapping their positions. The process involves calling a method InvertBinaryTree that takes a node as a parameter. If the node is null, we return null. Otherwise, we recursively call the method on both left and right children, then swap the left and right child references. ...
Read MoreHow to check whether a binary tree is a valid binary search tree using recursion in C#?
A Binary Search Tree (BST) is a tree structure where each node's left child contains values smaller than the node, and the right child contains values greater than the node. To validate whether a binary tree is a valid BST using recursion, we need to check that each node's value falls within its allowed range based on its position in the tree. The recursive approach uses upper and lower bounds that get updated as we traverse the tree. For each node, we verify that its value lies within the current bounds, then recursively check the left subtree with updated ...
Read MoreHow to check whether a binary tree has the given path sum in C#?
A binary tree has a path sum when there exists a root-to-leaf path where the sum of all node values equals a given target sum. The HasPathSum method recursively traverses the tree, subtracting each node's value from the target sum until reaching a leaf node. Syntax Following is the syntax for checking path sum in a binary tree − public bool HasPathSum(Node node, int targetSum) { if (node == null) return false; if (node.LeftChild == null && node.RightChild == null) { ...
Read MoreHow to sort 0,1,2 in an Array (Dutch National Flag) without extra space using C#?
The Dutch National Flag problem is a classic algorithm challenge that sorts an array containing only 0s, 1s, and 2s in a single pass without using extra space. This problem was originally described by Dutch computer scientist Edsger Dijkstra and is also known as the Three-Way Partitioning algorithm. The algorithm uses three pointers to partition the array into three regions: all 0s on the left, all 1s in the middle, and all 2s on the right. Algorithm Overview The algorithm maintains three pointers − low − Points to the boundary of the 0s region mid ...
Read More