Skip to content

Commit c8276e8

Browse files
Add files via upload
1 parent 6b3364a commit c8276e8

File tree

5 files changed

+256
-0
lines changed

5 files changed

+256
-0
lines changed

Base7.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
Given an integer num, return a string of its base 7 representation.
3+
4+
Example 1:
5+
Input: num = 100
6+
Output: "202"
7+
8+
Example 2:
9+
Input: num = -7
10+
Output: "-10"
11+
*/
12+
13+
class Solution {
14+
public:
15+
string s="";
16+
string convertToBase7(int num, bool flag=0) {
17+
if(num==0){
18+
if(s.size()==0) return "0";
19+
else reverse(s.begin(),s.end());
20+
return flag ? "-"+s : s;
21+
}
22+
if(num<0) flag=1;
23+
s.push_back(abs(num)%7+'0');
24+
return convertToBase7(abs(num)/7,flag);
25+
}
26+
};

BaseConversion.cpp

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
Given four numbers convert:
3+
4+
a, a decimal number to the binary equivalent
5+
b, a binary to decimal equivalent
6+
c, decimal to hexadecimal equivalent
7+
d, hexadecimal to decimal equivalent
8+
9+
Example 1:
10+
Input :
11+
a = 6
12+
b = 110
13+
c = 20
14+
d = 2A
15+
Output:
16+
110, 6, 14, 42
17+
Explanation:
18+
(6)10 = (110)2
19+
(110)2 = (6)10
20+
(20)10 = (14)16
21+
(2A)16 = (42)10
22+
23+
Example 2:
24+
Input:
25+
a = 10
26+
b = 111
27+
c = 25
28+
d = FA
29+
Output:
30+
1010, 7, 19, 250
31+
Explanation:
32+
(10)10 = (1010)2
33+
(111)2 = (7)10
34+
(25)10 = (19)16
35+
(FA)16 = (250)10
36+
37+
Your Task:
38+
You don't need to read input or print anything. Your task is to complete the function convert() which takes three integers a, b, c, and string d as input parameters and returns the converted numbers as an array of four strings.
39+
40+
Expected Time Complexity: O(log(max(a,b,c)) + |d|)
41+
Expected Auxiliary Space: O(1)
42+
*/
43+
44+
class Solution{
45+
public:
46+
47+
vector<string> convert(int a,int b,int c,string d)
48+
{
49+
vector<string> result;
50+
string res="";
51+
while(a!=0){
52+
int rem=a%2;
53+
res+=to_string(rem);
54+
a/=2;
55+
}
56+
reverse(res.begin(),res.end());
57+
result.push_back(res);
58+
59+
int i=1, res2=0;
60+
while(b!=0){
61+
int rem=b%10;
62+
res2=res2+rem*i;
63+
i*=2;
64+
b/=10;
65+
}
66+
result.push_back(to_string(res2));
67+
68+
string res3="";
69+
while(c!=0){
70+
int rem=c%16;
71+
if(rem<10){
72+
res3+=to_string(rem);
73+
}else{
74+
res3+=(char)('A'+(c%16)-10);
75+
}
76+
c/=16;
77+
}
78+
reverse(res3.begin(),res3.end());
79+
result.push_back(res3);
80+
81+
int res4=0, base=1;
82+
for(int i=d.size()-1;i>=0;i--){
83+
if(d[i]>=0 and d[i]<='9'){
84+
res4+=(d[i]-48)*base;
85+
base=base*16;
86+
}
87+
else if(d[i]>='A' and d[i]<='F'){
88+
res4+=(d[i]-55)*base;
89+
base*=16;
90+
}
91+
92+
}
93+
result.push_back(to_string(res4));
94+
return result;
95+
}
96+
};

ConvertToBase-2.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
Given an integer n, return a binary string representing its representation in base -2.
3+
Note that the returned string should not have leading zeros unless the string is "0".
4+
5+
Example 1:
6+
Input: n = 2
7+
Output: "110"
8+
Explantion: (-2)2 + (-2)1 = 2
9+
10+
Example 2:
11+
Input: n = 3
12+
Output: "111"
13+
Explantion: (-2)2 + (-2)1 + (-2)0 = 3
14+
15+
Example 3:
16+
Input: n = 4
17+
Output: "100"
18+
Explantion: (-2)2 = 4
19+
*/
20+
21+
class Solution {
22+
public:
23+
string baseNeg2(int n) {
24+
if(n==0) return "0";
25+
string s="";
26+
int counter=0;
27+
while(n){
28+
if(n%2==0){
29+
s+='0';
30+
}else{
31+
s+='1';
32+
if(counter%2==1){
33+
n++;
34+
}
35+
}
36+
counter+=1;
37+
n=n>>1;
38+
}
39+
reverse(s.begin(),s.end());
40+
return s;
41+
}
42+
};

LargestPrimeFactor.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
Given a number N, the task is to find the largest prime factor of that number.
3+
4+
Example 1:
5+
Input:
6+
N = 5
7+
Output:
8+
5
9+
Explanation:
10+
5 has 1 prime factor
11+
i.e 5 only.
12+
13+
Example 2:
14+
Input:
15+
N = 24
16+
Output:
17+
3
18+
Explanation:
19+
24 has 2 prime factors
20+
3 and 2 in which 3 is
21+
greater
22+
23+
Your Task:
24+
You don't need to read input or print anything. Your task is to complete the function largestPrimeFactor() which takes an integer N as input parameters and returns an integer, largest prime factor of N.
25+
26+
Expected Time Complexity: O(sqrt(N))
27+
Expected Space Complexity: O(1)
28+
*/
29+
30+
class Solution{
31+
public:
32+
33+
long long int largestPrimeFactor(int N){
34+
int result=2;
35+
for(int i=2;i*i<=N;i++){
36+
while(N%i==0){
37+
result=max(result,i);
38+
N/=i;
39+
}
40+
}
41+
result=max(result,N);
42+
return result;
43+
}
44+
};

ModularExponentiation.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
Implement pow(x, n) % M.
3+
In other words, given x, n and M, find (xn) % M.
4+
5+
Example 1:
6+
Input:
7+
x = 3, n = 2, m = 4
8+
Output:
9+
1
10+
Explanation:
11+
32 = 9. 9 % 4 = 1.
12+
13+
Example 2:
14+
Input:
15+
x = 2, n = 6, m = 10
16+
Output:
17+
4
18+
Explanation:
19+
26 = 64. 64 % 10 = 4.
20+
21+
Your Task:
22+
You don't need to read or print anything. Your task is to complete the function PowMod() which takes integers x, n and M as input parameters and returns xn % M.
23+
24+
Expected Time Complexity: O(log(n))
25+
Expected Space Complexity: O(1)
26+
*/
27+
28+
/// Modular Exponentiation for large numbers
29+
30+
class Solution
31+
{
32+
public:
33+
long long int PowMod(long long int x,long long int n,long long int M)
34+
{
35+
if(x==0) return 0;
36+
if(n==0) return 1;
37+
if(n==1) return x%M;
38+
long long int y;
39+
if(n%2==0){
40+
y=PowMod(x,n/2,M)%M;
41+
y=(y*y)%M;
42+
}else{
43+
y=x%M;
44+
y=(y*PowMod(x,n-1,M)%M)%M;
45+
}
46+
return (long long int)(y+M)%M;
47+
}
48+
};

0 commit comments

Comments
 (0)