Search⌘ K
AI Features

Solution Review: Finding Minimum Value in an Array

Explore the method to find the smallest value in an array by iterating through each element once. Learn how to track the minimum value efficiently and understand the linear time complexity O(n), essential for coding interviews and improving algorithm skills.

We'll cover the following...

Solution #

C#
namespace chapter_2
{
class Solution
{
// Find Minimum Value in an Array
static int findMinimum(int []arr, int size)
{
int minimum = arr[0];
//At every index compare its value with the minimum and if it is less
//then make that index value the new minimum value”
for (int i = 0; i < size; i++)
{
if (arr[i] < minimum)
{
minimum = arr[i];
}
}
return minimum;
}
static void Main(string[] args)
{
int size = 4;
int []arr = { 9, 2, 3, 6 };
Console.Write("Array : ");
for (int i = 0; i < size; i++)
Console.Write(arr[i] + " ");
Console.WriteLine("");
int min = findMinimum(arr, size);
Console.WriteLine("Minimum in the Array: " + min );
return ;
}
}
}

Explanation

Start with the first element (which is 9 in this example), and save it as the smallest value. ...