Bird
0
0
DSA Cprogramming~30 mins

Container With Most Water in DSA C - Build from Scratch

Choose your learning style9 modes available
Container With Most Water
📖 Scenario: You are helping a water storage company find the best two vertical lines to hold the most water between them. Each line's height is given, and you want to find the maximum water container that can be formed between any two lines.
🎯 Goal: Build a program that finds the maximum amount of water that can be contained between two lines represented by an array of heights.
📋 What You'll Learn
Create an array called height with the exact values: 1, 8, 6, 2, 5, 4, 8, 3, 7
Create two integer variables left and right to point to the start and end of the array
Use a while loop to move the pointers and calculate the maximum water container
Print the maximum water container value using printf
💡 Why This Matters
🌍 Real World
This problem models how to find the best container shape to hold the most water between vertical lines, useful in storage and design.
💼 Career
Understanding two-pointer techniques and array traversal is important for coding interviews and real-world algorithm optimization.
Progress0 / 4 steps
1
Create the height array
Create an integer array called height with these exact values: 1, 8, 6, 2, 5, 4, 8, 3, 7
DSA C
Hint

Use curly braces to list the values inside the array.

2
Initialize pointers
Create two integer variables called left and right. Set left to 0 and right to 8 (the last index of the array).
DSA C
Hint

Remember the array has 9 elements, so the last index is 8.

3
Calculate maximum water container
Create an integer variable maxArea and set it to 0. Use a while loop with the condition left < right. Inside the loop, calculate the area between height[left] and height[right]. Update maxArea if the current area is larger. Move left forward if height[left] is less than height[right], otherwise move right backward.
DSA C
Hint

Use a loop to check all pairs by moving pointers inward.

4
Print the maximum water container
Use printf to print the value of maxArea followed by a newline.
DSA C
Hint

Use printf("%d\n", maxArea); to print the result.