---
title: "Provide an example of an algorithm and determine its Big-O notation"  
description: "Provide an example of an algorithm and determine its Big-O notation"  
author: "Revati S Misra"  
published: 2023-04-19  
updated: 2023-04-24  
canonical: https://www.mindstick.com/forum/157920/provide-an-example-of-an-algorithm-and-determine-its-big-o-notation  
category: "algorithm"  
tags: ["algorithm", "Algorithm analysis"]  
reading_time: 2 minutes  

---

# Provide an example of an algorithm and determine its Big-O notation

Provide an example of an [algorithm and determine](https://www.mindstick.com/forum/157927/give-an-example-of-an-algorithm-and-determine-its-time-complexity-in-worst-best-and-average-cases) its Big-O notation

## Replies

### Reply by Aryan Kumar

The time complexity of this [algorithm](https://www.mindstick.com/blog/119/implementing-cryptography-in-c-sharp-dot-net-by-using-sha1-algorithm) is O(n), where n is the length of the array. This is because the algorithm needs to iterate over each element in the array once to find the maximum value. The space complexity is O(1), because the algorithm only needs to store one variable (max) to find the maximum value.

In the worst case scenario, the algorithm would need to iterate over all n elements in the array to find the maximum value, which would take O(n) time. In the best case scenario, where the first element is the maximum value, the algorithm would only need to iterate over the first element, which would take O(1) time. In the average case, the algorithm would need to iterate over approximately half of the elements in the array to find the maximum value, which would take O(n/2) or O(n) time.

Here is an example of an algorithm that finds the maximum value in an array of integers:

```java
public int findMax(int[] arr) {
   int max = arr[0];
   for (int i = 1; i < arr.length; i++) {
       if (arr[i] > max) {
           max = arr[i];
       }
   }
   return max;
}
```


---

Original Source: https://www.mindstick.com/forum/157920/provide-an-example-of-an-algorithm-and-determine-its-big-o-notation

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
