← Infosys bank

Infosys · Question 2 · Arrays

Find Pair With Given Sum

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

Problem

Given an integer array and a target sum K, determine if there exists a pair whose sum equals K.

Approach

Use a hash set to check for complement K - x while traversing.

Time O(n)Space O(n)

How it works

  1. 1For each x, look up K - x among seen values.

Sample input / output

Input:
arr = [1, 4, 45, 6, 10, 8], K = 16
Output:
true

Constraints

1 <= n <= 10^5.

Solution — read it, then walk it

Time O(n)Space O(n)Returns 1source · C++
Scroll sideways for wide charts — the view follows the active step.
STARTinclude <iostream>include <vector>include <unordered_set>using namespace stdbool hasPairWithSum(constvector<int>& arr, int K)DEFINE main()vector<int> arr = {1, 4,45, 6, 10, 8}cout << boolalpha <<hasPairWithSum(arr, 16) <<endlRETURN 0END
  • start / end
  • statement
  • condition
  • loop
  • returns
  • definition
1/10

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