Problem:-
You are given an array of elements. Now you need to choose the best index of this array . An index of the array is called best if the special sum of this index is maximum across the special sum of all the other indices.
To calculate the special sum for any index , you pick the first element that is and add it to your sum. Now you pick next two elements i.e. and and add both of them to your sum. Now you will pick the next elements and this continues till the index for which it is possible to pick the elements. For example:
If our array contains elements and you choose index to be then your special sum is denoted by -
, beyond this its not possible to add further elements as the index value will cross the value .
Find the best index and in the output print its corresponding special sum. Note that there may be more than one best indices but you need to only print the maximum special sum.
Input
First line contains an integer as input. Next line contains space separated integers denoting the elements of the array .
Output
In the output you have to print an integer that denotes the maximum special sum
Constraints
Input | Output |
5 | 8 |
10 | 9 |
Let us calculate the special value for each index :
For index 1:
For index 2:
For index 3:
For index 4:
For index 5:
For index 6:
So the maximum value among all the special values is 3 hence it is the output.
Code:-
Here I am going to give you two solution first one is on the basis of C language and second one is on the basis of c++ language which you can submit in c++14 and c++17 also
Solution 1 ( C language):-
Solution 2 ( C++ language):-
In this solution first three lines of the main function is only for the decreasing the time of execution of the program..
This is your choice that you want to use this or not but in some cases the code may take more time in execution and that time we need it .
Recommended Post:-
- Hackerearth Problems:-Data structure:-
- Very Cool numbers | Hacker earth solution
- Birthday party | Hacker earth solution
- Most frequent | hacker earth problem solution
- program to find symetric difference of two sets
- cost of balloons | Hacker earth problem solution
- Chacha o chacha | hacker earth problem solution
- jadu and dna | hacker earth solution
- Bricks game | hacker earth problem
- Anti-Palindrome strings | hacker earth solution
- connected components in the graph | hacker earth data structure
- odd one out || hacker earth problem solution
- Minimum addition | Hackerearth Practice problem
- The magical mountain | Hackerearth Practice problem
- The first overtake | Hackerearth Practice problem
- Program to find cycle in the graph
- Implementation of singly link list
- Implementation of queue by using link list
- Algorithm of quick sort
- stack by using link list
- program to find preorder post order and inorder of the binary search tree
- Minimum weight of spanning tree
- Preorder, inorder and post order traversal of the tree
0 Comments