Check existence of pair with given sum in array

Duration: 5 min

This video lesson is available to enrolled students.

Return to /learn/TCS-NQT-PREPARATION/advanced-coding/coding-questions/coding-question/asset-check-existence-of-pair-with-given-sum-in-array-12900 after enrolling

Inside: a video lesson and guided study material.

Module outline

  1. TCS One Shot Free Videos & Notes: TCS Preparation
  2. Numerical Ability (Foundation Section): Ratio and Proportion (Ratios), Divisibility Rules, Data Interpretation, Mixture and Alligation, Permutation and Combination, Logarithm, Number System, HCF LCM, Sequence and Series (Series), Speed Time and Distance, Percentage, Average, Powers and Exponents (Surds and Indices), Algebra, Probability, Profit and Loss, Simple Interest and Compound Interest, Time and Work, Pipe and Cistern, Mensuration and Geometry, Mental Ability, Age Problems, Statistics, Arithmetic, Partnership, Speed Mathematics and Basic Mathematics
  3. Reasoning Ability (Foundation Section): Series (Number and Letter Series) (Numerical Relations and Reasoning), Coding Decoding, Data Sufficiency, Non Verbal Reasoning (Spatial Aptitude) (Spatial Reasoning) (Visual Reasoning), Syllogisms, Deductive and Inductive Reasoning (Logical Deduction and Induction) (Prepositional Reasoning), Seating Arrangements, Blood Relations, Directions (Direction Test), Analytical Reasoning (Counting Figures Reasoning), Puzzle Solving (Puzzles), Calendar, Clock, Cubes & Dices, Selection Decision Table (Decision Making), Crypt Arithmetic, Venn Diagram, Data Arrangement, Inequality, Analogy, Ranking, Order and Sequence, Logical Sequence Of Words
  4. New Pattern-Verbal Ability (Foundation Section): Sentence Completion (Fill in the blanks), English Writing Ability
  5. Old Pattern-Verbal Ability (Foundation Section): Vocabulary, Noun, Subject Verb Agreement (Verb Noun Agreement), Adjectives, Tenses, Pronoun, Preposition, Articles, Direct and Indirect Speech, Conjunction, Modals, Sentence Re-arrangements (Para Jumbles) (Narrative Sequencing), Sentence Correction (Error Correction), Comprehension / Reading Comprehension / Unseen Passages (Critical Reasoning) (Paragraph Questions), Verbal Analogy (Word Based Analogy), Active Passive Voice (Voice), Determiner, Facts, Inference and Judgements, Adverb, Cloze Test (Cloze Passage), Sentence Construction, Verb
  6. Advanced Quantitative Ability: TCS NQT Advanced Quantitative Ability
  7. Advanced Reasoning Ability: TCS NQT Advanced Reasoning Ability
  8. Advanced Coding: Coding Questions by Yash Sir, C Programming by Yash Sir, Python for Placements by Yash Sir, DSA using Python, Object Oriented Programming (OOPS), Programming in C++, Java
  9. TCS Full Length Mock Test:
  10. TCS Interview & Resume Prep: MR & HR Interview Preparation, TCS Interview Preparation, Mock Interview, Interview Recordings, Resume Preparation, Resume Review
  11. Mega Test:
  12. Grand Test:
AI summary & chapters

AI Summary

An AI-generated summary of this video lecture.

This video is a programming tutorial that demonstrates an optimal algorithm to find a pair of elements in an array that sum to a given value. The lecture begins with an introduction to the problem, 'Program to check existence of pair with given sum in array (Naive Approach)', and then transitions to a more efficient 'Optimal Approach'. The instructor, a 'Placement Expert', explains the logic of sorting the array using quicksort and then applying a two-pointer technique. The code is written in C, with the main function calling a 'PairDetection' function that uses left and right pointers to traverse the sorted array. The video includes a live coding session on the GDB online compiler, where the algorithm is tested with the input array [2, 8, 1, 3, 4] and sum 9, successfully finding the pair (1, 8). The instructor also provides a complete C program with input/output handling and memory deallocation.

Chapters

  1. 0:00 – 2:00 00:00-02:00

    The video starts with the 'KNOX MEEHEGATF' logo, which is a stylized version of 'KNOWLEDGE GATE'. It then transitions to a presentation slide showing the 'KNOWLEDGE GATE' brand and its social media handles for various educational topics like 'KG GATE & PLACEMENT PREP', 'KG CODING', and 'KG UGC NET COLLEGE PREP'. The main content begins with a PowerPoint slide titled 'Program to check existence of pair with given sum in array (Naive Approach)'. The instructor, identified as a 'PLACEMENT EXPERT' and 'Yash Jain Sir', introduces the problem and begins to explain the code. The screen shows a C code snippet with the function declaration 'int PairDetection(int A[], int arr_size, int sum)' and the start of its implementation, including variable declarations for left and right pointers and a call to 'quickSort(A, 0, arr_size - 1);'. A green box on the right side of the screen labels this as the 'Code (Optimal Approach)'.

  2. 2:00 – 5:00 02:00-05:00

    The instructor continues to write and explain the C code for the 'Optimal Approach'. The code is displayed on a dark-themed editor. The function 'PairDetection' is shown with its parameters and the initialization of two pointers, 'l' and 'r', set to the start and end of the array. The core of the algorithm is a 'while' loop that runs as long as 'l < r'. Inside the loop, the sum of the elements at the two pointers is compared to the target sum. If the sum is equal, the function returns 1. If the sum is less than the target, the left pointer 'l' is incremented. If the sum is greater, the right pointer 'r' is decremented. The instructor uses a whiteboard to illustrate the logic with a sample array [2, 8, 1, 3, 4] and a target sum of 9, showing how the pointers move. The code is completed with a final 'return 0;' statement. The instructor then moves to the main function, showing how to prompt the user for input, call the 'PairDetection' function, and print the result. The code includes 'printf' and 'scanf' statements and a 'free(ptr);' statement for memory deallocation.

  3. 5:00 – 5:10 05:00-05:10

    The video transitions to a live demonstration of the code in the GDB online compiler. The instructor runs the program with the input array [2, 8, 1, 3, 4] and a sum of 9. The output displayed in the console is 'Array has two elements with given sum', confirming that the algorithm correctly identified the pair (1, 8). The instructor concludes the demonstration, and the video ends with the GDB compiler interface visible on the screen.

The video provides a comprehensive, step-by-step tutorial on solving the 'two sum' problem using an optimal algorithm. It begins by introducing the problem and then systematically builds the solution, starting with the core logic of sorting the array and using a two-pointer technique. The instructor clearly explains the algorithm's logic, using both code and a whiteboard to illustrate the pointer movement. The lesson progresses from the theoretical explanation to a complete, runnable C program, which is then tested in a live environment to confirm its correctness. The synthesis of theory, code, and practical demonstration makes the concept accessible and reinforces the learning of an efficient algorithmic approach.

Loading lesson…