Approach for a DP problem..!!

                                 STEP 1

Approach for a DP problem..!!

                                 STEP 1 : OBSERVATIONS FROM THE PROBLEM

1. We cannot take three consecutive numbers.

Approach for a DP problem..!!

                                 STEP 1 : OBSERVATIONS FROM THE PROBLEM

1. We cannot take three consecutive numbers.

So how we should take the numbers ?

Approach for a DP problem..!!

                                 STEP 1 : OBSERVATIONS FROM THE PROBLEM

1. We cannot take three consecutive numbers.

So how we should take the numbers ?

Lets analyse with help of an example.

Approach for a DP problem..!!

                                 STEP 1 : OBSERVATIONS FROM THE PROBLEM

1. We cannot take three consecutive numbers.

5       7       3

Example 1 : 

Approach for a DP problem..!!

                                 STEP 1 : OBSERVATIONS FROM THE PROBLEM

1. We cannot take three consecutive numbers.

5       7       3

Example 1 : 

Sum = 5+7 = 12

Approach for a DP problem..!!

                                 STEP 1 : OBSERVATIONS FROM THE PROBLEM

1. We cannot take three consecutive numbers.

5       7       3

Example 1 : 

Sum = 7+3 = 10

Approach for a DP problem..!!

                                 STEP 1 : OBSERVATIONS FROM THE PROBLEM

1. We cannot take three consecutive numbers.

5       7       3

Example 1 : 

Sum = 5+3 = 8

Approach for a DP problem..!!

                                 STEP 1 : OBSERVATIONS FROM THE PROBLEM

1. We cannot take three consecutive numbers.

5       7       3

Example 1 : 

The maximum sum that we get is 12

Approach for a DP problem..!!

                                 STEP 1 : OBSERVATIONS FROM THE PROBLEM

1. We cannot take three consecutive numbers.

9         5       7       3

Example 2 : 

         9

Approach for a DP problem..!!

                                 STEP 1 : OBSERVATIONS FROM THE PROBLEM

1. We cannot take three consecutive numbers.

9         5       7       3

Example 2 : 

         9        14        

Approach for a DP problem..!!

                                 STEP 1 : OBSERVATIONS FROM THE PROBLEM

1. We cannot take three consecutive numbers.

9         5       7       3

Example 2 : 

         9        14        

Approach for a DP problem..!!

                                 STEP 1 : OBSERVATIONS FROM THE PROBLEM

1. We cannot take three consecutive numbers.

9         5       7       3

Example 2 : 

         9        14

max(9+7,5+7,9+5)

Approach for a DP problem..!!

                                 STEP 1 : OBSERVATIONS FROM THE PROBLEM

1. We cannot take three consecutive numbers.

9         5       7       3

Example 2 : 

         9        14        16         

max(9+7,5+7,9+5)

Approach for a DP problem..!!

                                 STEP 1 : OBSERVATIONS FROM THE PROBLEM

1. We cannot take three consecutive numbers.

9         5       7       3

Example 2 : 

         9        14        16         

max(pos2+3,pos1+7+3,pos3)

pos1

pos2

pos3

pos4

Approach for a DP problem..!!

                                 STEP 1 : OBSERVATIONS FROM THE PROBLEM

1. We cannot take three consecutive numbers.

9         5       7       3

Example 2 : 

         9        14        16         

max(17,19,16)

pos1

pos2

pos3

pos4

Approach for a DP problem..!!

                                 STEP 1 : OBSERVATIONS FROM THE PROBLEM

1. We cannot take three consecutive numbers.

9         5       7       3

Example 2 : 

         9        14        16         19

max(17,19,16)

pos1

pos2

pos3

pos4

Step2 : Building  DP formula

       7      3          5       8        9        2

For any position (i) in the array

       7      10          12                       

Step2 : Building  DP formula

       7      3          5       8        9        2

For any position (i) in the array

       7      10          12                       

Case 1 :

Ignore the given value and take the previous sum.

(12)

Step2 : Building  DP formula

       7      3          5       8        9        2

For any position (i) in the array

       7      10          12                       

Case 1 :

Ignore the given value and take the previous sum.

Case 2 :

Take the values i and (i-1) and sum(i-3)

(12, (5+8+7))

Step2 : Building  DP formula

       7      3          5       8        9        2

For any position (i) in the array

       7      10          12                       

Case 1 :

Ignore the given value and take the previous sum.

Case 2 :

Take the values i and (i-1) and sum(i-3)

(12,20)

Step2 : Building  DP formula

       7      3          5       8        9        2

For any position (i) in the array

       7      10          12                       

Case 1 :

Ignore the given value and take the previous sum.

Case 2 :

Take the values i and (i-1) and sum(i-3)

Case 3 :

Take the values i and sum(i-2)

(12,20,(10+8))

Step2 : Building  DP formula

       7      3          5       8        9        2

For any position (i) in the array

       7      10          12                       

Case 1 :

Ignore the given value and take the previous sum.

Case 2 :

Take the values i and (i-1) and sum(i-3)

Case 3 :

Take the values i and sum(i-2)

(12,20,18)

Step2 : Building  DP formula

       7      3          5       8        9        2

For any position (i) in the array

       7      10          12       20                

Case 1 :

Ignore the given value and take the previous sum.

Case 2 :

Take the values i and (i-1) and sum(i-3)

Case 3 :

Take the values i and sum(i-2)

(12,20,18)

Step2 : Building  DP formula

       7      3          5       8        9        2

For any position (i) in the array

       7      10          12       20        27       27 

Case 1 :

Ignore the given value and take the previous sum.

Case 2 :

Take the values i and (i-1) and sum(i-3)

Case 3 :

Take the values i and sum(i-2)

Step2 : Building  DP formula

       7      3          5       8        9        2

For any position (i) in the array

       7      10          12       20        27       27 

FORMULA

sum[i] = max(sum[i-1],arr[i]+sum[i-2],arr[i] + arr[i-1] + sum[i-3])

Step2 : Forming the code

#include <bits/stdc++.h>
 
using namespace std;
int fun(int* arr, int n) 
{ 
     
    int* sum = new int[n];
    //Base conditions
  
    //if n == 1 then the maximum sum is the only given element.
    if (n >= 1) 
        sum[0] = arr[0]; 
        
    //if n == 2 then the maximum sum is the sum of both the values. 
    if (n >= 2) 
        sum[1] = arr[0] + arr[1]; 
        
    //if n == 3 then the maximum sum is the max of both consecutive pairs.
    if (n >= 3) 
        sum[2] = max(sum[1], max(arr[1] + arr[2], arr[0] + arr[2])); 
  
    for (int i = 3; i < n; i++) 
        sum[i] = max(sum[i - 1], max(sum[i-2] + arr[i],arr[i] + arr[i-1] + sum[i-3])); 
    return sum[n - 1]; 
} 
int main()
{
	int n;
	cin >> n;
	int* arr = new int[n];
	for(int  i = 0; i<n; i++)
		cin >> arr[i];
	cout << fun(arr,n) << endl;


return 0;
}
Made with Slides.com