# Find Maximum Index Product

### Problem Statement :

```You are given a N list of  numbers . For each element at position  (), we define  and  as:
= closest index j such that j < i and . If no such j exists then  = 0.
= closest index k such that k > i and . If no such k exists then  = 0.

We define  =  * . You need to find out the maximum  among all i.

Input Format

The first line contains an integer , the number of integers. The next line contains the  integers describing the list a[1..N].

Constraints

1  <=  N  <=  10^5
1  <= ai  <=  10^9

Output Format

Output the maximum IndexProduct among all indices from 1 to N.```

### Solution :

```                            ```Solution in C :

In   C++  :

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int inp;
int sta;
int lef;
int rig;
int n;

void find(int *arr, int st, int ed, int d, int *out) {
int top = 1;
sta = 2000000000;
sta = 0;
for (int i=st; i!=ed; i+=d) {
while (sta[top-1] <= arr[i]) top--;
out[i] = sta[top-1];
sta[top] = arr[i];
sta[top++] = i+1;
}
return;
}
int main() {

scanf("%d", &n);
for (int i=0; i<n; i++)scanf("%d", &inp[i]);
find(inp, 0, n, 1, lef);
find(inp, n-1, -1, -1, rig);
long long ans = 0;
for (int i=0; i<n; i++) {
if ((long long)lef[i] * rig[i] > ans) ans = (long long)lef[i] * rig[i];
}
cout << ans << endl;
return 0;
}

In    Java  :

import java.awt.Point;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.util.ArrayList;

public class Solution {
public static void main(String[] args) throws IOException,
NumberFormatException {
BufferedOutputStream bos =
new BufferedOutputStream(System.out);
byte[] eolb = System.getProperty(
"line.separator").getBytes();

long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(values[i]);
}

long[] leftArr = new long[n];
ArrayList<Point> states = new ArrayList<Point>();
for (int i = 1; i < n; i++) {
for (int j = states.size() - 1; j >= 0; j--) {
if (states.get(j).getX() > arr[i]) {
leftArr[i] = (int) states.get(j).getY();
states.add(new Point((int) arr[i], i + 1));
break;
} else {
states.remove(j);
if (states.size() == 0) {
states.add(new Point((int) arr[i], i + 1));
break;
}
}
}
}
states.clear();

long[] rightArr = new long[n];
states.add(new Point((int) arr[n - 1], n));
for (int i = n - 2; i >= 0; i--) {
for (int j = states.size() - 1; j >= 0; j--) {
if (states.get(j).getX() > arr[i]) {
rightArr[i] = (int) states.get(j).getY();
states.add(new Point((int) arr[i], i + 1));
break;
} else {
states.remove(j);
if (states.size() == 0) {
states.add(new Point((int) arr[i], i + 1));
break;
}
}
}
}

long ans = -1;
for (int i = 0; i < n; i++) {
ans = Math.max(ans,
(long) leftArr[i] * rightArr[i]);
}
bos.write(String.valueOf(ans).getBytes());
bos.write(eolb);
bos.flush();

}
}

In   C  :

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

long int n, left=0, right=0, i, j, a, out, max=0;

scanf("%ld", &n);
for(i=0;i<n;i++)
{
scanf("%ld", &a[i]);
}
for(i=n-1;i>=1;i--)
{
/*    for(j=i-1;j>=0;j--)
{
if(a[j]>a[i])
{
left=j+1;
break;
}
}
for(j=i+1;j<=n-1;j++)
{
if(a[j]>a[i])
{
right=j+1;
break;
}
}*/
if(a[i]<a[i+1]&&a[i]<a[i-1])
{
max=i*(i+2);
break;
}

/*out=left*right;
if(out>max)
max=out;
left=0;
right=0;*/

}
printf("%ld", max);
return 0;
}

In   Python3  :

def lefts(L):
left = [];
maxes = [];
for i in range(len(L)):
while maxes and L[maxes[-1]] <= L[i]:
maxes.pop();
if not maxes: left.append(-1);
else: left.append(maxes[-1]);
maxes.append(i); #maxes = [i];
return left;

def rights(L):
right = [];
maxes = [];
for i in range(len(L)-1, -1, -1):
while maxes and L[maxes[-1]] <= L[i]:
maxes.pop();
if not maxes: right.append(-1);
else: right.append(maxes[-1]);
maxes.append(i); #maxes = [i];
right.reverse();
return right;

def indexProduct(L):
left = lefts(L);
right = rights(L);
products = ((left[i] + 1) * (right[i] + 1) for i in range(len(L)));
return max(products);

if __name__ == "__main__":
input();
L = [int(n) for n in input().split()];
print(indexProduct(L));```
```

## Self-Driving Bus

Treeland is a country with n cities and n - 1 roads. There is exactly one path between any two cities. The ruler of Treeland wants to implement a self-driving bus system and asks tree-loving Alex to plan the bus routes. Alex decides that each route must contain a subset of connected cities; a subset of cities is connected if the following two conditions are true: There is a path between ever

## Unique Colors

You are given an unrooted tree of n nodes numbered from 1 to n . Each node i has a color, ci. Let d( i , j ) be the number of different colors in the path between node i and node j. For each node i, calculate the value of sum, defined as follows: Your task is to print the value of sumi for each node 1 <= i <= n. Input Format The first line contains a single integer, n, denoti

## Fibonacci Numbers Tree

Shashank loves trees and math. He has a rooted tree, T , consisting of N nodes uniquely labeled with integers in the inclusive range [1 , N ]. The node labeled as 1 is the root node of tree , and each node in is associated with some positive integer value (all values are initially ). Let's define Fk as the Kth Fibonacci number. Shashank wants to perform 22 types of operations over his tree, T

## Pair Sums

Given an array, we define its value to be the value obtained by following these instructions: Write down all pairs of numbers from this array. Compute the product of each pair. Find the sum of all the products. For example, for a given array, for a given array [7,2 ,-1 ,2 ] Note that ( 7 , 2 ) is listed twice, one for each occurrence of 2. Given an array of integers, find the largest v

## Lazy White Falcon

White Falcon just solved the data structure problem below using heavy-light decomposition. Can you help her find a new solution that doesn't require implementing any fancy techniques? There are 2 types of query operations that can be performed on a tree: 1 u x: Assign x as the value of node u. 2 u v: Print the sum of the node values in the unique path from node u to node v. Given a tree wi

## Ticket to Ride

Simon received the board game Ticket to Ride as a birthday present. After playing it with his friends, he decides to come up with a strategy for the game. There are n cities on the map and n - 1 road plans. Each road plan consists of the following: Two cities which can be directly connected by a road. The length of the proposed road. The entire road plan is designed in such a way that if o