Found 2587 Articles for Csharp

How to find the length of the longest continuous increasing subsequence from an array of numbers using C#?

Nizamuddin Siddiqui
Updated on 17-Aug-2021 08:12:06

919 Views

LongestIncreaingSubsequence returns integer of the continuous subsequence from the array. The method has a for loop, which iterates and keeps the track of the numbers. the final result will have the Max calculated. Time complexity is O(N) because every element is visited once and the space complexity is O(1) because we are not making use of any storage space.Time complexity − O(N)Space complexity − O(1)Example   − {2, 4, 6, 5, 8}Output − 3Examplepublic class Arrays{    public int longestIncreaingSubsequence(int[] nums){       if (nums == null || nums.Length == 0){          return -1;     ... Read More

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

Nizamuddin Siddiqui
Updated on 17-Aug-2021 08:08:45

1K+ Views

From the given string input, use the sliding window technique by having 2 pointers i and j . both i and j will point to the same character in the string. Traverse through the string and add it to the list. If the repeated character is found then remove it from the list else append to the list.Example 1Input − s = "abcabcbb"Output − 3Explanation − The answer is "abc", with the length of 3.Example 2Input − s = "bbbbb"Output − 1Explanation − The answer is "b", with the length of 1.Time complexity − O(N)Space complexity − O(N)Examplepublic class Arrays{    public int LongestSubstringWithNoRepeatingCharacters(string s){   ... Read More

How to check whether the given strings are isomorphic using C#?

Nizamuddin Siddiqui
Updated on 17-Aug-2021 08:06:07

931 Views

Two strings, X and Y, are called isomorphic if all occurrences of each character in X can be replaced with another character to get Y and vice-versa. For example, consider strings ACAB and XCXY. All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.Example 1Input − s = "egg", t = "add"Output − trueExample 2Input − s = "foo", t = "bar"Output − falseTime complexity − O(N)Space complexity − O(N)codepublic class Arrays{    public bool IsStringIsomorphic(string s, string t){     ... 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
Updated on 17-Aug-2021 08:00:57

728 Views

Create a method MoveZeros, traverse through the array and count the number of Zeros in the array. Based on the count size make all the final cells to zero. Return without processing if the array length is null or empty. The final result will be in nums Array. Time complexity is O(N) because we are traversing through the array once.Time complexity − O(N)Space complexity − O(1)Examplepublic class Arrays{    public void MoveZeros(int[] nums){       if (nums == null || nums.Length == 0){          return;       }       int count = 0; ... Read More

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

Nizamuddin Siddiqui
Updated on 17-Aug-2021 07:57:48

135 Views

Pascal’s Triangle is a number pattern in the shape of a triangle. Pascal’s Triangle has many applications in mathematics and statistics, including its ability to help you calculate combinations.Each number in the triangle is the sum of the two numbers above it. For example, row 4 − it’s the sum of 3 and 3 in the row above. The very first and very last number in any row are always going to be 1.Time complexity − O(N)Space complexity − O(N)Examplepublic class Arrays{    public List GeneratePascal(int n){       List res = new List();       if (n

How to find the quadruplet that is close to target using C#?

Nizamuddin Siddiqui
Updated on 17-Aug-2021 07:51:43

155 Views

Two Pointers pattern and is similar to quadruplet Sum to Zero. We can follow a similar approach to iterate through the array, taking one number at a time. At every step, we will save the difference between the quadruplet and the target number, and at each step we will compare it with the minimum target difference so far, so that in the end, we can return the triplet with the closest sum.Time complexitySorting the array will take O(N* logN). Overall fourSumClosest() will take O(N * logN + N^3), which is asymptotically equivalent to O(N^3).Space complexityThe space complexity of the above ... Read More

How to find all the unique quadruplets that is close to zero using C#?

Nizamuddin Siddiqui
Updated on 17-Aug-2021 07:38:42

250 Views

The easy approach is that we could create four nested loops and check one by one that the sum of all the four elements is zero or not. If the sum of the four elements is zero then print elements.Time Complexity − O(n4)Space Complexity − O(1)We could use an unordered set data structure to store each value of the array. Set provides the benefit of searching an element in O(1) time. So, for each pair in the array, we will look for the negative of their sum that might exist in the set. If such an element is found then ... Read More

How to find the unique triplet that is close to the given target using C#?

Nizamuddin Siddiqui
Updated on 17-Aug-2021 07:29:39

221 Views

Two Pointers pattern and is similar to Triplet Sum to Zero. We can follow a similar approach to iterate through the array, taking one number at a time. At every step, we will save the difference between the triplet and the target number, and at each step we will compare it with the minimum target difference so far, so that in the end, we can return the triplet with the closest sum.Time complexitySorting the array will take O(N* logN). Overall threeSumClosest() will take O(N * logN + N^2), which is asymptotically equivalent to O(N^2).Space complexityThe space complexity of the above ... Read More

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

Nizamuddin Siddiqui
Updated on 17-Aug-2021 07:16:51

272 Views

Create 2 different arrays leftDis and rightDis. The leftDis will store the value when moved from left direction. The rightDis will store the shortest value when moved from right. Whenever the character is met add the position of the character to the array. In the last step calculate the maximum of both the arrays.Time Complexity − O(n)Space Complexity − O(n)Examplepublic class Arrays{    public int[] LongestDistanceToCharacter(string s, char c){       int stringLength = s.Length;       int[] leftDis = new int[s.Length];       int[] rightDis = new int[s.Length];       leftDis = Enumerable.Range(0, s.Length).Select(n => ... Read More

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

Nizamuddin Siddiqui
Updated on 17-Aug-2021 07:07:19

366 Views

Create 2 different arrays leftDis and rightDis. The leftDis will store the value when moved from left direction. The rightDis will store the shortest value when moved from right. Whenever the character is met add the position of the character to the array. In the last step calculate the minimum of both the arrays.Time Complexity − O(n)Space Complexity − O(n)Examplepublic class Arrays{    public int[] ShortestDistanceToCharacter(string s, char c){       int stringLength = s.Length;       int[] leftDis = new int[s.Length];       int[] rightDis = new int[s.Length];       leftDis = Enumerable.Range(0, s.Length).Select(n => ... Read More

Advertisements