Lucy at the Film Festival: LucarnosFilm Festival is an annual film festival and is also known for being a prestigious platform for art house films. This time at the Lucarnos Film festival there are N movies screened, each of different genre ranging from drama movies to comedy ones and teen movies to horror ones. Lucy is a huge fan of movies and visited the film festival, but she’s not sure which movie she should watch.
Each movie can be characterized by two integers Li and Ri, denoting the length and the rating of the corresponding movie. Lucy wants to watch exactly one movie with the maximal value of Li × Ri. If there are several such movies, she would pick a one with the maximal Ri among them. If there is still a tie, she would pick the one with the minimal index among them.
Write a program to help Lucy pick a movie to watch at the film festival.
Input Format:
The first line of the input description contains an integer n. Assume that the maximum value for n as 50.
The second line of the input description contains n integers L1, L2, …,Ln.
The following line contains n integers R1, R2, …,Rn.
Output Format:
Output a single integer i denoting the index of the movie that Lucy should watch in the film festival. Note that you follow 1-based indexing.
Refer sample input and output for formatting specifications.
Sample Input 1:
2
1 2
2 1
Sample Output 1:
1
Sample Input 2:
4
2 1 4 1
2 4 1 4
Sample Output 2:
2
Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] lengths = new int[n];
int[] ratings = new int[n];
for (int i = 0; i < n; i++) {
lengths[i] = sc.nextInt();
}
for (int i = 0; i < n; i++) {
ratings[i] = sc.nextInt();
}
//sauarvhathi
int max = -1;
int index = -1;
for (int i = 0; i < n; i++) {
int temp = lengths[i] * ratings[i];
if (temp > max) {
max = temp;
}
}
// sauarvhathi
int max_rating = -1;
for (int i = 0; i < n; i++) {
if (max == lengths[i] * ratings[i]) {
if (ratings[i] > max_rating) {
max_rating = ratings[i];
index = i;
// sauarvhathi
}
}
}
if (index == -1) {
for (int i = 0; i < n; i++) {
if (max == lengths[i] * ratings[i]) {
index = i;
}
}
}
// sauarvhathi
System.out.println(index + 1);
}
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *a, *b;
int n, i, max, m, count = 0;
scanf("%d", &n);
a = (int *)malloc(n * sizeof(int));
//sauravhathi
b = (int *)malloc(n * sizeof(int));
for (i = 0; i < n; i++)
{
scanf("%d", a + i);
}
for (i = 0; i < n; i++)
{
scanf("%d", b + i);
}
i = 0;
max = *(a + i) * *(b + i);
for (i = 0; i < n; i++)
{
if (max < *(a + i) * *(b + i))
{
max = *(a + i) * *(b + i);
}
}
for (i = 0; i < n; i++)
{
if (max == *(a + i) * *(b + i))
{
count++;
}
}
if (count == 1)
{
for (i = 0; i < n; i++)
{
if (max == *(a + i) * *(b + i))
{
printf("%d", i + 1);
}
//sauravhathi
}
}
else
{
m = 1;
for (i = 0; i < n; i++)
{
if (m < *(b + i) && max == *(a + i) * *(b + i))
{
m = *(b + i);
}
}
for (i = 0; i < n; i++)
{
if (max == *(a + i) * *(b + i) && m == *(b + i))
{
printf("%d", i + 1);
break;
}
}
//sauravhathi
}
return 0;
}
n = int(input())
lengths = [int(i) for i in input().split()]
ratings = [int(i) for i in input().split()]
products = []
maximum = -1
for i in range(n):
temp = lengths[i]*ratings[i]
if temp > maximum:
maximum = temp
products.append(temp)
max_rating, index = -1, -1
for i in range(n):
if products[i] == maximum:
if ratings[i] > max_rating:
max_rating = ratings[i]
index = i
print(index+1)
Happy Learning – If you require any further information, feel free to contact me.