Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added day18 #174

Merged
merged 2 commits into from
Jan 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions day18/C++/CheckSquareDay18.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* @author divyakhetan
* @date 14/1/2019
*/

#include<bits/stdc++.h>
using namespace std;

int main(){
int n;
cin >> n;
int a[n];
int b[n];
for(int i = 0; i < n; i++){
cin >> a[i];

}

for(int i = 0; i < n; i++){
cin >> b[i];

}
int num;
cin >> num;
sort(a, a + n);
sort(b, b + n);


bool flag = true;
for(int i = 0; i< n; i++){

if((int) pow(a[i], num) != b[i]){
flag = false;
break;
}
}
if(flag)
cout << "true" << endl;
else cout << "false" << endl;
return 0;
}
27 changes: 27 additions & 0 deletions day18/C++/FreqCounterDay18.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* @author divyakhetan
* @date 14/1/2019
*/

#include<bits/stdc++.h>
using namespace std;

int main(){
int n;
cin >> n;
int a[n];
map<int, int> m;
for(int i = 0; i < n; i++){
cin >> a[i];
m[a[i]]++;
}

map<int, int>::iterator it;
for (it = m.begin(); it != m.end(); it++)
{
std::cout << it->first << " comes " << it->second << " times " << '\n';
}


return 0;
}
33 changes: 33 additions & 0 deletions day18/C++/UniqueElementsDay18.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @author divyakhetan
* @date 14/1/2019
*/

#include<bits/stdc++.h>
using namespace std;

int main(){
int n;
cin >> n;
int a[n];

for(int i = 0; i < n; i++){
cin >> a[i];

}

//since sorted

int count = 1;
int pos = a[0];
for(int i = 1; i < n; i++){
if(a[i] != pos){
count++;
pos = a[i];

}
}

cout << count << endl;
return 0;
}
125 changes: 125 additions & 0 deletions day18/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,40 @@ function printFrequency (freqObj) {
freqCounter ([ 1, 2, 3, 1, 3, 4, 4, 4, 4, 2, 5]);
```

### C++ Implementation

#### [Solution by @divyakhetan](./C++/FreqCounterDay18.cpp)

```cpp
/**
* @author divyakhetan
* @date 14/1/2019
*/

#include<bits/stdc++.h>
using namespace std;

int main(){
int n;
cin >> n;
int a[n];
map<int, int> m;
for(int i = 0; i < n; i++){
cin >> a[i];
m[a[i]]++;
}

map<int, int>::iterator it;
for (it = m.begin(); it != m.end(); it++)
{
std::cout << it->first << " comes " << it->second << " times " << '\n';
}


return 0;
}
```

## Java Implementation

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

### C++ Implementation

#### [Solution by @divyakhetan](./C++/UniqueElementsDay18.cpp)



```cpp
/**
* @author divyakhetan
* @date 14/1/2019
*/

#include<bits/stdc++.h>
using namespace std;

int main(){
int n;
cin >> n;
int a[n];

for(int i = 0; i < n; i++){
cin >> a[i];

}

//since sorted

int count = 1;
int pos = a[0];
for(int i = 1; i < n; i++){
if(a[i] != pos){
count++;
pos = a[i];

}
}

cout << count << endl;
return 0;
}
```

## Java Implementation

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

### C++ Implementation

#### [Solution](./C++/CheckSquareDay18.cpp)


```cpp
/**
* @author divyakhetan
* @date 14/1/2019
*/

#include<bits/stdc++.h>
using namespace std;

int main(){
int n;
cin >> n;
int a[n];
int b[n];
for(int i = 0; i < n; i++){
cin >> a[i];

}

for(int i = 0; i < n; i++){
cin >> b[i];

}
int num;
cin >> num;
sort(a, a + n);
sort(b, b + n);


bool flag = true;
for(int i = 0; i< n; i++){

if((int) pow(a[i], num) != b[i]){
flag = false;
break;
}
}
if(flag)
cout << "true" << endl;
else cout << "false" << endl;
return 0;
}
```

## Java Implementation

### [Solution](./Java/Power.java)
Expand Down