Hello folks, this is a new series of solved data structures and algorithms problems in C#.

I will be adding a new problem daily so stay tuned with this page.

Below are the problems:-

Array-Based Problems:

- Write a method to find the
*sum of all the elements*in the integer Array. - Write a method to search the value among all the elements inside the Array.
- Write a method to search the value among all the elements inside the Sorted Array.

String-Based Problems:

### Sum of Array

**Problem:-** Write a method to find the *sum of all the elements* in the integer Array.

**Problem Analysis:-**

*Input*: The method will take Integer Array as an input.

*Process*:- Declare an
*int variable***sum**and initialize with value 0 - Traversed through the array’s elements using
*for loop*- Inside
*for loop*: Add each element value to the**sum**variable

- Inside
- After transversing
*return*the**sum**value.

- Declare an

*Output*: Sum of all the elements in the Array

**Solution:-**

**Output:-**

**Analysis:- ** Since all the elements are traversed the Time complexity is **O(n)**

**Code:-**

public class SumOfArray { public static int GetSumOfArray(int[] intArray) { int sum = 0; for (int index = 0; index < intArray.Length; index++) { sum += intArray[index]; } return sum; } public static void Main() { int[] arr = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; Console.WriteLine($"Sum of all the values in the array: { GetSumOfArray(arr) } "); } }

### Search – Sequence-Based

**Problem 2-** Write a method to search the value among all the elements inside the Array.

**Use Case:- **Sequential Search as we have no idea whether the data in the array is sorted.

**Problem Analysis:-**

*Input*: The method will take 2 parameters as input.- Integer Array
- Value to be searched

*Process*:- Traversed through the array’s elements using
*for loop*- Inside
*for loop*: Match searchValue with each element in the Array

- Inside

- Traversed through the array’s elements using

*Output*: Index Value if*searchValue is found else return -1*

**Solution:-**

**Output:-**

**Analysis:- ** Since all the elements are traversed sequentially the Time complexity is **O(n)**

**Code:-**

public static int SearchSequenceBased(int[] intArray, int searchValue) { for (int index = 0; index < intArray.Length; index++) { if (searchValue == intArray[index]) { return index; } } return -1; } public static void Main() { int[] arr = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; Console.WriteLine($"Searched Value found at index: { SearchSequenceBased(arr, 7) } "); }

### Search – Binary Search

**Problem 3-** Write a method to search the value among all the elements inside the sorted Array.

**Use Case:- **To use Binary Search as we know that the data in the array is sorted.

**Problem Analysis:-**

*Input*: The method will take 2 parameters as input.- Integer Array
- Value to be searched

*Process*:- Traversed through the array’s elements using
*while loop*- Inside
*while loop*: Match searchValue with the array elements divided into half using the middle value.

- Inside

- Traversed through the array’s elements using

*Output*: Index Value if*searchValue is found else return -1*

**Solution:-**

**Output:-**

**Analysis:- ** Since half of the elements are traversed the Time complexity is **O(logn)**

**Code:-**

public static int SearchSortedArray(int[] intArray, int searchValue) { int start = 0; int end = intArray.Length - 1; int middle; while (start <= end) { middle = start + (end - start) /2 ; if ( searchValue == intArray[middle]) { return middle; } else { if ( intArray[middle] < searchValue ) { start = middle + 1; } else { end = middle - 1; } } } return -1; } public static void Main() { int[] arr = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; Console.WriteLine($"Searched Value found at index: { SearchSortedArray(arr, 7) } "); }

### Search – Pattern in a String Using Brute Force Search

**Problem 4-** Find a pattern in a given string.

**Use Case:- **To find occurrences of a word in a given text.

**Problem Analysis:-**

*Input*: The method will take 2 parameters as input.- String Text
- Pattern

*Process*:- Traversed through the text array’s elements using
*while loop to match pattern*- With each iteration inside outer
*while loop*: there is another while loop which matches each character in pattern with text and accordingly increase value of childIndex - If value of childIndex is equal to pattern>length, pattern is found inside text.

- With each iteration inside outer

- Traversed through the text array’s elements using

*Output*: Index Value if*pattern is found else return -1*

**Solution:-**

**Output:-**

**Analysis:- ** Since there is a loop inside a loop the Time complexity is **O(m*n)**

**Code:-**

public static int SearchSortedArray(int[] intArray, int searchValue) { int start = 0; int end = intArray.Length - 1; int middle; while (start <= end) { middle = start + (end - start) /2 ; if ( searchValue == intArray[middle]) { return middle; } else { if ( intArray[middle] < searchValue ) { start = middle + 1; } else { end = middle - 1; } } } return -1; } s int[] arr = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; Console.WriteLine($"Searched Value found at index: { SearchSortedArray(arr, 7) } "); }

*Yes, I know I can cover more algorithms in this series of solved data structures & algorithms problems in C#, which I will be, actually a new algorithm daily. *

*That’s all for today, thank you for your time. Stay tuned for a new algorithm tomorrow.*

*Please feel free to send me any comment, suggestion, or correction you may have by writing in the comment box below.*