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[100010];
int sta[100010][2];
int lef[100010];
int rig[100010];
int n;
void find(int *arr, int st, int ed, int d, int *out) {
int top = 1;
sta[0][0] = 2000000000;
sta[0][1] = 0;
for (int i=st; i!=ed; i+=d) {
while (sta[top-1][0] <= arr[i]) top--;
out[i] = sta[top-1][1];
sta[top][0] = arr[i];
sta[top++][1] = 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.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class Solution {
public static void main(String[] args) throws IOException,
NumberFormatException {
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
BufferedOutputStream bos =
new BufferedOutputStream(System.out);
byte[] eolb = System.getProperty(
"line.separator").getBytes();
int n = Integer.parseInt(br.readLine().trim());
String[] values = br.readLine().trim().split(" ");
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>();
states.add(new Point((int) arr[0], 1));
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[100000], 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));
View More Similar Problems
Components in a graph
There are 2 * N nodes in an undirected graph, and a number of edges connecting some nodes. In each edge, the first value will be between 1 and N, inclusive. The second node will be between N + 1 and , 2 * N inclusive. Given a list of edges, determine the size of the smallest and largest connected components that have or more nodes. A node can have any number of connections. The highest node valu
View Solution →Kundu and Tree
Kundu is true tree lover. Tree is a connected graph having N vertices and N-1 edges. Today when he got a tree, he colored each edge with one of either red(r) or black(b) color. He is interested in knowing how many triplets(a,b,c) of vertices are there , such that, there is atleast one edge having red color on all the three paths i.e. from vertex a to b, vertex b to c and vertex c to a . Note that
View Solution →Super Maximum Cost Queries
Victoria has a tree, T , consisting of N nodes numbered from 1 to N. Each edge from node Ui to Vi in tree T has an integer weight, Wi. Let's define the cost, C, of a path from some node X to some other node Y as the maximum weight ( W ) for any edge in the unique path from node X to Y node . Victoria wants your help processing Q queries on tree T, where each query contains 2 integers, L and
View Solution →Contacts
We're going to make our own Contacts application! The application must perform two types of operations: 1 . add name, where name is a string denoting a contact name. This must store name as a new contact in the application. find partial, where partial is a string denoting a partial name to search the application for. It must count the number of contacts starting partial with and print the co
View Solution →No Prefix Set
There is a given list of strings where each string contains only lowercase letters from a - j, inclusive. The set of strings is said to be a GOOD SET if no string is a prefix of another string. In this case, print GOOD SET. Otherwise, print BAD SET on the first line followed by the string being checked. Note If two strings are identical, they are prefixes of each other. Function Descriptio
View Solution →Cube Summation
You are given a 3-D Matrix in which each block contains 0 initially. The first block is defined by the coordinate (1,1,1) and the last block is defined by the coordinate (N,N,N). There are two types of queries. UPDATE x y z W updates the value of block (x,y,z) to W. QUERY x1 y1 z1 x2 y2 z2 calculates the sum of the value of blocks whose x coordinate is between x1 and x2 (inclusive), y coor
View Solution →