-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrequenciesofLimitedRangeArrayElements.java
More file actions
68 lines (60 loc) · 1.58 KB
/
FrequenciesofLimitedRangeArrayElements.java
File metadata and controls
68 lines (60 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
* Given an array A[] 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 elements from 1 to N.
Note: The elements greater than N in the array can be ignored for counting.
*/
public class FrequenciesofLimitedRangeArrayElements
{
public static void printArray(int[] arr ,int n)
{
for(int j=0;j<n;j++)
{
System.out.print(arr[j]+" ");
}
System.out.println("");
}
public static int[] frequencyCount(int arr[], int N, int P)
{
int k=0;
int[] temp=new int[N];
for(int i=1;i<=P;i++)
{
int count=0;
for(int j=0;j<N;j++)
{
if(arr[j]==i)
{
count++;
}
}
temp[k++]=count;
}
return temp;
// code here
}
public static void main(String[] args)
{
int[] a={2, 3, 2, 3, 5};
int n=a.length;
printArray(a,n);
int[] ans=frequencyCount(a,n,5);
printArray(ans,ans.length);
}
//TC:O(N*N)
//SP:O(N)
/*
Input:
N = 5
arr[] = {2, 3, 2, 3, 5}
P = 5
Output:
0 2 2 0 1
Explanation:
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.
*/
}