Server Side Programming Articles

Page 685 of 2109

How to find the shortest distance to a character in a given string using C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 446 Views

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 More

How to generate pascals triangle for the given number using C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 199 Views

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 More

How to move all the zeros to the end of the array from the given array of integer numbers using C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 801 Views

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 More

How to find the length of the longest substring from the given string without repeating the characters using C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 1K+ Views

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 More

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

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 2K+ Views

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 More

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

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 516 Views

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 More

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

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 378 Views

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 More

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

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 320 Views

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

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

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 591 Views

Finding the index of the first unique character in a string is a common programming problem. The approach involves using a frequency array to count character occurrences, then traversing the string again to find the first character with a count of 1. The algorithm uses an array of size 256 to accommodate all ASCII characters. First, we count the frequency of each character, then we find the first character that appears exactly once. Algorithm Steps The solution follows these steps − Create an integer array of size 256 to store character frequencies ...

Read More

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

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 433 Views

The minimum number of jumps problem involves finding the least number of jumps required to reach the end of an array, where each element represents the maximum number of positions you can jump forward from that index. Given an array where each element represents the maximum jump length from that position, we need to find the minimum jumps to reach the last index. For example, in array {1, 3, 6, 3, 2, 3, 6, 8, 9, 5}, we can reach the end in 4 jumps. Jump Path Visualization 1 ...

Read More
Showing 6841–6850 of 21,090 articles
« Prev 1 683 684 685 686 687 2109 Next »
Advertisements