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 4 Solutions #28

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
60 changes: 60 additions & 0 deletions Algorithms/Bit Manipulation/Sum vs XOR.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <bits/stdc++.h>

using namespace std;

string ltrim(const string &);
string rtrim(const string &);

// Complete the sumXor function below.
long sumXor(long n) {
int ctr=0;
while(n)
{
if(n%2==0)
{
ctr++;
}
n/=2;
}
return pow(2,ctr);
}

int main()
{
ofstream fout(getenv("OUTPUT_PATH"));

string n_temp;
getline(cin, n_temp);

long n = stol(ltrim(rtrim(n_temp)));

long result = sumXor(n);

fout << result << "\n";

fout.close();

return 0;
}

string ltrim(const string &str) {
string s(str);

s.erase(
s.begin(),
find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
);

return s;
}

string rtrim(const string &str) {
string s(str);

s.erase(
find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),
s.end()
);

return s;
}
25 changes: 25 additions & 0 deletions Algorithms/Implementation/Climbing_the_Leaderboard.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <ios>
#include <iostream>
#include <vector>
#include <algorithm>

std::vector<int> vec;

int main()
{
int n, q, v;
std::cin >> n;
for (int i = 0; i < n; i++)
{
std::cin >> v;
vec.push_back(v);
}
std::sort(vec.begin(), vec.end());
vec.resize(std::distance(vec.begin(), std::unique(vec.begin(), vec.end())));
std::cin >> q;
while (q--)
{
std::cin >> v;
std::cout << std::distance(std::upper_bound(vec.begin(), vec.end(), v), vec.end())+1 << '\n';
}
}
31 changes: 31 additions & 0 deletions Algorithms/Implementation/Encryption.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <bits/stdc++.h>

using namespace std;

// Complete the encryption function below.
void encryption(string s) {
int l=s.size();
int row=round(sqrt(l));
int column;
if(row>=sqrt(l))
column=row;
else column=row+1;
for(int j=0;j<column;++j)
{
for(int i=j; i<l;i+=column)
{
cout << s[i];
}
cout << ' ';
}

}

int main()
{
string s;
getline(cin, s);
encryption(s);
return 0;
}