Cognizant GenC Elevate Sample Coding Question 5
Question 5
In this article, we will discuss about Coding Question with their solution in java. In this Question , we have to find maximum number of bulbs jasleen can collect , starting from any machine and collecting from every consecutive machine until shre reaches the last machine she wants to collect from.
Question 5
Jasleen has bought a new bulb factory. The factory has a single file of machines, numbered from 1 to N. Each machine has a certain number of fully prepared bulbs.
Jasleen has a rule she wants to follow. She wants to collect an equal amount of bulb from
each machine from which she collects bulbs.
Jasleen can start collecting bulb from any machine, but once she starts collecting, she collects
from every consecutive machine until she reaches the last machine she wants to collect from. Find the maximum number of bulbs she can collect.
Input Specification:
Input1: N, the number of machines
Input2: An array of N elements (a1,a2 a3……aN], denoting the number of fully prepared bulbs in each machine of the factory.
Output Specification:
An integer output denoting the maximum number of bulbs that Allie can collect.
Example 1:
input1: 3
Input2: [1,2,3]
Output: 3
Example 2:
input1: 4
Input2: [5,8,9,10]
Output: 20
import java.util.*;
public class Main
{
public static void main (String[]args)
{
Scanner sc = new Scanner (System.in);
int n = sc.nextInt ();
int a[] = new int[n];
for (int i = 0; i < n; i++)
a[i] = sc.nextInt ();
Arrays.sort (a);
ArrayList < Integer > a1 = new ArrayList <> ();
for (int i = a.length - 1; i >= 0; i--)
{
int x = 0;
x = a[i] * (a.length - i);
a1.add (x);
}
for (int i = 0; i < a1.size (); i++)
{
if (a1.get (a1.size () - 1 - i) >= a1.get (i))
{
System.out.println (a1.get (a1.size () - 1 - i));
break;
}
}
}
}