Check existence of pair with given sum in array

Duration: 5 min

This video lesson is available to enrolled students.

Return to /learn/ACCENTURE-PREPARATION/coding-assessment-questions/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. Behavioral Assessment (Psychometric): Behavioral Assessment (Behavioral Competency Profiling) (Psychometric Test)
  2. Cognitive Assessment (Game-Based): Gamified / Game Based Aptitude Selection Decision Table (Decision Making) Puzzle Solving (Puzzles) Non Verbal Reasoning (Spatial Aptitude) (Spatial Reasoning) (Visual Reasoning) Analytical Reasoning (Counting Figures Reasoning) Cubes & Dices Coding Decoding Ranking, Order and Sequence Series (Number and Letter Series) (Numerical Relations and Reasoning)
  3. Common Applications & MS Office (Technical Assessment): MS Office by Yash Sir
  4. Pseudo Code (Technical Assessment): Pseudo Codes
  5. Networking, Security & Cloud (Technical Assessment): Introduction to CN Data Communication DLL: Access Control DLL: Flow Control DLL: Error Control DLL: Framing Data Link Layer - Ethernet Net Layer: IPv4 & Proto Net Layer: IP Addressing Net Layer:Routing Protocol Transport Layer Services TL: Congestion & UDP Application Layer Hardware basics Network Security Cloud Computing and IoT Cyber and Info Security Emerging Tech Trends
  6. Python (Technical Assessment): Python for Placements by Yash Sir
  7. Java (Technical Assessment): Java
  8. OOPs (Technical Assessment): Object Oriented Programming (OOPS)
  9. Data Structure (Coding Round): Introduction to DS Array Stack Queue Linked List Tree Graphs Hashing
  10. Algorithms (Coding Round): Algorithm Analysis Sorting Algorithms Greedy Algorithms Dynamic Programming Minimum Spanning Trees Shortest Path Algos Time Complexity Analysis Advanced Algorithms
  11. Coding Assessment Questions: C Programming by Yash Sir Coding Questions by Yash Sir Programming in C++
  12. SQL (Coding Round): SQL
  13. JavaScript Frontend (Coding Round): JavaScript
  14. Communication Test (Basic Verbal Ability): Vocabulary Sentence Construction Noun Pronoun Verb Tenses Subject Verb Agreement (Verb Noun Agreement) Articles Preposition Conjunction Adverb Modals Sentence Correction (Error Correction) Sentence Completion (Fill in the blanks) Comprehension / Reading Comprehension / Unseen Passages (Critical Reasoning) (Paragraph Questions) Communication Assessment
  15. HR Interview: MR & HR Interview Preparation
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…