Given an array arr[] of n positive integers which can contain integers from 1 to p where elements can be repeated or can be absent from the array.Your task is to count the frequency of all numbers from 1 to n. Do modify the array in-place changes in arr[], such that arr[i] = frequency(i) and assume 1-based indexing.Note: The elements greater than n in the array can be ignored for counting. ExamplesInput: n = 5 arr[] = {2, 3, 2, 3, 5} p = 5Output: 0 2 2 0 1Explanation: Counting frequencies of each array element We have: 1 occurring 0 times. 2 occurring 2 times. 3 occurring 2 times. 4 occurring 0 times. 5 occurring 1 time.Input: n = 4 arr[] = {3,3,3,3} p = 3Output: 0 0 4 0Explanation: Counting frequencies of each array element We have: 1 occurring 0 times. 2 occurring 0 times. 3 occurring 4 times. 4 occurring 0 times.Input: n = 2 arr[] = {8,9} p = 9Output: 0 0Explanation: Counting frequencies of each array element We have: 1 occurring 0 times. 2 occurring 0 times. Since here P=9, but there are no 9th Index present so can't count the value.Your Task:You don't need to read input or print anything. Complete the function frequencyCount() that takes the array arr, and integers n, and p as input parameters and modify the array in-place to denote the frequency count of each element from 1 to N.Expected time complexity: O(N)Expected auxiliay space: O(1)Constraints:1 ≤ n ≤ 1051 ≤ p ≤ 4*104 1 <= arr[i] <= p
Question
Given an array arr[] of n positive integers which can contain integers from 1 to p where elements can be repeated or can be absent from the array.Your task is to count the frequency of all numbers from 1 to n. Do modify the array in-place changes in arr[], such that arr[i] = frequency(i) and assume 1-based indexing.Note: The elements greater than n in the array can be ignored for counting. ExamplesInput: n = 5 arr[] = {2, 3, 2, 3, 5} p = 5Output: 0 2 2 0 1Explanation: Counting frequencies of each array element We have: 1 occurring 0 times. 2 occurring 2 times. 3 occurring 2 times. 4 occurring 0 times. 5 occurring 1 time.Input: n = 4 arr[] = {3,3,3,3} p = 3Output: 0 0 4 0Explanation: Counting frequencies of each array element We have: 1 occurring 0 times. 2 occurring 0 times. 3 occurring 4 times. 4 occurring 0 times.Input: n = 2 arr[] = {8,9} p = 9Output: 0 0Explanation: Counting frequencies of each array element We have: 1 occurring 0 times. 2 occurring 0 times. Since here P=9, but there are no 9th Index present so can't count the value.Your Task:You don't need to read input or print anything. Complete the function frequencyCount() that takes the array arr, and integers n, and p as input parameters and modify the array in-place to denote the frequency count of each element from 1 to N.Expected time complexity: O(N)Expected auxiliay space: O(1)Constraints:1 ≤ n ≤ 1051 ≤ p ≤ 4*104 1 <= arr[i] <= p
Solution
The problem is asking to count the frequency of all numbers from 1 to n in the given array. The array can contain integers from 1 to p and elements can be repeated or can be absent from the array. The task is to modify the array in-place such that arr[i] = frequency(i) and assume 1-based indexing. The elements greater than n in the array can be ignored for counting.
Here is a step by step solution:
-
Initialize a new array of size n+1 to all zeros. This will be used to store the frequency of each number.
-
Traverse the given array from start to end.
-
For every element in the array, if it is less than or equal to n, increment the count of that number in the new array.
-
Now, the new array contains the frequency of all numbers from 1 to n.
-
Finally, copy the new array to the original array.
Here is the Python code for the above steps:
def frequencyCount(arr, n, p):
freq = [0]*(n+1)
for i in range(n):
if arr[i] <= n:
freq[arr[i]] += 1
for i in range(1, n+1):
arr[i-1] = freq[i]
This function takes the array arr, and integers n, and p as input parameters and modify the array in-place to denote the frequency count of each element from 1 to N. The time complexity of this function is O(N) and it uses O(1) auxiliary space.
Similar Questions
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
Frequency of all NumbersWrite 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 Input
Given an array Arr of N positive integers. Your task is to find the elements whose value is equal to that of its index value ( Consider 1-based indexing ).
Problem Statement You are given a function, static int ElementsAndIndices(int[] arr){} The function takes an integer array 'arr' of size 'n' as its arguments. Implement the function to find and return the number of array elements which are equal to their index value in array i.e. arr[k] = k, 0 <= k < n. Note: Indexing starts from 0. Return -1 if 'arr' is empty or None in case of python Example: Input: 10 1 12 3 5 8 9 7 12 23 Output: 3 Explanation: Index Element 0 10 1 1 2 12 3 3 4 5 5 8 6 9 7 7 8 12 9 23 Elements at index {1, 3, 7} are equal to their index values {1, 3, 7} respectively. Since, these are 3 elements, thus, output is 3. The custom input format for the above case: 10 10 1 12 3 5 8 9 7 12 23 (The first line represents the size of the array, the second line represents the elements of the array) Sample input -3 0 1 3 5 7 Sample Output 1 The custom input format for the above case: 6 -3 0 1 3 5 7 (The first line represents the size of the array, the second line represents the elements of the array) Instructions : This is a template based question, DO NOT write the "main" function. Your code is judged by an automated system, do not write any additional welcome/greeting messages. "Save and Test" only checks for basic test cases, more rigorous cases will be used to judge your code while scoring. Additional score will be given for writing optimized code both in terms of memory and execution time. Now let's start coding : Language: Java 17.0 Day Mode Read-only code below . . . 1 class SolutionClass { 2 public static void main(String[] args) throws java.lang.Exception { 3 //Input read from STDIN 4 int result = ElementsAndIndices(arr); 5 //Value in result printed to STDOUT 6 } 7 } 8 Write your code below . . . 9 static int ElementsAndIndices(int[] arr) throws java.lang.Exception 10 { 11 /* Write your code here. */ 12 } 13 14 15
Given a fixed-length integer array arr, duplicate each occurrence of zero, shifting the remaining elements to the right.
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.