Dynamic Programming Series
Dynamic Programming Series
Day 3 :
Orac and Models
Problem Description
You are given n models in the shop numbered from 1 to n with sizes s1,s2......,sn.
An arrangement is good if some index j is divisible i and Sj > Si.
Problem Description
You are given n models in the shop numbered from 1 to n with sizes s1,s2......,sn.
An arrangement is good if some index j is divisible i and Sj > Si.
Note : An arrangement with one model is also considered as good
TASK : Find the maximum numbers of models he can buy?
Problem Description
Let us understand with the help of example..!!
Suppose we have an array of different sizes of models
Problem Description
Let us understand with the help of example..!!
Suppose we have an array of different sizes of models
| 3 | 6 | 7 | 7 | 7 | 7 |
|---|
1 2 3 4 5 6
Example 1:
Problem Description
Let us understand with the help of example..!!
Suppose we have an array of different sizes of models
| 3 | 6 | 7 | 7 | 7 | 7 |
|---|
1 2 3 4 5 6
Example 1:
Problem Description
Let us understand with the help of example..!!
Suppose we have an array of different sizes of models
| 3 | 6 | 7 | 7 | 7 | 7 |
|---|
1 2 3 4 5 6
Example 1:
Problem Description
Let us understand with the help of example..!!
Suppose we have an array of different sizes of models
| 3 | 6 | 7 | 7 | 7 | 7 |
|---|
1 2 3 4 5 6
Example 1:
Problem Description
Let us understand with the help of example..!!
Suppose we have an array of different sizes of models
| 3 | 6 | 7 | 7 | 7 | 7 |
|---|
1 2 3 4 5 6
Example 1:
Now next multiple of 4 is not available
Problem Description
Let us understand with the help of example..!!
Suppose we have an array of different sizes of models
| 3 | 6 | 7 | 7 | 7 | 7 |
|---|
1 2 3 4 5 6
Example 1:
Now in this combination..let us look at the values..!!
Problem Description
Let us understand with the help of example..!!
Suppose we have an array of different sizes of models
| 3 | 6 | 7 | 7 | 7 | 7 |
|---|
1 2 3 4 5 6
Example 1:
Now in this combination..let us look at the values..!!
3
6
7
Problem Description
Let us understand with the help of example..!!
Suppose we have an array of different sizes of models
| 3 | 6 | 7 | 7 | 7 | 7 |
|---|
1 2 3 4 5 6
Example 1:
The values are strictly increasing and hence we got one possible sequence..!!
3
6
7
Length = 3
Problem Description
Let us understand with the help of example..!!
Suppose we have an array of different sizes of models
| 3 | 6 | 7 | 7 | 7 | 7 |
|---|
1 2 3 4 5 6
Example 1:
3
6
7
This could be another possible sequence which is good..!!
Length = 3
Problem Description
Let us understand with the help of example..!!
Suppose we have an array of different sizes of models
| 3 | 6 | 7 | 7 | 7 | 7 |
|---|
1 2 3 4 5 6
Example 1:
3
7
7
As the value at index 6 is not strictly increasing.
Length = 2
Problem Description
Let us understand with the help of example..!!
Suppose we have an array of different sizes of models
| 3 | 6 | 7 | 7 | 7 | 7 |
|---|
1 2 3 4 5 6
Example 1:
3
7
This could be another possible sequence which is good..!!
Length = 2
Problem Description
Let us understand with the help of example..!!
Suppose we have an array of different sizes of models
| 3 | 6 | 7 | 7 | 7 | 7 |
|---|
1 2 3 4 5 6
Example 1:
3
7
This could be another possible sequence which is good..!!
Length = 2
Problem Description
Let us understand with the help of example..!!
Suppose we have an array of different sizes of models
| 3 | 6 | 7 | 7 | 7 | 7 |
|---|
1 2 3 4 5 6
Example 1:
3
7
This could be another possible sequence which is good..!!
Length = 2
Problem Description
Let us understand with the help of example..!!
Suppose we have an array of different sizes of models
| 3 | 6 | 7 | 7 | 7 | 7 |
|---|
1 2 3 4 5 6
Example 1:
The maximum length of good sequence is
3
So now we understood the problem..!!!
Now we will thonk how we can form a solution..!!

Can we relate the problem with sorting??
Can we relate the problem with sorting??
NO!!!!
Can we relate the problem with sorting??
NO!!!!
The reason is we cannot change the relative positions of the models..!!
Can we relate the problem with sorting??
NO!!!!
The reason is we cannot change the relative positions of the models..!!
So, what more can we think??
What if we know that how many previous indices are factors of the given indices??
What if we know that how many previous indices are factors of the given indices??
YES!!! WE ARE ONE STEP CLOSER!!
What if we know that how many previous indices are factors of the given indices??
YES!!! WE ARE ONE STEP CLOSER!!
But how can we find that??
What if we know that how many previous indices are factors of the given indices??
YES!!! WE ARE ONE STEP CLOSER!!
But how can we find that??
Ok so let us understand..!!
LOGIC FOR FINDING THE FACTORS..!!
Let us suppose we have to find factors of : 6
We have to see values only till
The values are 1 and 2.
So we will run a loop on these values and check..
The values are 1 and 2.
So we will run a loop on these values and check..
if n is divisible by the value.
if n/value == value
then value is a factor
else value, n/value both are factors..!!
Running this algorithm on the example we have.... i.e, 6
= 2
Running this algorithm on the example we have.... i.e, 6
= 2
| 1 | 2 |
|---|
Running this algorithm on the example we have.... i.e, 6
= 2
| 1 | 2 |
|---|
1,6 are the factors
Running this algorithm on the example we have.... i.e, 6
= 2
| 1 | 2 |
|---|
1,6 are the factors
2,3 are the factors
Running this algorithm on the example we have.... i.e, 6
= 2
| 1 | 2 |
|---|
1,6 are the factors
2,3 are the factors
So total factors are 1,2,3,and 6.
If you want more detailed explination
for finding the factors of a number then do comment in the comment section..!!
I will make seperate video content for that..!!
Now coming back to problem!!!!
Now coming back to problem!!!!
We can find the factors of a particular index and know from which indices we could get the answer
Now coming back to problem!!!!
We can find the factors of a particular index and know from which indices we could get the answer
Now applying concept of dynamic programming..!!
Now coming back to problem!!!!
We can find the factors of a particular index and know from which indices we could get the answer
Now applying concept of dynamic programming..!!
Suppose that we got the maximum length at the factors of a given number than we only need to find the maximum of all these values +1 and fill at the particular position..!!
Confused ??
Confused ??
Dont be !!! Let's see with example..!!
we have reached at n = 3
| 3 | 4 | 5 | 7 | 8 |
|---|
Suppose we take index i = 4
1 2 3 4 5
we have reached at n = 3
| 3 | 4 | 5 | 7 | 8 |
|---|
i = 4
1 2 3 4 5
i=1 and i=2 are factors
we have reached at n = 3
| 3 | 4 | 5 | 7 | 8 |
|---|
i = 4
1 2 3 4 5
i=1 and i=2 are factors
| 1 | 2 |
|---|
DP array
we have reached at n = 3
| 3 | 4 | 5 | 7 | 8 |
|---|
i = 4
1 2 3 4 5
i=1 and i=2 are factors
| 1 | 2 |
|---|
DP array
MAX (1,2) = 2
we have reached at n = 3
| 3 | 4 | 5 | 7 | 8 |
|---|
i = 4
1 2 3 4 5
i=1 and i=2 are factors
| 1 | 2 | 3 |
|---|
DP array
MAX (1,2) = 2
Now total length including ith position will be 2+1 = 3
i = 4
we have reached at n = 3
{
int t;
cin >> t;
while(t--)
{
int n;
cin >> n;
long long int* arr = new long long int[n+1]();
for(int i =1; i<=n; i++)
cin >> arr[i];
long long int* dp = new long long int[n+1]();
for(int i =1; i<=n; i++)
dp[i] = 1;
long long int ans = 1;
for(int i = 1; i<=n; i++)
{
for(int j = 1; j*j<=i; j++)
{
if(i%j == 0)
{
if(i/j == i && arr[i] > arr[j])
dp[i] = max(dp[i],dp[j]+1);
else
{
if(arr[i] > arr[j])
dp[i] = max(dp[j]+1,dp[i]);
if(arr[i] > arr[i/j])
dp[i] = max(dp[i/j]+1,dp[i]);
}
}
}
ans = max(ans,dp[i]);
}
cout << ans << endl;
}
return 0;
}we have reached at n = 3
Example!!!
| 1 | 4 | 2 | 3 | 6 | 4 | 9 |
|---|
| 1 | 1 | 1 | 1 | 1 | 1 | 1 |
|---|
1
6
5
4
2
7
3
For i = 1 No factors
we have reached at n = 3
Example!!!
| 1 | 4 | 2 | 3 | 6 | 4 | 9 |
|---|
| 1 | 1 | 1 | 1 | 1 | 1 | 1 |
|---|
1
6
5
4
2
7
3
For i = 2 , only factor is 1
we have reached at n = 3
Example!!!
| 1 | 4 | 2 | 3 | 6 | 4 | 9 |
|---|
| 1 | 1 | 1 | 1 | 1 | 1 | 1 |
|---|
1
6
5
4
2
7
3
For i = 2 , only factor is 1
Check if arr[1] < arr[2]
we have reached at n = 3
Example!!!
| 1 | 4 | 2 | 3 | 6 | 4 | 9 |
|---|
| 1 | 1 | 1 | 1 | 1 | 1 | 1 |
|---|
1
6
5
4
2
7
3
For i = 2 , only factor is 1
Check if arr[1] < arr[2] YESS
we have reached at n = 3
Example!!!
| 1 | 4 | 2 | 3 | 6 | 4 | 9 |
|---|
| 1 | 1 | 1 | 1 | 1 | 1 | 1 |
|---|
1
6
5
4
2
7
3
For i = 2 , only factor is 1
Check if arr[1] < arr[2] YESS
DP[2] = MAX(DP[2],DP[1]+1)
MAX(1,2) = 2
we have reached at n = 3
Example!!!
| 1 | 4 | 2 | 3 | 6 | 4 | 9 |
|---|
| 1 | 2 | 1 | 1 | 1 | 1 | 1 |
|---|
1
6
5
4
2
7
3
Similary for i = 3, factor is 1
Therefore DP[3] = MAX(1,2)
DP[3] = 2
we have reached at n = 3
Example!!!
| 1 | 4 | 2 | 3 | 6 | 4 | 9 |
|---|
| 1 | 2 | 2 | 1 | 1 | 1 | 1 |
|---|
1
6
5
4
2
7
3
Factors of 4 are 1,2.
Check arr[1] and arr[2] < arr[4]
Only arr[1] is less than arr[4]
DP[4] = MAX(DP[4], DP[1]+1) = 2
we have reached at n = 3
Example!!!
| 1 | 4 | 2 | 3 | 6 | 4 | 9 |
|---|
| 1 | 2 | 2 | 2 | 1 | 1 | 1 |
|---|
1
6
5
4
2
7
3
Similarly for 5 DP[5] = 2
we have reached at n = 3
Example!!!
| 1 | 4 | 2 | 3 | 6 | 4 | 9 |
|---|
| 1 | 2 | 2 | 2 | 2 | 1 | 1 |
|---|
1
6
5
4
2
7
3
Factors are 1,2,3
Only arr[1] and arr[3] is less than arr[6]
DP[6] = MAX(DP[6], DP[1]+1, DP[3]+1) = MAX(1,2,3) = 3
we have reached at n = 3
Example!!!
| 1 | 4 | 2 | 3 | 6 | 4 | 9 |
|---|
| 1 | 2 | 2 | 2 | 2 | 3 | 1 |
|---|
1
6
5
4
2
7
3
Now we can find for i = 7
DP[7] = 2 as 1 is only factor
we have reached at n = 3
Example!!!
| 1 | 4 | 2 | 3 | 6 | 4 | 9 |
|---|
| 1 | 2 | 2 | 2 | 2 | 3 | 2 |
|---|
1
6
5
4
2
7
3
We are getting max length for good sequence at i = 6
we have reached at n = 3
Example!!!
| 1 | 4 | 2 | 3 | 6 | 4 | 9 |
|---|
| 1 | 2 | 2 | 2 | 2 | 3 | 2 |
|---|
1
6
5
4
2
7
3
Answer = 3
So that's It for Day 3
Concepts that we have learnt
So that's It for Day 1
We have taken a very first step in Dynamic Programming.
Concepts that we have learnt
1. Decision making in dynamic programming.
So that's It for Day 1
We have taken a very first step in Dynamic Programming.
Concepts that we have learnt
1. Decision making in dynamic programming.
2. Using previous store results to calculate optimal present result..!!
Still, Have any Doubts???
Still, Have any Doubts???
Comment it You will get a reply.
OR
Send your doubt to email provided in description.
Copy of deck
By Chirayu Jain
Copy of deck
- 116