Check existence of pair with given sum in array
Duration: 5 min
This video lesson is available to enrolled students.
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
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: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.
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.