ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Find Minimum Element in a List

Cypher (Neo4j)

Goal -- WPM

Ready
Exercise Algorithm Area
1WITH $numbers AS numberList
2
3// Handle the edge case where the input list is empty.
4IF size(numberList) = 0 THEN
5RETURN null AS minimumValue
6END
7
8// Initialize the minimum value with the first element of the list.
9// This assumes the list is not empty, which is checked above.
10LET currentMin = numberList[0]
11
12// Iterate through the rest of the list starting from the second element.
13// The loop runs from index 1 up to (but not including) the size of the list.
14FOR i FROM 1 TO size(numberList) - 1
15// Get the current element being examined.
16LET currentElement = numberList[i]
17
18// Compare the current element with the current minimum.
19// If the current element is smaller, update the current minimum.
20IF currentElement < currentMin THEN
21SET currentMin = currentElement
22END
23END
24
25// Return the final minimum value found after iterating through the entire list.
26RETURN currentMin AS minimumValue
Algorithm description viewbox

Find Minimum Element in a List

Algorithm description:

This Cypher query finds the minimum element within a provided list of numbers. It's a fundamental operation used in various data analysis tasks, such as identifying the lowest value in a dataset or finding the smallest price in a list of transactions.

Algorithm explanation:

The algorithm initializes a variable `currentMin` with the first element of the input list. It then iterates through the remaining elements of the list. In each iteration, it compares the current element with `currentMin`. If the current element is found to be smaller than `currentMin`, `currentMin` is updated to this new smaller value. This process continues until all elements have been checked. The time complexity is O(n), where n is the number of elements in the list, because each element is visited exactly once. The space complexity is O(1) as it only uses a few variables to store the current minimum and loop index, regardless of the input list size. An edge case for an empty list is handled by returning `null`.

Pseudocode:

FUNCTION findMinimum(numbers):
  IF numbers IS empty:
    RETURN null
  END

  LET currentMin = numbers[0]

  FOR i FROM 1 TO length(numbers) - 1:
    LET currentElement = numbers[i]
    IF currentElement < currentMin:
      SET currentMin = currentElement
    END
  END

  RETURN currentMin
END FUNCTION