Skip to content

Commit c9ebe34

Browse files
committed
🚀 09-Jul-2020
1 parent 7195822 commit c9ebe34

File tree

7 files changed

+312
-0
lines changed

7 files changed

+312
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
Given a number N, find all factors of N.
2+
3+
Example:
4+
5+
N = 6
6+
factors = {1, 2, 3, 6}
7+
Make sure the returned array is sorted
8+
9+
10+
11+
12+
13+
14+
15+
16+
vector<int> Solution::allFactors(int n) {
17+
vector<int> res, tmp;
18+
if(n==0){
19+
res.push_back(0);
20+
return res;
21+
}
22+
for(int i=1;i*i<=n;i++){
23+
if(n%i==0){
24+
res.push_back(i);
25+
if((i*i)!=n) tmp.push_back(n/i);
26+
}
27+
}
28+
int k=tmp.size();
29+
for(int i=k-1;i>=0;i--)
30+
res.push_back(tmp[i]);
31+
return res;
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Given a number N >= 0, find its representation in binary.
2+
3+
Example:
4+
5+
if N = 6,
6+
7+
binary form = 110
8+
9+
10+
11+
12+
13+
14+
15+
string Solution::findDigitsInBinary(int n) {
16+
string res;
17+
if(n==0) res.push_back(0+'0');
18+
while(n){
19+
int tmp=n%2;
20+
n/=2;
21+
res.push_back(tmp+'0');
22+
}
23+
reverse(res.begin(), res.end());
24+
return res;
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
Problem Description
2+
3+
Hamming distance between two non-negative integers is defined as the number of positions at which the corresponding bits are different.
4+
5+
Given an array A of N non-negative integers, find the sum of hamming distances of all pairs of integers in the array. Return the answer modulo 1000000007.
6+
7+
8+
9+
Problem Constraints
10+
1 <= |A| <= 200000
11+
12+
1 <= A[i] <= 109
13+
14+
15+
16+
Input Format
17+
First and only argument is array A.
18+
19+
20+
21+
Output Format
22+
Return one integer, the answer to the problem.
23+
24+
25+
26+
Example Input
27+
Input 1:
28+
29+
A = [1]
30+
Input 2:
31+
32+
A = [2, 4, 6]
33+
34+
35+
Example Output
36+
Output 1:
37+
38+
0
39+
Output 2:
40+
41+
8
42+
43+
44+
Example Explanation
45+
Explanation 1:
46+
47+
No pairs are formed.
48+
Explanation 2:
49+
50+
We return, f(2, 2) + f(2, 4) + f(2, 6) + f(4, 2) + f(4, 4) + f(4, 6) + f(6, 2) + f(6, 4) + f(6, 6) = 8
51+
52+
53+
54+
55+
56+
57+
58+
59+
60+
// O(n^2)
61+
int Solution::hammingDistance(const vector<int> &a) {
62+
int n=a.size();
63+
int cnt=0;
64+
for(int i=0;i<n;i++){
65+
for(int j=0;j<n;j++){
66+
int x=a[i];
67+
int y=a[j];
68+
int tmp=x^y;
69+
while(tmp>0){
70+
cnt+=tmp & 1;
71+
tmp=tmp>>1;
72+
}
73+
}
74+
75+
}
76+
return cnt;
77+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
Given a number N, find all prime numbers upto N ( N included ).
2+
3+
Example:
4+
5+
if N = 7,
6+
7+
all primes till 7 = {2, 3, 5, 7}
8+
9+
Make sure the returned array is sorted.
10+
11+
12+
13+
14+
15+
16+
17+
18+
19+
vector<int> Solution::sieve(int n) {
20+
vector<int> res;
21+
vector<bool> prime(n+1, true);
22+
prime[0]=false;
23+
prime[1]=false;
24+
int i=2*2;
25+
while(i<=n){
26+
prime[i]=false; // multiples of 2
27+
i+=2;
28+
}
29+
for(i=3;i*i<n;i+=2){
30+
int tmp=i*2;
31+
if(prime[i]){
32+
while(tmp<=n){
33+
prime[tmp]=false;
34+
tmp+=i;
35+
}
36+
}
37+
}
38+
for(int i=2;i<=n;i++){
39+
if(prime[i]) res.push_back(i);
40+
}
41+
return res;
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
Given an even number ( greater than 2 ), return two prime numbers whose sum will be equal to given number.
2+
3+
NOTE A solution will always exist. read Goldbach’s conjecture
4+
5+
Example:
6+
7+
8+
Input : 4
9+
Output: 2 + 2 = 4
10+
11+
If there are more than one solutions possible, return the lexicographically smaller solution.
12+
13+
If [a, b] is one solution with a <= b,
14+
and [c,d] is another solution with c <= d, then
15+
16+
[a, b] < [c, d]
17+
18+
If a < c OR a==c AND b < d.
19+
20+
21+
22+
23+
24+
25+
26+
27+
28+
29+
vector<int> Solution::primesum(int n) {
30+
vector<int> ret;
31+
vector<bool> seive(n+1, true);
32+
seive[0]=false;
33+
seive[1]=false;
34+
for(int i=2;i*i<=n;i++){
35+
int tmp=i*2;
36+
if(seive[i]){
37+
while(tmp<=n){
38+
seive[tmp]=false;
39+
tmp+=i;
40+
}
41+
}
42+
}
43+
for(int i=0;i<n;i++){
44+
if(seive[i] && seive[n-i]){
45+
ret.push_back(i);
46+
ret.push_back(n-i);
47+
break;
48+
}
49+
}
50+
return ret;
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Given a number N, verify if N is prime or not.
2+
3+
Return 1 if N is prime, else return 0.
4+
5+
Example :
6+
7+
Input : 7
8+
Output : True
9+
10+
11+
12+
13+
14+
15+
16+
int Solution::isPrime(int n) {
17+
if(n<=1) return 0;
18+
if(n<=3) return 1;
19+
if(n%2==0 || n%3==0) return 0;
20+
for(int i=5;i*i<n;i+=6)
21+
if(n%i==0 || n%(i+2)==0) return 0;
22+
return 1;
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
2+
3+
Note:
4+
5+
The solution set must not contain duplicate triplets.
6+
7+
Example:
8+
9+
Given array nums = [-1, 0, 1, 2, -1, -4],
10+
11+
A solution set is:
12+
[
13+
[-1, 0, 1],
14+
[-1, -1, 2]
15+
]
16+
Hide Hint #1
17+
So, we essentially need to find three numbers x, y, and z such that they add up to the given value. If we fix one of the numbers say x, we are left with the two-sum problem at hand!
18+
Hide Hint #2
19+
For the two-sum problem, if we fix one of the numbers, say
20+
x
21+
, we have to scan the entire array to find the next number
22+
y
23+
which is
24+
value - x
25+
where value is the input parameter. Can we change our array somehow so that this search becomes faster?
26+
Hide Hint #3
27+
The second train of thought for two-sum is, without changing the array, can we use additional space somehow? Like maybe a hash map to speed up the search?
28+
29+
30+
31+
32+
33+
34+
35+
class Solution {
36+
public:
37+
vector<vector<int>> threeSum(vector<int>& nums) {
38+
vector<vector<int> > res;
39+
int n=nums.size();
40+
if(n<3) return res;
41+
42+
sort(nums.begin(), nums.end());
43+
44+
for(int i=0;i<n-2;i++){
45+
if(i==0 || (i>0 && nums[i]!=nums[i-1])){ // not taking duplicates
46+
int sum=0-nums[i];
47+
int low=i+1, high=n-1;
48+
while(low<high){
49+
if(nums[low]+nums[high]==sum){
50+
res.push_back({nums[i], nums[low], nums[high]});
51+
while(low<high && nums[low]==nums[low+1]) low++;
52+
while(low<high && nums[high]==nums[high-1]) high--;
53+
low++; high--;
54+
} else if(nums[low]+nums[high]>sum) high--;
55+
else low++;
56+
}
57+
58+
}
59+
}
60+
return res;
61+
}
62+
};

0 commit comments

Comments
 (0)