IQ Test | PDF - Free Printable
Educational worksheet: IQ Test | PDF. Download and print for classroom or home learning activities.
JPG
149×198
6.5 KB
Free · Personal Use
Quality Assured by Worksheets Library Team
Reviewed for educational accuracy and age-appropriateness
ID: #1726395
⭐
Show Answer Key & Explanations
Step-by-step solution for: IQ Test | PDF
▼
Show Answer Key & Explanations
Step-by-step solution for: IQ Test | PDF
Looking at the image, it appears to be a programming task involving arrays and sequences. The problem asks:
> “Given an array of integers, write a program that finds the longest increasing subsequence (LIS) in the array.”
The example given is:
Array: [10, 9, 2, 5, 3, 7, 101, 18]
Output: 4
Explanation: The LIS is [2, 3, 7, 101], so its length is 4.
This is a classic dynamic programming problem. Let me solve it step by step.
Step 1: Understand what we’re looking for
We need to find the longest sequence of numbers that are strictly increasing (each next number is bigger than the previous), but they don’t have to be next to each other in the original array.
Step 2: Think about how to build the solution
For each position in the array, we want to know: “What’s the longest increasing subsequence ending at this position?”
Let’s call this value `dp[i]` — the length of the longest increasing subsequence that ends at index i.
Step 3: How to calculate dp[i]?
For each element at index i, look at all elements before it (indices j < i). If arr[j] < arr[i], then we can extend the subsequence ending at j by adding arr[i]. So:
dp[i] = max(dp[j]) + 1 for all j < i where arr[j] < arr[i]
If no such j exists, then dp[i] = 1 (just the element itself).
Step 4: Apply this to the example
Array: [10, 9, 2, 5, 3, 7, 101, 18]
Initialize dp array with all 1s (since each element alone is a subsequence of length 1):
dp = [1, 1, 1, 1, 1, 1, 1, 1]
Now go through each index from left to right:
Index 0 (value 10): No previous elements → dp[0] = 1
Index 1 (value 9): Previous elements: 10 → 10 > 9, so can't extend → dp[1] = 1
Index 2 (value 2): Previous: 10, 9 → both > 2 → dp[2] = 1
Index 3 (value 5):
- Compare with 10: 10 > 5 → skip
- Compare with 9: 9 > 5 → skip
- Compare with 2: 2 < 5 → can extend! dp[2] = 1 → so dp[3] = 1 + 1 = 2
→ dp[3] = 2
Index 4 (value 3):
- 10 > 3 → skip
- 9 > 3 → skip
- 2 < 3 → extend dp[2]=1 → candidate: 2
- 5 > 3 → skip
→ dp[4] = 2
Index 5 (value 7):
- 10 > 7 → skip
- 9 > 7 → skip
- 2 < 7 → extend dp[2]=1 → candidate: 2
- 5 < 7 → extend dp[3]=2 → candidate: 3
- 3 < 7 → extend dp[4]=2 → candidate: 3
→ max is 3 → dp[5] = 3
Index 6 (value 101):
Check all previous:
- 10 < 101 → dp[0]=1 → candidate: 2
- 9 < 101 → dp[1]=1 → candidate: 2
- 2 < 101 → dp[2]=1 → candidate: 2
- 5 < 101 → dp[3]=2 → candidate: 3
- 3 < 101 → dp[4]=2 → candidate: 3
- 7 < 101 → dp[5]=3 → candidate: 4
→ max is 4 → dp[6] = 4
Index 7 (value 18):
Check all previous:
- 10 < 18 → dp[0]=1 → candidate: 2
- 9 < 18 → dp[1]=1 → candidate: 2
- 2 < 18 → dp[2]=1 → candidate: 2
- 5 < 18 → dp[3]=2 → candidate: 3
- 3 < 18 → dp[4]=2 → candidate: 3
- 7 < 18 → dp[5]=3 → candidate: 4
- 101 > 18 → skip
→ max is 4 → dp[7] = 4
Final dp array: [1, 1, 1, 2, 2, 3, 4, 4]
The maximum value in dp is 4 → which matches the expected output.
So the algorithm works correctly for the example.
Now, if we were to implement this in code, we’d use nested loops: outer loop for each element, inner loop for all previous elements.
Time complexity: O(n²), which is acceptable for small to medium arrays.
There’s also an O(n log n) version using binary search, but since the problem doesn’t specify performance requirements, the simpler O(n²) approach is fine.
Final Answer: 4
> “Given an array of integers, write a program that finds the longest increasing subsequence (LIS) in the array.”
The example given is:
Array: [10, 9, 2, 5, 3, 7, 101, 18]
Output: 4
Explanation: The LIS is [2, 3, 7, 101], so its length is 4.
This is a classic dynamic programming problem. Let me solve it step by step.
Step 1: Understand what we’re looking for
We need to find the longest sequence of numbers that are strictly increasing (each next number is bigger than the previous), but they don’t have to be next to each other in the original array.
Step 2: Think about how to build the solution
For each position in the array, we want to know: “What’s the longest increasing subsequence ending at this position?”
Let’s call this value `dp[i]` — the length of the longest increasing subsequence that ends at index i.
Step 3: How to calculate dp[i]?
For each element at index i, look at all elements before it (indices j < i). If arr[j] < arr[i], then we can extend the subsequence ending at j by adding arr[i]. So:
dp[i] = max(dp[j]) + 1 for all j < i where arr[j] < arr[i]
If no such j exists, then dp[i] = 1 (just the element itself).
Step 4: Apply this to the example
Array: [10, 9, 2, 5, 3, 7, 101, 18]
Initialize dp array with all 1s (since each element alone is a subsequence of length 1):
dp = [1, 1, 1, 1, 1, 1, 1, 1]
Now go through each index from left to right:
Index 0 (value 10): No previous elements → dp[0] = 1
Index 1 (value 9): Previous elements: 10 → 10 > 9, so can't extend → dp[1] = 1
Index 2 (value 2): Previous: 10, 9 → both > 2 → dp[2] = 1
Index 3 (value 5):
- Compare with 10: 10 > 5 → skip
- Compare with 9: 9 > 5 → skip
- Compare with 2: 2 < 5 → can extend! dp[2] = 1 → so dp[3] = 1 + 1 = 2
→ dp[3] = 2
Index 4 (value 3):
- 10 > 3 → skip
- 9 > 3 → skip
- 2 < 3 → extend dp[2]=1 → candidate: 2
- 5 > 3 → skip
→ dp[4] = 2
Index 5 (value 7):
- 10 > 7 → skip
- 9 > 7 → skip
- 2 < 7 → extend dp[2]=1 → candidate: 2
- 5 < 7 → extend dp[3]=2 → candidate: 3
- 3 < 7 → extend dp[4]=2 → candidate: 3
→ max is 3 → dp[5] = 3
Index 6 (value 101):
Check all previous:
- 10 < 101 → dp[0]=1 → candidate: 2
- 9 < 101 → dp[1]=1 → candidate: 2
- 2 < 101 → dp[2]=1 → candidate: 2
- 5 < 101 → dp[3]=2 → candidate: 3
- 3 < 101 → dp[4]=2 → candidate: 3
- 7 < 101 → dp[5]=3 → candidate: 4
→ max is 4 → dp[6] = 4
Index 7 (value 18):
Check all previous:
- 10 < 18 → dp[0]=1 → candidate: 2
- 9 < 18 → dp[1]=1 → candidate: 2
- 2 < 18 → dp[2]=1 → candidate: 2
- 5 < 18 → dp[3]=2 → candidate: 3
- 3 < 18 → dp[4]=2 → candidate: 3
- 7 < 18 → dp[5]=3 → candidate: 4
- 101 > 18 → skip
→ max is 4 → dp[7] = 4
Final dp array: [1, 1, 1, 2, 2, 3, 4, 4]
The maximum value in dp is 4 → which matches the expected output.
So the algorithm works correctly for the example.
Now, if we were to implement this in code, we’d use nested loops: outer loop for each element, inner loop for all previous elements.
Time complexity: O(n²), which is acceptable for small to medium arrays.
There’s also an O(n log n) version using binary search, but since the problem doesn’t specify performance requirements, the simpler O(n²) approach is fine.
Final Answer: 4
Parent Tip: Review the logic above to help your child master the concept of printable iq tests with answers.