Skip to content

Commit 322fa46

Browse files
divyakhetanMadhavBahl
authored andcommitted
added day18 (#174)
1 parent 07e3991 commit 322fa46

File tree

4 files changed

+226
-0
lines changed

4 files changed

+226
-0
lines changed

day18/C++/CheckSquareDay18.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* @author divyakhetan
3+
* @date 14/1/2019
4+
*/
5+
6+
#include<bits/stdc++.h>
7+
using namespace std;
8+
9+
int main(){
10+
int n;
11+
cin >> n;
12+
int a[n];
13+
int b[n];
14+
for(int i = 0; i < n; i++){
15+
cin >> a[i];
16+
17+
}
18+
19+
for(int i = 0; i < n; i++){
20+
cin >> b[i];
21+
22+
}
23+
int num;
24+
cin >> num;
25+
sort(a, a + n);
26+
sort(b, b + n);
27+
28+
29+
bool flag = true;
30+
for(int i = 0; i< n; i++){
31+
32+
if((int) pow(a[i], num) != b[i]){
33+
flag = false;
34+
break;
35+
}
36+
}
37+
if(flag)
38+
cout << "true" << endl;
39+
else cout << "false" << endl;
40+
return 0;
41+
}

day18/C++/FreqCounterDay18.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @author divyakhetan
3+
* @date 14/1/2019
4+
*/
5+
6+
#include<bits/stdc++.h>
7+
using namespace std;
8+
9+
int main(){
10+
int n;
11+
cin >> n;
12+
int a[n];
13+
map<int, int> m;
14+
for(int i = 0; i < n; i++){
15+
cin >> a[i];
16+
m[a[i]]++;
17+
}
18+
19+
map<int, int>::iterator it;
20+
for (it = m.begin(); it != m.end(); it++)
21+
{
22+
std::cout << it->first << " comes " << it->second << " times " << '\n';
23+
}
24+
25+
26+
return 0;
27+
}

day18/C++/UniqueElementsDay18.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @author divyakhetan
3+
* @date 14/1/2019
4+
*/
5+
6+
#include<bits/stdc++.h>
7+
using namespace std;
8+
9+
int main(){
10+
int n;
11+
cin >> n;
12+
int a[n];
13+
14+
for(int i = 0; i < n; i++){
15+
cin >> a[i];
16+
17+
}
18+
19+
//since sorted
20+
21+
int count = 1;
22+
int pos = a[0];
23+
for(int i = 1; i < n; i++){
24+
if(a[i] != pos){
25+
count++;
26+
pos = a[i];
27+
28+
}
29+
}
30+
31+
cout << count << endl;
32+
return 0;
33+
}

day18/README.md

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,40 @@ function printFrequency (freqObj) {
8181
freqCounter ([ 1, 2, 3, 1, 3, 4, 4, 4, 4, 2, 5]);
8282
```
8383

84+
### C++ Implementation
85+
86+
#### [Solution by @divyakhetan](./C++/FreqCounterDay18.cpp)
87+
88+
```cpp
89+
/**
90+
* @author divyakhetan
91+
* @date 14/1/2019
92+
*/
93+
94+
#include<bits/stdc++.h>
95+
using namespace std;
96+
97+
int main(){
98+
int n;
99+
cin >> n;
100+
int a[n];
101+
map<int, int> m;
102+
for(int i = 0; i < n; i++){
103+
cin >> a[i];
104+
m[a[i]]++;
105+
}
106+
107+
map<int, int>::iterator it;
108+
for (it = m.begin(); it != m.end(); it++)
109+
{
110+
std::cout << it->first << " comes " << it->second << " times " << '\n';
111+
}
112+
113+
114+
return 0;
115+
}
116+
```
117+
84118
## Java Implementation
85119

86120
### [Solution](./Java/Freq.java)
@@ -246,6 +280,48 @@ function countUniques (arr) {
246280
console.log (`Number of unique elements = ${countUniques([1, 1, 2, 2, 2, 3, 4, 4, 4, 4, 4, 5, 5, 5, 6, 7])}`);
247281
```
248282

283+
### C++ Implementation
284+
285+
#### [Solution by @divyakhetan](./C++/UniqueElementsDay18.cpp)
286+
287+
288+
289+
```cpp
290+
/**
291+
* @author divyakhetan
292+
* @date 14/1/2019
293+
*/
294+
295+
#include<bits/stdc++.h>
296+
using namespace std;
297+
298+
int main(){
299+
int n;
300+
cin >> n;
301+
int a[n];
302+
303+
for(int i = 0; i < n; i++){
304+
cin >> a[i];
305+
306+
}
307+
308+
//since sorted
309+
310+
int count = 1;
311+
int pos = a[0];
312+
for(int i = 1; i < n; i++){
313+
if(a[i] != pos){
314+
count++;
315+
pos = a[i];
316+
317+
}
318+
}
319+
320+
cout << count << endl;
321+
return 0;
322+
}
323+
```
324+
249325
## Java Implementation
250326

251327
### [Solution](./Java/Unique.java)
@@ -379,6 +455,55 @@ console.log (checkPowerN ([1, 2, 3, 4], [4, 9, 1, 16], 2));
379455
console.log (checkPowerN ([3, 4, 5, 2], [1, 2, 3], 4));
380456
```
381457

458+
### C++ Implementation
459+
460+
#### [Solution](./C++/CheckSquareDay18.cpp)
461+
462+
463+
```cpp
464+
/**
465+
* @author divyakhetan
466+
* @date 14/1/2019
467+
*/
468+
469+
#include<bits/stdc++.h>
470+
using namespace std;
471+
472+
int main(){
473+
int n;
474+
cin >> n;
475+
int a[n];
476+
int b[n];
477+
for(int i = 0; i < n; i++){
478+
cin >> a[i];
479+
480+
}
481+
482+
for(int i = 0; i < n; i++){
483+
cin >> b[i];
484+
485+
}
486+
int num;
487+
cin >> num;
488+
sort(a, a + n);
489+
sort(b, b + n);
490+
491+
492+
bool flag = true;
493+
for(int i = 0; i< n; i++){
494+
495+
if((int) pow(a[i], num) != b[i]){
496+
flag = false;
497+
break;
498+
}
499+
}
500+
if(flag)
501+
cout << "true" << endl;
502+
else cout << "false" << endl;
503+
return 0;
504+
}
505+
```
506+
382507
## Java Implementation
383508

384509
### [Solution](./Java/Power.java)

0 commit comments

Comments
 (0)