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 70 of 196
How to implement minimum step to one using topDown approach using C#?
The minimum steps to one problem asks: given a positive integer n, find the minimum number of steps to reduce it to 1 using these operations − If n is divisible by 3, divide it by 3 If n is divisible by 2, divide it by 2 Subtract 1 from n The top-down approach uses recursion with memoization to solve this problem efficiently by storing computed results in a DP array. How It Works The algorithm recursively explores all possible operations at each step and chooses the one that gives the minimum result. Memoization ...
Read MoreHow to implement coin change problem using bottom-up approach using C#?
The coin change problem is a classic dynamic programming challenge where we need to find the minimum number of coins required to make a given amount. The bottom-up approach builds solutions incrementally, starting from the smallest amounts and working up to the target amount. This approach uses dynamic programming to avoid recalculating the same subproblems multiple times, storing previously computed results in an array. Time complexity − O(n × t) where n is the amount and t is the number of coin types Space complexity − O(n) for the dynamic programming array Syntax Following is the ...
Read MoreHow to check whether the tree is symmetric or not using iterative in C#?
A symmetric binary tree is one where the left subtree is a mirror reflection of the right subtree. In the iterative approach, we use two queues to traverse both subtrees simultaneously and compare their values in mirror positions. The algorithm works by enqueueing nodes from the left and right subtrees in opposite orders. For the left subtree, we add left child first, then right child. For the right subtree, we add right child first, then left child. This ensures we compare mirror positions correctly. Syntax Following is the basic structure for checking tree symmetry iteratively − ...
Read MoreHow to check whether the tree is symmetric or not using recursion in C#?
A symmetric binary tree is a tree where the left subtree is a mirror image of the right subtree. In C#, we can check if a tree is symmetric using recursion by comparing corresponding nodes from left and right subtrees. The recursive approach works by first checking if the tree is null (which is symmetric by definition), then calling a helper method to compare the left and right subtrees as mirror images of each other. Algorithm Steps If the root is null, the tree is symmetric Compare left subtree with right subtree using a mirror comparison ...
Read MoreHow to rotate an array k time using C#?
Array rotation is a common programming problem where elements are shifted by k positions. When we rotate an array to the right by k positions, the last k elements move to the beginning, and the remaining elements shift right. For example, if we have array [1, 2, 3, 4, 5] and rotate it right by 2 positions, we get [4, 5, 1, 2, 3]. Algorithm The most efficient approach uses the reversal algorithm which works in three steps − Reverse the entire array Reverse the first k elements Reverse the remaining elements from position k ...
Read MoreWhat are the different ways to find missing numbers in a sorted array without any inbuilt functions using C#?
Finding missing numbers in a sorted array is a common programming problem with multiple efficient solutions. This article explores three different approaches to identify missing numbers without using built-in functions in C#. Each method has its own advantages in terms of time and space complexity, making them suitable for different scenarios depending on your specific requirements. Using Sum Formula (Mathematical Approach) This method uses the mathematical formula n(n+1)/2 to calculate the expected sum of consecutive numbers, then subtracts the actual sum of array elements to find the missing number − using System; public class ...
Read MoreHow to find the missing number and the repeated number in a sorted array without using any inbuilt functions using C#?
Finding the missing number and the repeated number in a sorted array is a common programming problem. This article demonstrates how to solve this problem without using any built-in functions in C#. The approach uses a boolean array to track which numbers are present and an integer array to count occurrences. By traversing the original array and marking elements in auxiliary arrays, we can identify both the missing and repeated elements efficiently. Algorithm Overview The solution works in two main steps − Finding the missing number: Create a boolean array and mark elements as ...
Read MoreHow to find the unique combination k sum that corresponds to k sum using C#?
The k-sum combination problem involves finding all unique combinations of exactly k numbers from a given range that sum up to a specific target value. This is solved using backtracking, which explores all possible combinations while pruning invalid paths early. The algorithm maintains two lists: an output list to store valid combinations and a currentList to track the current combination being explored. The backtracking function recursively explores combinations, adding numbers one by one until either the target sum is achieved with exactly k numbers, or the constraints are violated. How It Works The backtracking algorithm follows these ...
Read MoreHow to find the minimum number of steps needed by knight to reach the destination using C#?
In chess, a knight moves in an L-shape − two squares in one direction and one square perpendicular to that. This article explains how to find the minimum number of steps (moves) needed for a knight to reach a destination cell on a chessboard using Breadth-First Search (BFS) algorithm in C#. The knight's movement pattern creates a shortest path problem that can be efficiently solved using BFS, as it explores all possible moves level by level, guaranteeing the minimum number of steps. Knight's Movement Pattern A knight can move to at most 8 positions from any given ...
Read MoreHow to find the number of times array is rotated in the sorted array by recursion using C#?
To find the number of times a sorted array has been rotated, we need to locate the position of the minimum element. The number of rotations equals the index of the minimum element. We can solve this efficiently using binary search recursion. In a rotated sorted array, the minimum element is the pivot point where the rotation occurred. For example, in array [4, 5, 6, 1, 2, 3], the minimum element 1 is at index 3, meaning the array was rotated 3 times. Algorithm The recursive binary search approach works as follows − Find ...
Read More