Skip to content

Commit decef30

Browse files
committed
Added C++ solutions
1 parent 8d43f77 commit decef30

File tree

4 files changed

+97
-3
lines changed

4 files changed

+97
-3
lines changed

C++/14_Lucky_Numbers.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
bool isValid(int i)
5+
{
6+
bool check = true;
7+
while (i > 0)
8+
{
9+
int rem = i % 10;
10+
if (rem != 4 || rem != 7) //conditions to check for validity
11+
check = false;
12+
i = i / 10;
13+
}
14+
return check;
15+
}
16+
17+
int main()
18+
{
19+
int a, b;
20+
cin >> a >> b; //input from user
21+
int cnt = 0;
22+
for (int i = a; i <= b; i++)
23+
{
24+
if (isValid(i)) //checking validity for each number in range a to b
25+
{
26+
cnt++;
27+
cout << i << " ";
28+
}
29+
}
30+
if (cnt == 0) //if no number is valid then print -1
31+
cout << -1 << endl;
32+
return 0;
33+
}

C++/15_Numbers_Histogram.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int main()
5+
{
6+
char ch;
7+
cin >> ch; //input character
8+
int n;
9+
cin >> n;
10+
vector<int> v(n);
11+
for (int i = 0; i < n; i++)
12+
cin >> v[i]; //input
13+
for (auto i : v)
14+
{
15+
for (int j = 0; j < i; j++)
16+
{
17+
cout << ch; //printing the character Xi times
18+
}
19+
cout << endl;
20+
}
21+
return 0;
22+
}

C++/26_Binary_Search.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int main()
5+
{
6+
int n, q;
7+
cin >> n >> q; //input from user
8+
vector<int> v(n);
9+
for (int i = 0; i < n; i++)
10+
cin >> v[i];
11+
sort(v.begin(), v.end()); //sorting the vector to apply binary search
12+
while (q--)
13+
{
14+
int target;
15+
cin >> target;
16+
bool check = false;
17+
int start = 0, end = n - 1;
18+
while (start <= end) //running Binary Search for each query
19+
{
20+
int mid = start + (end - start) / 2;
21+
if (v[mid] == target)
22+
{
23+
check = true;
24+
break;
25+
}
26+
else if (v[mid] < target)
27+
{
28+
start = mid + 1;
29+
}
30+
else
31+
end = mid - 1;
32+
}
33+
if (check) //condition
34+
cout << "found" << endl;
35+
else
36+
cout << "not found" << endl;
37+
}
38+
return 0;
39+
}

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ In the "Repository" you will find the topics and its problems.
9191
|11|[Primes from 1 to n](https://codeforces.com/group/MWSDmqGsZm/contest/219432/problem/J) | [C++](./C++/37_Primes_from_1to_n.cpp)
9292
|12|[Divisors](https://codeforces.com/group/MWSDmqGsZm/contest/219432/problem/K) | [C++](./C++/38_Divisors.cpp)
9393
|13|[GCD](https://codeforces.com/group/MWSDmqGsZm/contest/219432/problem/L) | [C++](./C++/13_GCD.cpp)
94-
|14|[Lucky Numbers](https://codeforces.com/group/MWSDmqGsZm/contest/219432/problem/M)
95-
|15|[Numbers Histogram](https://codeforces.com/group/MWSDmqGsZm/contest/219432/problem/N)
94+
|14|[Lucky Numbers](https://codeforces.com/group/MWSDmqGsZm/contest/219432/problem/M) | [C++](./C++/14_Lucky_Numbers.cpp)
95+
|15|[Numbers Histogram](https://codeforces.com/group/MWSDmqGsZm/contest/219432/problem/N) | [C++](./C++/15_Numbers_Histogram.cpp)
9696
|16|[Pyramid](https://codeforces.com/group/MWSDmqGsZm/contest/219432/problem/O) | [C++](./C++/42_Pyramid.cpp)
9797
|17|[Shape1](https://codeforces.com/group/MWSDmqGsZm/contest/219432/problem/P) | [C++](./C++/43_Shape1.cpp)
9898
|18|[Digits](https://codeforces.com/group/MWSDmqGsZm/contest/219432/problem/Q) | [C++](./C++/44_Digits.cpp)
@@ -135,7 +135,7 @@ In the "Repository" you will find the topics and its problems.
135135
|23|[Mirror Array](https://codeforces.com/group/MWSDmqGsZm/contest/219774/problem/W) | [C++](./C++/mirror.cpp)
136136
|24|[8 Neighbors](https://codeforces.com/group/MWSDmqGsZm/contest/219774/problem/X)| [C++](./C++/neighbours.cpp)
137137
|25|[Range sum query](https://codeforces.com/group/MWSDmqGsZm/contest/219774/problem/Y) | [C++](./C++/rangeSumQuery.cpp)
138-
|26|[Binary Search](https://codeforces.com/group/MWSDmqGsZm/contest/219774/problem/Z)
138+
|26|[Binary Search](https://codeforces.com/group/MWSDmqGsZm/contest/219774/problem/Z) | [C++](./C++/26_Binary_Search.cpp)
139139

140140
## Strings
141141

0 commit comments

Comments
 (0)