Skip to content

Algo Folder - min_max_array.py #11817

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions data_structures/arrays/min_max-element.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""

Check failure on line 1 in data_structures/arrays/min_max-element.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N999)

data_structures/arrays/min_max-element.py:1:1: N999 Invalid module name: 'min_max-element'

Check failure on line 1 in data_structures/arrays/min_max-element.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N999)

data_structures/arrays/min_max-element.py:1:1: N999 Invalid module name: 'min_max-element'

Check failure on line 1 in data_structures/arrays/min_max-element.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N999)

data_structures/arrays/min_max-element.py:1:1: N999 Invalid module name: 'min_max-element'
Author - Seema Kumari Patel - 2024

Retrieves minimum and maximum valued number from an array.
There are two ways to retrieve value(s):

1. Approach #01 -Linear Search.
Initialize values of min and max as minimum and maximum of the first two elements respectively.

Check failure on line 8 in data_structures/arrays/min_max-element.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

data_structures/arrays/min_max-element.py:8:89: E501 Line too long (95 > 88)

Check failure on line 8 in data_structures/arrays/min_max-element.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

data_structures/arrays/min_max-element.py:8:89: E501 Line too long (95 > 88)

Check failure on line 8 in data_structures/arrays/min_max-element.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

data_structures/arrays/min_max-element.py:8:89: E501 Line too long (95 > 88)
Starting from 3rd, compare each element with max and min, and change max and min accordingly.

Check failure on line 9 in data_structures/arrays/min_max-element.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

data_structures/arrays/min_max-element.py:9:89: E501 Line too long (93 > 88)

Check failure on line 9 in data_structures/arrays/min_max-element.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

data_structures/arrays/min_max-element.py:9:89: E501 Line too long (93 > 88)

Check failure on line 9 in data_structures/arrays/min_max-element.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

data_structures/arrays/min_max-element.py:9:89: E501 Line too long (93 > 88)

2. Approach #02 - First sort the array in ascending order.
Once the array is sorted, the first element of the array will be the minimum element and the last element of the array will be the maximum element.

Check failure on line 12 in data_structures/arrays/min_max-element.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

data_structures/arrays/min_max-element.py:12:89: E501 Line too long (147 > 88)

Check failure on line 12 in data_structures/arrays/min_max-element.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

data_structures/arrays/min_max-element.py:12:89: E501 Line too long (147 > 88)

Check failure on line 12 in data_structures/arrays/min_max-element.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

data_structures/arrays/min_max-element.py:12:89: E501 Line too long (147 > 88)

Here is the code for second approach
"""

import java.io.*;

Check failure on line 17 in data_structures/arrays/min_max-element.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff

data_structures/arrays/min_max-element.py:17:16: SyntaxError: Expected an identifier

Check failure on line 17 in data_structures/arrays/min_max-element.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff

data_structures/arrays/min_max-element.py:17:17: SyntaxError: Expected an expression

Check failure on line 17 in data_structures/arrays/min_max-element.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E703)

data_structures/arrays/min_max-element.py:17:17: E703 Statement ends with an unnecessary semicolon

Check failure on line 17 in data_structures/arrays/min_max-element.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff

data_structures/arrays/min_max-element.py:17:16: SyntaxError: Expected an identifier

Check failure on line 17 in data_structures/arrays/min_max-element.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff

data_structures/arrays/min_max-element.py:17:17: SyntaxError: Expected an expression

Check failure on line 17 in data_structures/arrays/min_max-element.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E703)

data_structures/arrays/min_max-element.py:17:17: E703 Statement ends with an unnecessary semicolon

Check failure on line 17 in data_structures/arrays/min_max-element.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff

data_structures/arrays/min_max-element.py:17:16: SyntaxError: Expected an identifier

Check failure on line 17 in data_structures/arrays/min_max-element.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff

data_structures/arrays/min_max-element.py:17:17: SyntaxError: Expected an expression

Check failure on line 17 in data_structures/arrays/min_max-element.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E703)

data_structures/arrays/min_max-element.py:17:17: E703 Statement ends with an unnecessary semicolon
import java.util.*;

Check failure on line 18 in data_structures/arrays/min_max-element.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff

data_structures/arrays/min_max-element.py:18:18: SyntaxError: Expected an identifier

Check failure on line 18 in data_structures/arrays/min_max-element.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff

data_structures/arrays/min_max-element.py:18:19: SyntaxError: Expected an expression

Check failure on line 18 in data_structures/arrays/min_max-element.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E703)

data_structures/arrays/min_max-element.py:18:19: E703 Statement ends with an unnecessary semicolon

Check failure on line 18 in data_structures/arrays/min_max-element.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff

data_structures/arrays/min_max-element.py:18:18: SyntaxError: Expected an identifier

Check failure on line 18 in data_structures/arrays/min_max-element.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff

data_structures/arrays/min_max-element.py:18:19: SyntaxError: Expected an expression

Check failure on line 18 in data_structures/arrays/min_max-element.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E703)

data_structures/arrays/min_max-element.py:18:19: E703 Statement ends with an unnecessary semicolon

Check failure on line 18 in data_structures/arrays/min_max-element.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff

data_structures/arrays/min_max-element.py:18:18: SyntaxError: Expected an identifier

Check failure on line 18 in data_structures/arrays/min_max-element.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff

data_structures/arrays/min_max-element.py:18:19: SyntaxError: Expected an expression

Check failure on line 18 in data_structures/arrays/min_max-element.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E703)

data_structures/arrays/min_max-element.py:18:19: E703 Statement ends with an unnecessary semicolon

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An error occurred while parsing the file: data_structures/arrays/min_max-element.py

Traceback (most recent call last):
  File "/opt/render/project/src/algorithms_keeper/parser/python_parser.py", line 146, in parse
    reports = lint_file(
              ^^^^^^^^^^
libcst._exceptions.ParserSyntaxError: Syntax Error @ 18:17.
parser error: error at 17:16: expected NAME

import java.util.*;
                ^



'''To get the values to min / max respectively'''
class Pair {
public int min;
public int max;
}

'''To sort the array in ascending order'''
class Main {
static Pair getMinMax(int arr[], int n) {
Pair minmax = new Pair();
Arrays.sort(arr);
minmax.min = arr[0];
minmax.max = arr[n - 1];
return minmax;
}

'''To get the array elements'''
public static void main(String[] args) {
int arr[] = { 1000, 11, 445, 1, 330, 3000 };
int arr_size = arr.length;
Pair minmax = getMinMax(arr, arr_size);
System.out.println("Minimum element is " + minmax.min);
System.out.println("Maximum element is " + minmax.max);
}
}
Loading