#include <stdio.h>
int main() {
int arr[] = {5, 2, 9, 1, 5, 6};
int size = sizeof(arr) / sizeof(arr[0]); // Calculate the size of the array
int max = arr[0]; // Assume the first element as maximum
int min = arr[0]; // Assume the first element as minimum
for (int i = 1; i < size; i++) {
if (arr[i] > max) {
max = arr[i]; // Update maximum if current element is greater
}
if (arr[i] < min) {
min = arr[i]; // Update minimum if current element is smaller
}
}
printf("Maximum element: %d\n", max);
printf("Minimum element: %d\n", min);
return 0;
}
#include <stdio.h>
: This line tells the compiler to include the standard input and output library, which provides functions for input and output operations.
int main() {
: This is the beginning of the main function. All C programs start executing from here.
int arr[] = {5, 2, 9, 1, 5, 6};
: Here, we define an array named arr
containing a list of numbers.
int size = sizeof(arr) / sizeof(arr[0]);
: We calculate the size of the array by dividing the total size of the array by the size of a single element. This gives us the number of elements in the array.
int max = arr[0];
: We initialize a variable max
with the value of the first element in the array. This variable will keep track of the maximum value.
int min = arr[0];
: Similarly, we initialize a variable min
with the value of the first element in the array. This variable will keep track of the minimum value.
for (int i = 1; i < size; i++) {
: We start a loop that goes through each element in the array, starting from the second element (index 1) since we already initialized max
and min
with the first element.
if (arr[i] > max) { max = arr[i]; }
: Inside the loop, we compare the current element (arr[i]
) with the current maximum value (max
). If the current element is greater than the current maximum, we update max
with the value of the current element.
if (arr[i] < min) { min = arr[i]; }
: Similarly, we compare the current element with the current minimum value (min
). If the current element is smaller than the current minimum, we update min
with the value of the current element.
}
: The closing brace ends the loop.
printf("Maximum element: %d\n", max);
: After the loop, we use printf
to print out the maximum value that we found.
printf("Minimum element: %d\n", min);
: We also print out the minimum value that we found.
return 0;
: This line indicates that the program has finished running and is returning a status of 0, which typically indicates successful execution.
}
: The closing brace ends the main
function.
Overall, this code reads through an array of numbers, compares each number to find the maximum and minimum values, and then prints out these maximum and minimum values.
Start
Declare an integer array arr
with some values.
Calculate the size of the array by dividing the total size of arr
by the size of its first element. Store the result in the variable size
.
Initialize variables max
and min
with the value of the first element of arr
.
Loop through each element of the array from index 1 to size - 1
: a. Check if the current element arr[i]
is greater than the current maximum value max
.
If true, update max
with the value of arr[i]
. b. Check if the current element arr[i]
is smaller than the current minimum value min
.
If true, update min
with the value of arr[i]
.
After the loop finishes, print the value of max
as the maximum element found in the array.
Print the value of min
as the minimum element found in the array.
End
This algorithm describes the step-by-step process of how the code works to find the maximum and minimum elements in the given integer array.
The time complexity of the above code is O(n), where n is the number of elements in the array.
Here's why:
The loop iterates through each element of the array exactly once. Since there are n elements in the array, the loop runs n times.
Inside the loop, there are only simple constant-time operations: comparisons and assignments. These operations take roughly the same amount of time regardless of the value of n.
As a result, the dominant factor in determining the time complexity is the number of iterations through the loop, which is directly proportional to the number of elements in the array (n). Therefore, the time complexity of this code is O(n), also known as linear time complexity.
The space complexity of the above code is O(1), which is constant space complexity.
Here's why:
The memory usage of the program doesn't grow with the size of the input array. The memory usage remains constant regardless of the number of elements in the array.
The program uses a fixed number of variables (arr
, size
, max
, min
, i
) that don't depend on the input size. These variables occupy a constant amount of memory throughout the execution of the program.
The program doesn't use any data structures like arrays or lists that would consume additional memory proportional to the input size.
Because the memory usage of the program remains constant regardless of the input size, the space complexity is O(1), indicating constant space usage.
Thank You