Integers - Maximum and Count
Integers - Maximum and Count
Problem:
N integers are passed as the input. The program must repeat the steps given below.
Step 1: Find the maximum M of the available integers and check if M is a single digit integer. If M is a single digit integer exit the program after printing M and the count of integers C present finally.
Step 2: If M is not a single digit integer then divide M into M/2 and M-M/2. Remove M from the list of integers and add M/2 and M-M/2 to the list of integers. Now repeat steps 1 and 2 until M is a single digit integer.
Boundary Condition(s): 2 <= N <= 10^5
Example Input/Output 1:
Input:
4
12 6 20 8
Output:
8 8
Explanation:
20 is the maximum. So the integers become 12 6 10 10 8. Now 12 is the maximum. So the integers become 6 6 6 10 10 8. Now 10 in the maximum. So the integers become 6 6 6 5 5 10 8. Again 10 in the maximum. So the integers become 6 6 6 5 5 5 5 8. Now 8 is the maximum and is a single digit integer. So 8 and 8 are printed. The second 8 denotes the count of integers present.
Example Input/Output 2:
Input: 6
4 5 9 7 8 2
Output:
9 6
Example Input/Output 3:
Input:
9
2 3 3 2 3 2 2 100 55
Output:
7 31
Program:
import java.util.*;
public class Hello {
public static void fun(int k,int [] a)
{
if(k < 10)
{
a[k]++;
}
else
{
fun(k/2,a);
fun(k-(k/2),a);
}
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int a [] = new int [10];
while(n-- > 0)
{
int k = sc.nextInt();
fun(k,a);
}
for(int i = 9;i>=0;i--)
{
if(a[i]!=0)
{
System.out.print(i+" ");
break;
}
}
int s = 0;
for(int i=0;i<=9;i++)
{
s+= a[i];
}
System.out.print(s);
}
}
Comments
Post a Comment