# Best Index | Hackerearth practice problem solution

Problem:-

You are given an array

$A$

of

$N$

elements. Now you need to choose the best index of this array

$A$

. 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

$i$

, you pick the first element that is

$A\left[i\right]$

and add it to your sum. Now you pick next two elements i.e.

$A\left[i+1\right]$

and

$A\left[i+2\right]$

and add both of them to your sum. Now you will pick the next

$3$

elements and this continues till the index for which it is possible to pick the elements. For example:

If our array contains

$10$

elements and you choose index to be

$3$

then your special sum is denoted by –

$\left(A\left[3\right]\right)+\left(A\left[4\right]+A\left[5\right]\right)+\left(A\left[6\right]+A\left[7\right]+A\left[8\right]\right)$

, beyond this its not possible to add further elements as the index value will cross the value

$10$

.

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

$N$

as input. Next line contains

$N$

space separated integers denoting the elements of the array

$A$

.
Output
In the output you have to print an integer that denotes the maximum special sum

Constraints

$1\le N\le {10}^{5}$

$-{10}^{7}\le A\left[i\right]\le {10}^{7}$

 Input Output 51 3 1 2 5 8 102 1 3 9 2 4 -10 -9 1 3 9

Sample Input
`6-3 2 3 -4 3 1`
Sample Output
`3`
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Let us calculate the special value for each index :

For index 1:

$\left(A\left[1\right]\right)+\left(A\left[2\right]+A\left[3\right]\right)+\left(A\left[4\right]+A\left[5\right]+A\left[6\right]\right)=2$

For index 2:

$\left(A\left[2\right]\right)+\left(A\left[3\right]+A\left[4\right]\right)=1$

For index 3:

$\left(A\left[3\right]\right)+\left(A\left[4\right]+A\left[5\right]\right)=2$

For index 4:

$\left(A\left[4\right]\right)+\left(A\left[5\right]+A\left[6\right]\right)=0$

For index 5:

$\left(A\left[5\right]\right)=3$

For index 6:

$\left(A\left[6\right]\right)=1$

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):-

#include<stdio.h>
#include<math.h>
main()
{
long n,i,k,j,left,p=0,max=-10000000;
scanf(“%ld”,&n);
long a[n],sum;
for(i=0;i<n;i++)
{
scanf(“%ld”,&a[i]);
if(i>0)
a[i]+=a[i1];
}
for(i=0;i<n;i++)
{
left=ni;
sum=0;
k=(-1+(int)sqrt((double)(8*left+1)))/2;
sum=a[(k*(k+1))/2+i1];
if(i!=0)
sum-=a[i1];
if(max<sum)
max=sum;
}
printf(“%ld”,max);
}

Solution 2 ( C++ language):-

This solution is based on the c++ language and you can submit ib c++14 and c++17 also.
In this solution first three lines of the main function is only for the decreasing the time of execution of the program..
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);

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 .

#include<bits/stdc++.h>
using namespace std;
int main()
{
long int i,j,n,m,limit=0;
cin>>n;
long long a[++n]={0},sum=0,max=-10000000;
for(i=1;i<n;i++)
{
cin>>a[i];
a[i]+=a[i1];
}
for(i=1;i<n;i++)
{
m=ni+1;limit=0;sum=0;
for(j=1;mj>0;j++)
{
limit+=j;
m=mj;
}
sum+=a[limit+i1]-a[i1];
if(sum>max)
max=sum;
}
cout<<max;
}

Recommended Post:-