← Infosys bank

Infosys · Question 3 · Arrays

Find Maximum and Minimum Element

ArraysC++code⏱ O(n)💾 O(1)
Service-based

Problem

Given an integer array, find the largest and smallest elements present in the array.

Approach

Single pass keeping track of min and max seen so far.

Time O(n)Space O(1)

How it works

  1. 1Update min and max on every element.

Sample input / output

Input:
arr = [3, 5, 4, 1, 9]
Output:
[1, 9]

Constraints

1 <= n <= 10^5; non-empty array.

Solution — read it, then walk it

Time O(n)Space O(1)Returns 1source · C++
Scroll sideways for wide charts — the view follows the active step.
STARTinclude <iostream>include <vector>include <limits>using namespace stdpair<int, int>findMinMax(constvector<int>& arr)DEFINE main()vector<int> arr = {3, 5, 4,1, 9}auto p = findMinMax(arr)cout << "[" << p.first <<", " << p.second << "]" <<endlRETURN 0END
  • start / end
  • statement
  • condition
  • loop
  • returns
  • definition
1/11

StartSTART

Interactive control-flow diagram and step-by-step code walkthrough.

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits