Question

In: Computer Science

C PROGRAMMING LANGUAGE Problem title : Bibi's Array Bibi also has an array containing N elements....

C PROGRAMMING LANGUAGE

Problem title : Bibi's Array

Bibi also has an array containing N elements. Like Lili, Bibi wants to know the highest frequency (most occurrences) and all elements which have that frequency.

Format Input

The first line contains an integer T stating the number of test cases. For each test case, the first line contains a single integer N which indicate the number of element in the array. The next line contains N integers Xi (1≤ i ≤ N ) which indicate i^th element in the array.

Format Output

Consist of T lines where each line has the format "Case #X:Y" , where X is the test case number starting at 1 and Y is the highest frequency. Next line contains all elements which have that frequency sorted in ascending order.

Constraints

¶ 1≤ T ≤ 20

¶ 2 ≤ N ≤ 20.000

¶ 1 ≤ Xi ≤ 2 x 10^5

Sample Input (standard input)

3

8

1 1 2 2 3 4 5 5

8

5 5 4 3 2 2 1 1

4

1 1 1 3

Sample Output (standard output)

Case #1: 2

1 2 5

Case #2: 2

1 2 5

Case #3: 3

1

Solutions

Expert Solution

C CODE:

#include <stdio.h>

int main()
{
int t,n,MaxCount=0;
  
scanf("%d",&t);
for(int x=0;x<t;x++)
{
  
scanf("%d",&n);
  
int arr[n], freq[n];
int i, j, count;

  

/* Input elements in array */
  
for(i=0; i<n; i++)
{
scanf("%d", &arr[i]);

/* Initially initialize frequencies to -1 */
freq[i] = -1;
}


for(i=0; i<n; i++)
{
count = 1;
for(j=i+1; j<n; j++)
{
/* If duplicate element is found */
if(arr[i]==arr[j])
{
count++;

/* Make sure not to count frequency of same element again */
freq[j] = 0;
}
}

/* If frequency of current element is not counted */
if(freq[i] != 0)
{
if(count>MaxCount)
{
MaxCount=count;
}
freq[i] = count;
}
}

/*
* Print frequency of each element
*/
int result[n],z=0,c=0;
printf("Case # %d:%d\n",x+1,MaxCount);
  
for(i=0; i<n; i++)
{
if(freq[i] ==MaxCount)
{
result[z++]=arr[i];
c++;
}
}
//sorting the result array
for(i=0; i<c; i++)
{
for(j=i+1; j<c; j++)
{
if(result[j] <result[i])
{
int tmp = result[i];
result[i] = result[j];
result[j] = tmp;
}
}
}
//printing Elements of array in sorted ascending order
for(i=0; i<c; i++)
{
printf("%d ", result[i]);
}
}

return 0;
}

OUTPUT SCREENSHOT:

I hope you find this solution helpful and please do UPVOTE if you like my effort , Thanks:)


Related Solutions

C PROGRAMMING LANGUAGE PROBLEM TITLE : ARRAY usually, if people want to input number into an...
C PROGRAMMING LANGUAGE PROBLEM TITLE : ARRAY usually, if people want to input number into an array, they will put it from index 0 until N - 1 using for. But, Bibi is bored to code like that. So, she didin't want to input the number that way. So Bibi challenged you to make a program that will read a sequence (represent index) that she made, then input the number to an array but input it with the same sequence...
C Programming Language (Code With C Programming Language) Problem Title : Which Pawn? Jojo is playing...
C Programming Language (Code With C Programming Language) Problem Title : Which Pawn? Jojo is playing chess himself to practice his abilities. The chess that Jojo played was N × N. When Jojo was practicing, Jojo suddenly saw a position on his chessboard that was so interesting that Jojo tried to put the pieces of Rook, Bishop and Knight in that position. Every time he put a piece, Jojo counts how many other pieces on the chessboard can be captured...
*C PROGRAMMING LANGUAGE* a) Given an array of size n, sort the array using pointers using...
*C PROGRAMMING LANGUAGE* a) Given an array of size n, sort the array using pointers using malloc or calloc. Examples: Input: n = 5, A= {33,21,2,55,4} Output: {2,4,21,33,55} b) Write a function that declares an array of 256 doubles and initializes each element in the array to have a value equal to half of the index of that element. That is, the value at index 0 should be 0.0, the value at index 1 should be 0.5, the value at...
Using the C Programming language, write a program that sums an array of 50 elements. Next,...
Using the C Programming language, write a program that sums an array of 50 elements. Next, optimize the code using loop unrolling. Loop unrolling is a program transformation that reduces the number of iterations for a loop by increasing the number of elements computed on each iteration. Generate a graph of performance improvement. Tip: Figure 5.17 in the textbook provides an example of a graph depicting performance improvements associated with loop unrolling. Marking:- Optimize the code for an array of...
C Programming Language Problem Title : Take Three Jojo just graduated and moved up to grade...
C Programming Language Problem Title : Take Three Jojo just graduated and moved up to grade 4. Today is his first day in 4th grade. Unfortunately, the lessons are held online because of the pandemic. So that the quality of learning remains good, Jojo's teacher gives a hard task for 4th grader. After the 4th graders finished their first task which is prime factorization. Jojo's teacher set up a game for the stundets. The game is very simple. Given N...
C Programming Language Problem Title : 4th Grade Jojo just graduated and moved up to grade...
C Programming Language Problem Title : 4th Grade Jojo just graduated and moved up to grade 4. Today is his first day in 4th grade. Unfortunately, the lessons are held online because of pandemic. So that the quality of learning remains good, Jojo’s teacher gives a hard task for 4th grader. The first task is to find the prime factorization of a number. Prime number is a natural number greater than 1 that is not a product of two smaller...
C Programming Language Problem Title : Museum Heist Jojo loves art. In his free time, he...
C Programming Language Problem Title : Museum Heist Jojo loves art. In his free time, he usually goes to the museum and admires the artwork there. since Jojo loves are so much, he is planning a heist at the museum. Jojo knows the price of every piece of art and doesn't want to raise suspicion, so he decided to steal the second most expensive art piece. It is guaranteed that there are at least two art pieces with different prices....
C Language - Programming Write a function that takes an array of ints, and the size...
C Language - Programming Write a function that takes an array of ints, and the size of the array – another int. It also returns a double. Call this one ‘average.’ Return a double that is the average of the values in the array. Demonstrate that it works by finding the average of an array with these values {78, 90, 56, 99, 88, 68, 92} Write a function that takes one double parameter, and returns a char. The parameter represents...
c++ Redo Programming Exercise 14 by first sorting the array before determining the array elements that...
c++ Redo Programming Exercise 14 by first sorting the array before determining the array elements that are the sum of two other elements. Use a selection sort algorithm, discussed in this chapter to sort the array. Instructions and code for Programming Exercise 14 have been included for your convenience. Exercise 14 Write a program that prompts the user to enter 50 integers and stores them in an array. The program then determines and outputs which numbers in the array are...
C Programming Language Title : Making wave In Physics, Mathematics, and related fields, a wave is...
C Programming Language Title : Making wave In Physics, Mathematics, and related fields, a wave is a disturbance of one of more fields such that the field values oscillate repeatedly about a stable equilibrium value. Waves are usually represented using mathematical functions of the form F (x, t), where x = position and t = time. Your task is to write a program that will visualize a given wave for exactly N seconds. You do not need to worry about...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT