Skip to content

Commit 7d14c0d

Browse files
solved on 30/09/23
1 parent 8b6097e commit 7d14c0d

File tree

3 files changed

+161
-4
lines changed

3 files changed

+161
-4
lines changed

DivideUsingBS.cpp renamed to DivideUsingBSWithPrecision.cpp

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,35 @@ int getQuotient(int divisor, int divident)
2828
return ans;
2929
}
3030

31+
double getQuotientWithPrecision(int divisor, int divident)
32+
{
33+
double quotient = getQuotient(divisor, divident);
34+
double step = 0.1;
35+
int precision = 10;
36+
37+
for (int i = 0; i < precision; i++)
38+
{
39+
double temp = quotient;
40+
while (temp * divisor < divident)
41+
{
42+
quotient = temp;
43+
temp += step;
44+
}
45+
step /= 10;
46+
}
47+
return quotient;
48+
}
49+
3150
int main()
3251
{
33-
int divisor = -5;
34-
int divident = 20;
35-
int ans = getQuotient(abs(divisor), abs(divident));
52+
int divisor = -3;
53+
int divident = 571;
54+
double ans = getQuotientWithPrecision(abs(divisor), abs(divident));
3655

3756
if ((divisor < 0 && divident > 0) || (divisor > 0 && divident < 0))
3857
{
3958
ans = -ans;
4059
}
41-
cout << ans << endl;
60+
printf("%.10f\n", ans);
4261
return 0;
4362
}

SqrtWithPrecision.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int mySqrt(int x)
5+
{
6+
int s = 0, e = x;
7+
int ans = -1;
8+
long long mid = 0;
9+
10+
while (s <= e)
11+
{
12+
mid = s + (e - s) / 2;
13+
// ans found
14+
if (mid * mid == x)
15+
{
16+
return mid;
17+
}
18+
else if (mid * mid < x)
19+
{
20+
// store ans
21+
// and & move right
22+
ans = mid;
23+
s = mid + 1;
24+
}
25+
else
26+
{
27+
// move left
28+
e = mid - 1;
29+
}
30+
}
31+
return ans;
32+
}
33+
34+
double precisionOfSqrt(int n)
35+
{
36+
double sqrt = mySqrt(n);
37+
double step = 0.1;
38+
39+
int precision = 11;
40+
for (int i = 0; i < precision; i++)
41+
{
42+
double j = sqrt;
43+
while (j * j <= n)
44+
{
45+
sqrt = j;
46+
j += step;
47+
}
48+
step /= 10;
49+
}
50+
51+
return sqrt;
52+
}
53+
54+
int main()
55+
{
56+
int x = 51;
57+
// cout << precisionOfSqrt(x) << endl;
58+
printf("%.11f\n", precisionOfSqrt(x));
59+
return 0;
60+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
string removeDuplicates(string s, int k)
5+
{
6+
string ans = "";
7+
int idx = 0;
8+
while (idx < s.length())
9+
{
10+
int temp = k - 1;
11+
bool isRepeated = false;
12+
while (temp)
13+
{
14+
if (idx + temp >= s.length())
15+
{
16+
break;
17+
}
18+
19+
if (s[idx] == s[idx + temp])
20+
{
21+
isRepeated = true;
22+
}
23+
else
24+
{
25+
isRepeated = false;
26+
}
27+
--temp;
28+
}
29+
30+
if (!isRepeated)
31+
{
32+
ans.push_back(s[idx]);
33+
idx++;
34+
}
35+
else
36+
{
37+
idx += k;
38+
}
39+
}
40+
return ans;
41+
}
42+
43+
int countCharactersWithKOrMoreOccurrences(const string input, int k)
44+
{
45+
unordered_map<char, int> charCount;
46+
47+
// Count the occurrences of each character in the input string
48+
for (char c : input)
49+
{
50+
charCount[c]++;
51+
}
52+
53+
int count = 0;
54+
55+
// Iterate through the character count map and count characters that appear k or more times
56+
for (const auto &pair : charCount)
57+
{
58+
if (pair.second >= k)
59+
{
60+
count++;
61+
}
62+
}
63+
64+
return count;
65+
}
66+
67+
int main()
68+
{
69+
int k = 3;
70+
string s = "deeedbbcccbdaa";
71+
int cnt = countCharactersWithKOrMoreOccurrences(s, k);
72+
for (int i = 0; i < cnt; i++)
73+
{
74+
s = removeDuplicates(s, k);
75+
}
76+
cout << s << endl;
77+
return 0;
78+
}

0 commit comments

Comments
 (0)