Skip to content

Commit 07e3991

Browse files
SpreehaMadhavBahl
authored andcommitted
day18 (#173)
1 parent 995dc51 commit 07e3991

File tree

4 files changed

+243
-0
lines changed

4 files changed

+243
-0
lines changed

Diff for: day18/Java/Freq.java

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package javaapplication3;
7+
8+
/**
9+
* @date 14/01/19
10+
* @author SPREEHA DUTTA
11+
*/
12+
import java.util.*;
13+
public class Freq {
14+
public static void main(String []args)
15+
{
16+
int n;int i;int t;int k=0;
17+
Scanner sc=new Scanner(System.in);
18+
System.out.println("Enter number of elements in the array ");
19+
n=sc.nextInt();
20+
int arr[]=new int[n];
21+
System.out.println("Enter elements of the array ");
22+
for(i=0;i<n;i++)
23+
arr[i]=sc.nextInt();
24+
Arrays.sort(arr);
25+
if(n!=1){
26+
if(arr[0]==arr[1])
27+
k=1;
28+
}
29+
else
30+
System.out.println("frequency of "+arr[0]+" is "+1);
31+
for(i=1;i<n;i++)
32+
{
33+
if(arr[i]==arr[i-1])
34+
k++;
35+
else
36+
{
37+
System.out.println("frequency of "+arr[i-1]+" is "+k);
38+
k=1;
39+
}
40+
}
41+
if(n!=1)
42+
System.out.println("frequency of "+arr[i-1]+" is "+k);
43+
}
44+
}

Diff for: day18/Java/Power.java

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package javaapplication3;
7+
8+
/**
9+
* @date 14/01/19
10+
* @author SPREEHA DUTTA
11+
*/
12+
import java.util.*;
13+
public class Power {
14+
public static void main(String []args)
15+
{
16+
Scanner sc=new Scanner(System.in);
17+
int n,p;int i,j;int k=0;
18+
System.out.println("Enter size of the array ");
19+
n=sc.nextInt();
20+
ArrayList<Integer> arr1 = new ArrayList<Integer>(n);
21+
ArrayList<Integer> arr2 = new ArrayList<Integer>(n);
22+
System.out.println("Enter elements of the first array ");
23+
for(i=0;i<n;i++)
24+
arr1.add(sc.nextInt());
25+
System.out.println("Enter elements of the second array ");
26+
for(i=0;i<n;i++)
27+
arr2.add(sc.nextInt());
28+
System.out.println("Enter power");
29+
p=sc.nextInt();
30+
for(i=0;i<n;i++)
31+
{
32+
int t=(int)(Math.pow(arr1.get(i),p));
33+
if(arr2.contains(t))
34+
k++;
35+
}
36+
if(k==n)
37+
System.out.println("true");
38+
else
39+
System.out.println("false");
40+
}
41+
}

Diff for: day18/Java/Unique.java

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package javaapplication3;
7+
8+
/**
9+
* @date 14/01/19
10+
* @author SPREEHA DUTTA
11+
*/
12+
import java.util.*;
13+
public class Unique {
14+
public static void countUniques(int arr[])
15+
{
16+
int k=1;
17+
for(int i=1;i<arr.length;i++)
18+
{
19+
if(arr[i]!=arr[i-1])
20+
k++;
21+
}
22+
System.out.println("Number of unique elements = "+k);
23+
}
24+
public static void main(String []args)
25+
{
26+
int n;int i;
27+
Scanner sc=new Scanner(System.in);
28+
System.out.println("Enter number of elements");
29+
n=sc.nextInt();
30+
int arr[]=new int[n];
31+
System.out.println("Enter elements of the array ");
32+
for(i=0;i<n;i++)
33+
arr[i]=sc.nextInt();
34+
countUniques(arr);
35+
}
36+
}

Diff for: day18/README.md

+122
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,50 @@ function printFrequency (freqObj) {
8181
freqCounter ([ 1, 2, 3, 1, 3, 4, 4, 4, 4, 2, 5]);
8282
```
8383

84+
## Java Implementation
85+
86+
### [Solution](./Java/Freq.java)
87+
88+
```java
89+
/**
90+
* @date 14/01/19
91+
* @author SPREEHA DUTTA
92+
*/
93+
import java.util.*;
94+
public class Freq {
95+
public static void main(String []args)
96+
{
97+
int n;int i;int t;int k=0;
98+
Scanner sc=new Scanner(System.in);
99+
System.out.println("Enter number of elements in the array ");
100+
n=sc.nextInt();
101+
int arr[]=new int[n];
102+
System.out.println("Enter elements of the array ");
103+
for(i=0;i<n;i++)
104+
arr[i]=sc.nextInt();
105+
Arrays.sort(arr);
106+
if(n!=1){
107+
if(arr[0]==arr[1])
108+
k=1;
109+
}
110+
else
111+
System.out.println("frequency of "+arr[0]+" is "+1);
112+
for(i=1;i<n;i++)
113+
{
114+
if(arr[i]==arr[i-1])
115+
k++;
116+
else
117+
{
118+
System.out.println("frequency of "+arr[i-1]+" is "+k);
119+
k=1;
120+
}
121+
}
122+
if(n!=1)
123+
System.out.println("frequency of "+arr[i-1]+" is "+k);
124+
}
125+
}
126+
```
127+
84128
### Python Implementation
85129

86130
#### [Solution](./Python/A_Frequency_Counter.py)
@@ -202,6 +246,42 @@ function countUniques (arr) {
202246
console.log (`Number of unique elements = ${countUniques([1, 1, 2, 2, 2, 3, 4, 4, 4, 4, 4, 5, 5, 5, 6, 7])}`);
203247
```
204248

249+
## Java Implementation
250+
251+
### [Solution](./Java/Unique.java)
252+
253+
```java
254+
/**
255+
* @date 14/01/19
256+
* @author SPREEHA DUTTA
257+
*/
258+
import java.util.*;
259+
public class Unique {
260+
public static void countUniques(int arr[])
261+
{
262+
int k=1;
263+
for(int i=1;i<arr.length;i++)
264+
{
265+
if(arr[i]!=arr[i-1])
266+
k++;
267+
}
268+
System.out.println("Number of unique elements = "+k);
269+
}
270+
public static void main(String []args)
271+
{
272+
int n;int i;
273+
Scanner sc=new Scanner(System.in);
274+
System.out.println("Enter number of elements");
275+
n=sc.nextInt();
276+
int arr[]=new int[n];
277+
System.out.println("Enter elements of the array ");
278+
for(i=0;i<n;i++)
279+
arr[i]=sc.nextInt();
280+
countUniques(arr);
281+
}
282+
}
283+
```
284+
205285
### Python Implementation
206286

207287
#### [Solution](./Python/B_Count_Uniques.py)
@@ -218,6 +298,7 @@ def countUniques(data):
218298
data = [1, 1, 2, 2, 2, 3, 4, 4, 4, 4, 4, 5, 5, 5, 6, 7]
219299
print("Number of unique elements =", countUniques(data))
220300
```
301+
221302
***
222303

223304
## Question 3 -- Check Power N
@@ -298,6 +379,47 @@ console.log (checkPowerN ([1, 2, 3, 4], [4, 9, 1, 16], 2));
298379
console.log (checkPowerN ([3, 4, 5, 2], [1, 2, 3], 4));
299380
```
300381

382+
## Java Implementation
383+
384+
### [Solution](./Java/Power.java)
385+
386+
```java
387+
/**
388+
* @date 14/01/19
389+
* @author SPREEHA DUTTA
390+
*/
391+
import java.util.*;
392+
public class Power {
393+
public static void main(String []args)
394+
{
395+
Scanner sc=new Scanner(System.in);
396+
int n,p;int i,j;int k=0;
397+
System.out.println("Enter size of the array ");
398+
n=sc.nextInt();
399+
ArrayList<Integer> arr1 = new ArrayList<Integer>(n);
400+
ArrayList<Integer> arr2 = new ArrayList<Integer>(n);
401+
System.out.println("Enter elements of the first array ");
402+
for(i=0;i<n;i++)
403+
arr1.add(sc.nextInt());
404+
System.out.println("Enter elements of the second array ");
405+
for(i=0;i<n;i++)
406+
arr2.add(sc.nextInt());
407+
System.out.println("Enter power");
408+
p=sc.nextInt();
409+
for(i=0;i<n;i++)
410+
{
411+
int t=(int)(Math.pow(arr1.get(i),p));
412+
if(arr2.contains(t))
413+
k++;
414+
}
415+
if(k==n)
416+
System.out.println("true");
417+
else
418+
System.out.println("false");
419+
}
420+
}
421+
```
422+
301423
### Python Implementation
302424

303425
#### [Solution](./Python/C_Check_Power_N.py)

0 commit comments

Comments
 (0)