Write a Program to print all the frequencies in a given an Array.Constraints:First line consists an integer representing the length of the array(n).Length of the array must be greater than zero or else Print Invalid Input.Second line consists all the Elements of an array.All the Elements in the array must be greaterthan zero or else print Invalid Input.Example:Input 1 : 6 1 2 2 3 3 3Output 1 : 1 - 1 2 - 2 3 - 3Input 1 : -6Output 1 : Invalid InputExplanation: NAPublic Test Cases:# INPUT EXPECTED OUTPUT1 61 2 2 3 3 31 - 12 - 23 - 32 -6Invalid Input
Question
Write a Program to print all the frequencies in a given an Array.Constraints:First line consists an integer representing the length of the array(n).Length of the array must be greater than zero or else Print Invalid Input.Second line consists all the Elements of an array.All the Elements in the array must be greaterthan zero or else print Invalid Input.Example:Input 1 : 6 1 2 2 3 3 3Output 1 : 1 - 1 2 - 2 3 - 3Input 1 : -6Output 1 : Invalid InputExplanation: NAPublic Test Cases:# INPUT EXPECTED OUTPUT1 61 2 2 3 3 31 - 12 - 23 - 32 -6Invalid Input
Solution
Here is a Python program that fulfills your requirements:
def print_frequencies():
n = int(input())
if n <= 0:
print("Invalid Input")
return
arr = list(map(int, input().split()))
if any(i <= 0 for i in arr):
print("Invalid Input")
return
freq = {}
for i in arr:
if i in freq:
freq[i] += 1
else:
freq[i] = 1
for key, value in freq.items():
print(f"{key} - {value}")
print_frequencies()
This program first reads the length of the array. If the length is not greater than zero, it prints "Invalid Input" and ends. Then it reads the elements of the array. If any element is not greater than zero, it prints "Invalid Input" and ends. Then it calculates the frequencies of the elements and prints them.
You can run this program and provide the input as specified in your question. For example:
6
1 2 2 3 3 3
The output will be:
1 - 1
2 - 2
3 - 3
Similar Questions
You are given an array of integers. Sort them by frequency. See examples for more clarifications.Input FormatThe first line of input contains T - the number of test cases. It's followed by 2T lines, the first line contains N - the size of the array. The second line contains the elements of the array.Output FormatFor each test case, print the elements of the array sorted by frequency. In case 2 elements have the same frequency, print the smaller element first.Constraints1 <= T <= 1001 <= N <= 10000-1000 <= A[i] <= 1000ExampleInput264 -2 10 12 -8 48176 -272 -272 -45 269 -327 -945 176Output-8 -2 10 12 4 4-945 -327 -45 269 -272 -272 176 176
You are given an array nums consisting of positive integers.Return the total frequencies of elements in nums such that those elements all have the maximum frequency.The frequency of an element is the number of occurrences of that element in the array. Example 1:Input: nums = [1,2,2,3,1,4]Output: 4Explanation: The elements 1 and 2 have a frequency of 2 which is the maximum frequency in the array.So the number of elements in the array with maximum frequency is 4
Read Array Size and ElementsWrite a Program to read the Array Size and Array Values from the User and Print Array Values?Constraints:Input :- First Line of Input Consists of One Integer Value (Array Size). Second Line of Input Consists of few Integer Values Separated by Spaces (Array Elements).Output :- Print the All Array Elements.Constraints :- NoExample:Input 1 : 8 86 210 33 73 53 93 848 48Output 1: 86 210 33 73 53 93 848 48 Input 2 : 6 1 2 3 4 5 6Output 2: 1 2 3 4 5 6 Input 3 : 5 5 10 88 2 5Output 3: 5 10 88 2 5
Create a list of integers from user input. Write the C program to find the element that occurs with the highest frequency and also find the second-highest number in the given elements.Sample Input:512234Sample Output23
Given an array, you have to find the frequency of a number x.Input FormatThe first line of input contains N - size of the array. The next line contains N integers, the elements of the array. The next line contains Q - number of queries. Each of the next Q lines contains a single integer X, for which you have to find the frequency of X in the given array.Output FormatFor each query, print the frequency of X, separated by a new line.Constraints20 points1 <= N <= 1051 <= Q <= 102-108 <= ar[i] <= 10830 points1 <= N <= 1051 <= Q <= 105-108 <= ar[i] <= 10850 points1 <= N <= 1051 <= Q <= 105-108 <= ar[i] <= 108ExampleInput9-6 10 -1 20 -1 15 5 -1 155-11081520Output31021
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.