Search
 
SCRIPT & CODE EXAMPLE
 

CPP

Maximum sum of non consecutive elements

int sum(int *arr,int size) {
        int *sum = malloc(sizeof(int) * size);
        int i;

        for(i=0;i<size;i++) {
                if(i==0) {
                        sum[0] = arr[0];
                }else if(i==1) {
                        sum[1] = max(sum[0],arr[1]);
                }else{
                        sum[i] = max(sum[i-2]+arr[i],sum[i-1]);
                }
        }    
        return sum[size-1];
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: sum of n natural numbers 
Cpp :: vector iterator in c++ 
Cpp :: dangling pointer 
Cpp :: transpose matrix c++ vectors 
Cpp :: cin c++ 
Cpp :: Basic Makefile C++ 
Cpp :: array of charcter c++ 
Cpp :: fractional knapsack problem 
Cpp :: nth fibonacci number 
Cpp :: c++ unordered_map initialize new value 
Cpp :: Implicit conversion casting 
Cpp :: queue in cpp 
Cpp :: binary to decimal online converter 
Cpp :: how to compile c++ code with g+ 
Cpp :: bool nullable to bool c# 
Cpp :: how atan work c++ 
Cpp :: Array declaration by specifying the size and initializing elements in C++ 
Cpp :: two dimensional array A[N,M] with the random numbers from 10 to 90. 
Cpp :: new expression 
Cpp :: divisor summation 
Cpp :: initalising array c++ 
Cpp :: cpp module 42 
Cpp :: open url from dev cpp 
Cpp :: gcd multi num 
Cpp :: create dynamic variable c++ 
Cpp :: Qt asynchronous HTTP request 
Cpp :: how initilaize deffult value to c++ class 
Cpp :: convert java to c++ 
Cpp :: gcc compile multi thread 
Cpp :: new lien c++ 
ADD CONTENT
Topic
Content
Source link
Name
5+9 =