📖 Scenario: You are analyzing a sequence of daily temperatures recorded over a week. You want to find the longest period during which the temperature kept increasing day by day.
🎯 Goal: Build a Java program that finds the length of the longest increasing subsequence in a given array of integers representing daily temperatures.
📋 What You'll Learn
Create an integer array called temperatures with the exact values: 3, 10, 2, 1, 20
Create an integer variable called n that stores the length of the temperatures array
Create an integer array called lis of size n to store the longest increasing subsequence lengths
Use a nested for loop with variables i and j to compute the LIS values
Print the length of the longest increasing subsequence using System.out.println
💡 Why This Matters
🌍 Real World
Finding trends in data such as stock prices, temperatures, or sales figures to understand growth periods.
💼 Career
Useful for software engineers and data analysts working on time series data, pattern recognition, and optimization problems.
Progress0 / 4 steps
1
Create the temperatures array
Create an integer array called temperatures with these exact values: 3, 10, 2, 1, 20
DSA Java
Hint
Use curly braces to list the values inside the array.
2
Create the length variable and LIS array
Create an integer variable called n that stores the length of the temperatures array. Then create an integer array called lis of size n.
DSA Java
Hint
Use temperatures.length to get the array size.
3
Compute the LIS values using nested loops
Initialize all elements of lis to 1 using a for loop with variable i. Then use nested for loops with variables i and j where i goes from 1 to n-1 and j goes from 0 to i-1. Inside the inner loop, if temperatures[i] is greater than temperatures[j] and lis[i] is less than lis[j] + 1, update lis[i] to lis[j] + 1.
DSA Java
Hint
First set all lis values to 1. Then compare each pair of elements to update lis.
4
Find and print the length of the longest increasing subsequence
Create an integer variable called maxLength and set it to 0. Use a for loop with variable i from 0 to n-1 to find the maximum value in the lis array and store it in maxLength. Then print maxLength using System.out.println.
DSA Java
Hint
Find the largest number in lis and print it.
Practice
(1/5)
1. What does the Longest Increasing Subsequence (LIS) problem ask for in a given sequence of numbers?
easy
A. The longest subsequence with all equal numbers
B. The longest subsequence where each number is smaller than the previous one
C. The longest subsequence with alternating increasing and decreasing numbers
D. The longest subsequence where each number is larger than the previous one
Solution
Step 1: Understand the problem 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, while others describe different patterns.
Final Answer:
The longest subsequence where each number is larger than the previous one -> Option D
Quick Check:
LIS = strictly increasing subsequence [OK]
Hint: Look for strictly increasing order in subsequence [OK]
Common Mistakes:
Confusing subsequence with substring
Thinking LIS means non-decreasing instead of strictly increasing
Mixing up increasing with decreasing sequences
2. Which of the following Java code snippets correctly initializes an array to store the length of LIS ending at each index for an input array arr of size n?
easy
A. int[] lis = new int[n]; lis.fill(1);
B. int[] lis = new int[n]; for(int i=0; i
C. int[] lis = new int[n]; for(int i=0; i
D. int lis[] = new int[n]; Arrays.fill(lis, 0);
Solution
Step 1: Understand LIS initialization
Each element starts with LIS length 1 because the smallest subsequence is the element itself.
Step 2: Check code correctness
int[] lis = new int[n]; for(int i=0; ifill on array. int lis[] = new int[n]; Arrays.fill(lis, 0); fills with 0, incorrect for LIS.
Final Answer:
int[] lis = new int[n]; for(int i=0; i -> Option C
Quick Check:
Initialize LIS array with 1s [OK]
Hint: Initialize LIS array with 1 for each element [OK]
Common Mistakes:
Initializing LIS array with zeros
Using non-existent array methods like fill()
Not initializing LIS array before DP
3. What is the output of the following Java code snippet that calculates the length of the Longest Increasing Subsequence for the array arr = {10, 22, 9, 33, 21, 50, 41, 60}?
int[] arr = {10, 22, 9, 33, 21, 50, 41, 60};
int n = arr.length;
int[] lis = new int[n];
for (int i = 0; i < n; i++) lis[i] = 1;
for (int i = 1; i < n; i++) {
for (int j = 0; j < i; j++) {
if (arr[i] > arr[j] && lis[i] < lis[j] + 1) {
lis[i] = lis[j] + 1;
}
}
}
int max = 0;
for (int i = 0; i < n; i++) {
if (lis[i] > max) max = lis[i];
}
System.out.println(max);
4. Identify the bug in the following Java code snippet that attempts to compute the length of the Longest Increasing Subsequence for array arr:
int[] lis = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
lis[i] = 0;
}
for (int i = 1; i < arr.length; i++) {
for (int j = 0; j < i; j++) {
if (arr[i] > arr[j] && lis[i] < lis[j] + 1) {
lis[i] = lis[j] + 1;
}
}
}
int max = 0;
for (int i = 0; i < lis.length; i++) {
if (lis[i] > max) max = lis[i];
}
System.out.println(max);
medium
A. LIS array should be initialized with 1, not 0
B. The inner loop should start from i+1 instead of 0
C. The comparison should be arr[i] < arr[j] instead of arr[i] > arr[j]
D. The max variable should be initialized to Integer.MIN_VALUE
Solution
Step 1: Check LIS array initialization
LIS array must start with 1 for each element because each element alone forms a subsequence of length 1. Initializing with 0 causes incorrect LIS lengths.
Step 2: Verify loops and conditions
Inner loop and comparison are correct. Max initialization to 0 is fine since LIS lengths are positive.
Final Answer:
LIS array should be initialized with 1, not 0 -> Option A
Quick Check:
Initialize LIS with 1 to avoid zero-length subsequences [OK]
Hint: Always start LIS array values at 1 [OK]
Common Mistakes:
Initializing LIS array with zeros
Changing loop ranges incorrectly
Misunderstanding LIS condition comparison
5. Given the array arr = {3, 10, 2, 1, 20}, which of the following sequences is the Longest Increasing Subsequence (LIS)?
hard
A. [1, 2, 3]
B. [3, 10, 20]
C. [10, 20]
D. [3, 2, 1, 20]
Solution
Step 1: Identify increasing subsequences
Check each option for strictly increasing order:
- [3, 10, 20]: 3 < 10 < 20 (valid)
- [3, 2, 1, 20]: 3 > 2 > 1 (not increasing)
- [10, 20]: 10 < 20 (valid but shorter)
- [1, 2, 3]: 1 < 2 < 3 (valid but not a subsequence in original order)
Step 2: Compare lengths of valid subsequences
[3, 10, 20] length is 3, [10, 20] length is 2, [1, 2, 3] length is 3 but elements not in original order (subsequence must follow original order). [1, 2, 3] is invalid as subsequence because 1 comes after 3 in original array.
Final Answer:
[3, 10, 20] -> Option B
Quick Check:
LIS must be increasing and follow original order [OK]
Hint: Check subsequence order matches original array [OK]
Common Mistakes:
Choosing subsequence not in original order
Confusing increasing with non-decreasing
Ignoring subsequence length
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.