📖 Scenario: Imagine you are analyzing a sequence of daily temperatures to find the longest period where the temperature keeps increasing day by day.
🎯 Goal: You will write a C program to find the length of the longest increasing subsequence (LIS) in a given array of integers.
📋 What You'll Learn
Create an integer array called arr with the exact values: 10, 22, 9, 33, 21, 50, 41, 60
Create an integer variable called n that stores the length of arr
Create an integer array called lis of the same length as arr
Use a for loop with variable i to initialize all elements of lis to 1
Use nested for loops with variables i and j to compute the LIS values
Create an integer variable called max_lis to track the maximum LIS length
Print the value of max_lis as the final output
💡 Why This Matters
🌍 Real World
Finding trends in data such as stock prices, temperatures, or sales figures where increasing sequences are important.
💼 Career
Understanding LIS helps in algorithm design, dynamic programming, and problem-solving skills required in software development and data analysis roles.
Progress0 / 4 steps
1
Create the input array and its length
Create an integer array called arr with these exact values: 10, 22, 9, 33, 21, 50, 41, 60. Then create an integer variable called n that stores the length of arr.
DSA C
Hint
Use sizeof(arr) / sizeof(arr[0]) to get the length of the array.
2
Initialize the LIS array
Create an integer array called lis of length n. Use a for loop with variable i from 0 to n - 1 to set all elements of lis to 1.
DSA C
Hint
Use a for loop from 0 to n - 1 and set lis[i] = 1.
3
Compute the LIS values
Use nested for loops with variables i from 1 to n - 1 and j from 0 to i - 1. Inside the inner loop, if arr[i] > arr[j] and lis[i] < lis[j] + 1, update lis[i] to lis[j] + 1. Then create an integer variable called max_lis and set it to 1. Use a for loop with variable i from 0 to n - 1 to update max_lis to the maximum value in lis.
DSA C
Hint
Use two loops to compare each pair of elements and update lis. Then find the maximum value in lis.
4
Print the length of the longest increasing subsequence
Use printf to print the value of max_lis with the exact format: "Length of LIS is %d\n".
DSA C
Hint
Use printf("Length of LIS is %d\n", max_lis); to print the result.
Practice
(1/5)
1. What does the Longest Increasing Subsequence (LIS) problem ask you to find in a sequence of numbers?
easy
A. The longest subsequence where each number is larger than the previous one
B. The longest subsequence where each number is smaller than the previous one
C. The longest subsequence where numbers are equal
D. The shortest subsequence with increasing numbers
Solution
Step 1: Understand the LIS definition
The Longest Increasing Subsequence is a subsequence where every next number is strictly greater than the previous one.
Step 2: Compare options with the definition
The longest subsequence where each number is larger than the previous one matches the definition exactly. Options B, C, and D do not describe LIS correctly.
Final Answer:
The longest subsequence where each number is larger than the previous one -> Option A
Quick Check:
LIS = longest increasing subsequence [OK]
Hint: LIS means strictly increasing sequence, not equal or decreasing [OK]
Common Mistakes:
Confusing increasing with non-decreasing
Thinking LIS means equal numbers
Mixing up subsequence with substring
2. Which of the following C code snippets correctly initializes the array to store LIS lengths for each element in an array arr of size n?
easy
A. int lis[n] = {0};
B. int lis[n]; for(int i=0; i
C. int lis[n]; for(int i=0; i
D. int lis[n]; memset(lis, 1, n*sizeof(int));
Solution
Step 1: Understand LIS initialization
Each element starts with LIS length 1 because each element alone is a subsequence.
Step 2: Check code correctness
int lis[n]; for(int i=0; i
Final Answer:
int lis[n]; for(int i=0; i -> Option B
Quick Check:
Initialize LIS array with 1 [OK]
Hint: Initialize LIS array elements to 1, not 0 [OK]
Common Mistakes:
Using memset to set integers to 1 (wrong)
Initializing LIS array with zeros
Not initializing LIS array at all
3. What is the output of the following C code snippet that calculates the length of LIS for array arr = {10, 22, 9, 33, 21, 50, 41, 60}?
int arr[] = {10, 22, 9, 33, 21, 50, 41, 60};
int n = 8;
int lis[8];
for(int i=0; i arr[j] && lis[i] < lis[j] + 1) {
lis[i] = lis[j] + 1;
}
}
}
int max = lis[0];
for(int i=1; i max) max = lis[i];
}
printf("%d", max);
Hint: Trace LIS array updates carefully for each element [OK]
Common Mistakes:
Counting non-increasing subsequences
Forgetting to update lis[i] only if condition met
Mixing up indices in nested loops
4. Identify the bug in the following C code snippet that attempts to compute the LIS length:
int arr[] = {3, 10, 2, 1, 20};
int n = 5;
int lis[5];
for(int i=0; i arr[j] && lis[i] < lis[j] + 1) {
lis[i] = lis[j] + 1;
}
}
}
int max = lis[0];
for(int i=1; i max) max = lis[i];
}
printf("%d", max);
medium
A. LIS array should be initialized with 1, not 0
B. The nested loops should start from i=0
C. The condition arr[i] > arr[j] should be arr[i] >= arr[j]
D. The max variable should be initialized to 0
Solution
Step 1: Check LIS array initialization
LIS array elements represent length of subsequence ending at each index. Each element alone forms subsequence of length 1, so initialize lis[i] = 1.
Step 2: Analyze effect of initialization
Initializing lis[i] = 0 causes all comparisons lis[i] < lis[j] + 1 to fail or produce wrong results, leading to incorrect max LIS length.
Final Answer:
LIS array should be initialized with 1, not 0 -> Option A
Quick Check:
Initialize LIS array with 1 to avoid zero-length subsequences [OK]
Hint: Always start LIS lengths at 1, not 0 [OK]
Common Mistakes:
Initializing LIS array with zeros
Changing comparison operators incorrectly
Starting loops at wrong indices
5. Given an array arr = {5, 7, 4, 8, 6, 9}, write a C function to find the length of the Longest Increasing Subsequence using dynamic programming. What is the length of the LIS for this array?
hard
A. 6
B. 3
C. 5
D. 4
Solution
Step 1: Initialize LIS array with 1 for each element
lis = {1,1,1,1,1,1}
Step 2: Update LIS values by comparing each element with previous ones