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[i]

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

A[i+1]

 and 

A[i+2]

 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 –

(A[3])+(A[4]+A[5])+(A[6]+A[7]+A[8])

 , 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

1N105


107A[i]107

Sample Inputs

Input Output

5
1 3 1 2 5

8

10
2 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: 

(A[1])+(A[2]+A[3])+(A[4]+A[5]+A[6])=2

For index 2: 

(A[2])+(A[3]+A[4])=1

For index 3: 

(A[3])+(A[4]+A[5])=2

For index 4: 

(A[4])+(A[5]+A[6])=0

For index 5: 

(A[5])=3

For index 6: 

(A[6])=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:-



Leave a Reply

Your email address will not be published.