Find the minimum and maximum value in an array
Difficulty: Medium
M = slope (scaled max.scaled min.)/(input max.input min.) x = input value b = offset (y intercept) = scaled min - (input min. X slope) Note: The Input Minimum, Input Maximum, Scaled Minimum, and Scaled Maximum are used to determine the slope and offset values. The input value can go outside of the specified input limits and no ordering is. Int intinput (char. msg, int min, int max) Here, char.msg - Is the user input message that will appear when we input the value. Int min - Minimum input value to validate. Int max - Maximum input value to validate. Int (return type) - Function will return an integer value that can be stored in the actual variable declared in the parent. Min and max attributes for date input type have very little browser support. You can use jQuery validation as a workaround. Input id='dateInput' required='required' min='1900-01-01' max='2099-09-13' type='date' class='form-control' id='inputDataNascimento' $ ('#dateInput').validate. Take the input from the input element and convert it to number using Number method. Use IF-ELSE Condition to verify if it is in range or not? If the number is less than the minimum value then give it the minimum value else if. If the number is greater than the maximum value then give it the maximum value else the number is in the range itself.
Input Type Number Min
Asked in: Facebook
Understanding the Problem

Problem Description: Given an array A[] of size n, you need to find the maximum and minimum element present in the array. Your algorithm should make the minimum number of comparisons.
For Example:
Input: A[] = { 4, 2, 0, 8, 20, 9, 2}
Output: Maximum: 20, Minimum: 0
Input: A[] = {-8, -3, -10, -32, -1}
Output: Maximum: -1, Minimum: -32
Possible follow-up questions to ask the interviewer :-
- Are the array elements necessarily positive? (Ans: No, they can be positive, negative, or zero)
- Are the array element sorted ? (Ans: No, they can be in any order)
- Can the array contain duplicates? (Ans: Sure, that's a possibility.)
Problem Note:- The interviewer would not judge your algorithm for this question based on the time complexity as all solution has the time complexity of O(n). The bottleneck parameter in this problem is the number of comparisons that your algorithm requires to determine the maximum and the minimum element. You need to decrease the number of comparisons as much as you can. Acer acerpower 2000 driver download for windows 10.
Solutions
- Searching linearly: Increment the loop by 1
- Divide and Conquer: Tournament Method
- Comparison in pairs: Increment the loop by 2
1. Searching linearly: Increment the loop by 1
We initialize both minimum and maximum element to the first element and then traverse the array, comparing each element and update minimum and maximum whenever necessary.
Pseudo-Code
Complexity Analysis
At every step of the loop, we are doing 2 comparisons in the worst case. Total no. of comparisons (in worst case) = 2*(n-1) = 2n - 2
Amij v3g modem drivers download for windows 10, 8.1, 7, vista, xp. Time complexity = O(n), Space complexity = O(1)
In the best case, a total of n-1 comparisons have been made. (How?)
Critical ideas to think!
- We have initialized maximum and minimum with the first element of the array - why?
- What would be the best case and worst case input?
- How can we decrease the number of comparisons made here?
2. Divide and Conquer : Tournament Method
Another way to do this could be by following the divide and conquer strategy. Just like the merge sort, we could divide the array into two equal parts and recursively find the maximum and minimum of those parts. After this, compare the maximum and minimum of those parts to get the maximum and minimum of the whole array.
Solution Steps
- Write a recursive function accepting the array and its start and end index as parameters
- The base cases will be
- If array size is 1, return the element as both max and min
- If array size is 2, compare the two elements and return maximum and minimum
3. The recursive part is
- Recursively calculate and store the maximum and minimum for left and right parts
- Determine the maximum and minimum among these by 2 comparisons
4. Return max and min.
Pseudo Code

Complexity Analysis
For counting the number of comparisons, since this is a recursive function, let us define the recurrence relation :
Time complexity = O(n) and space complexity = O(logn) (For recursion call stack)
If n is a power of 2, the algorithm needs exactly 3n/2–2 comparisons to find min and max. If it's not a power of 2, it will take a few more(not significant).
Critical ideas to think!
- How do we analyze the recursion by the master's theorem and recursion tree method?
- How is the space complexity derived to be O(logn)?
- Why there are 2 base cases? What if we remove the base case with array size 2?
- Why prefer mid = start + (end - start)/2 over (start + end)/2 when calculating middle of the array ?
- Can the number of comparisons be decreased further?
3. Comparison in Pairs : Increment the loop by 2
In this approach, we pick array elements in pairs and update the min and max. If the array size is odd, we initialize the first element as both min and max, and if it's even, we compare the first two elements and initialize min and max accordingly.
Solution Steps
- Create max and min variables.
- Check for the size of the array
- If odd, initialize min and max to the first element
- If even, compare the elements and set min to the smaller value and max to the bigger value
3. Traverse the array in pairs
4. For each pair, compare the two elements and then
- Compare the bigger element with max, update max if required.
- Compare the smaller element with min, update min if required.
5. Return max and min.
Pseudo Code
Complexity Analysis
Time Complexity is O(n) and Space Complexity is O(1).
For each pair, there are a total of three comparisons, first among the elements of the pair and the other two with min and max.
Total number of comparisons:-
- If n is odd, 3 * (n-1) / 2
- If n is even, 1 + 3*(n-2)/2 = 3n/2-2
Critical ideas to think!
- Why min and max are initialized differently for even and odd sized arrays?
- Why incrementing the loop by 2 help to reduce the total number of comparsion ?
- Is there any other way to solve this problem? Think.
- In which case, the number of comparisons by method 2 and 3 is equal?
Comparison of different solutions
Suggested Problems to solve
- Find the smallest and second smallest element in the array using minimum number of comparsions
- Find the minimum element in a sorted and rotated array
- Find Kth largest element in an array
- Find K largest element in an array
- Find the middle element among three numbers
- Find median of k sorted arrays
Please write comments if you find any error or bug. Happy Coding! Enjoy Algorithms!
AfterAcademy Data Structure And Algorithms Online Course - Admissions Open
Feature scaling is a method used to normalize the range of independent variables or features of data. In data processing, it is also known as data normalization and is generally performed during the data preprocessing step.
Motivation[edit]
Since the range of values of raw data varies widely, in some machine learning algorithms, objective functions will not work properly without normalization. For example, many classifiers calculate the distance between two points by the Euclidean distance. If one of the features has a broad range of values, the distance will be governed by this particular feature. Therefore, the range of all features should be normalized so that each feature contributes approximately proportionately to the final distance.
Another reason why feature scaling is applied is that gradient descent converges much faster with feature scaling than without it.[1]
It's also important to apply feature scaling if regularization is used as part of the loss function (so that coefficients are penalized appropriately).
Methods[edit]
Rescaling (min-max normalization)[edit]
Also known as min-max scaling or min-max normalization, is the simplest method and consists in rescaling the range of features to scale the range in [0, 1] or [−1, 1]. Selecting the target range depends on the nature of the data. The general formula for a min-max of [0, 1] is given as:

where is an original value, is the normalized value. For example, suppose that we have the students' weight data, and the students' weights span [160 pounds, 200 pounds]. To rescale this data, we first subtract 160 from each student's weight and divide the result by 40 (the difference between the maximum and minimum weights).
To rescale a range between an arbitrary set of values [a, b], the formula becomes:
where are the min-max values.
Mean normalization[edit]
where is an original value, is the normalized value. There is another form of the means normalization which is when we divide by the standard deviation which is also called standardization.
Standardization (Z-score Normalization)[edit]
In machine learning, we can handle various types of data, e.g. audio signals and pixel values for image data, and this data can include multiple dimensions. Feature standardization makes the values of each feature in the data have zero-mean (when subtracting the mean in the numerator) and unit-variance. This method is widely used for normalization in many machine learning algorithms (e.g., support vector machines, logistic regression, and artificial neural networks).[2][citation needed] The general method of calculation is to determine the distribution mean and standard deviation for each feature. Next we subtract the mean from each feature. Then we divide the values (mean is already subtracted) of each feature by its standard deviation.
Where is the original feature vector, is the mean of that feature vector, and is its standard deviation.
Scaling to unit length[edit]
Another option that is widely used in machine-learning is to scale the components of a feature vector such that the complete vector has length one. This usually means dividing each component by the Euclidean length of the vector:
In some applications (e.g., histogram features) it can be more practical to use the L1 norm (i.e., taxicab geometry) of the feature vector. This is especially important if in the following learning steps the scalar metric is used as a distance measure.[why?]
Application[edit]
Html Input Minimum Value
In stochastic gradient descent, feature scaling can sometimes improve the convergence speed of the algorithm[2][citation needed]. In support vector machines,[3] it can reduce the time to find support vectors. Note that feature scaling changes the SVM result[citation needed].
See also[edit]
- fMLLR, Feature space Maximum Likelihood Linear Regression
References[edit]
- ^Ioffe, Sergey; Christian Szegedy (2015). 'Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift'. arXiv:1502.03167 [cs.LG].
- ^ abGrus, Joel (2015). Data Science from Scratch. Sebastopol, CA: O'Reilly. pp. 99, 100. ISBN978-1-491-90142-7.
- ^Juszczak, P.; D. M. J. Tax; R. P. W. Dui (2002). 'Feature scaling in support vector data descriptions'. Proc. 8th Annu. Conf. Adv. School Comput. Imaging: 25–30. CiteSeerX10.1.1.100.2524.
Further reading[edit]
- Han, Jiawei; Kamber, Micheline; Pei, Jian (2011). 'Data Transformation and Data Discretization'. Data Mining: Concepts and Techniques. Elsevier. pp. 111–118.
External links[edit]
Input Min Max Not Working
