Skip to content

Commit dfd7d3c

Browse files
Added solution for Best Divisor in C++
1 parent 22f5536 commit dfd7d3c

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
# HackerRank Mathematics
22

3-
![problems-solved](https://img.shields.io/badge/Problems%20Solved-12/284-1f425f.svg)
3+
![problems-solved](https://img.shields.io/badge/Problems%20Solved-13/284-1f425f.svg)
44
![problems-solved-java](https://img.shields.io/badge/Java-0/284-008000.svg)
55
![problems-solved-python](https://img.shields.io/badge/Python-1/284-008000.svg)
6-
![problems-solved-cpp](https://img.shields.io/badge/C++-11/284-008000.svg)
6+
![problems-solved-cpp](https://img.shields.io/badge/C++-12/284-008000.svg)
77
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](CONTRIBUTING.md)
88
[![cp](https://img.shields.io/badge/also%20see-Competitve%20Programming-1f72ff.svg)](https://github.com/anishLearnsToCode/competitive-programming)
99
[![profile](https://img.shields.io/badge/also%20see-My%20Hackerrank%20Profile-1f72ff.svg)](https://www.hackerrank.com/anishviewer)
1010

1111
## Problems
12-
### Fundamentals ![problems-solved](https://img.shields.io/badge/Solved-7/32-00ffff.svg)
12+
### Fundamentals ![problems-solved](https://img.shields.io/badge/Solved-8/32-00ffff.svg)
1313
| Name | Difficulty | Solution |
1414
|------|:----------:|:--------:|
1515
| [Find The Point](https://www.hackerrank.com/challenges/find-point/problem) | Easy |[![cpp](assets/cpp.png)](cpp/findThePoint.cpp) |
@@ -21,7 +21,7 @@
2121
| [Connecting Towns](https://www.hackerrank.com/challenges/connecting-towns) | Easy |[![cpp](assets/cpp.png)](cpp/connectingTowns.cpp) |
2222
| [Cutting Paper Squares](https://www.hackerrank.com/challenges/p1-paper-cutting) | Easy | |
2323
| [Sherlock and Moving Tiles](https://www.hackerrank.com/challenges/sherlock-and-moving-tiles) | Easy | |
24-
| [Best Divisor](https://www.hackerrank.com/challenges/best-divisor) | Easy | |
24+
| [Best Divisor](https://www.hackerrank.com/challenges/best-divisor) | Easy |[![cpp](assets/cpp.png)](cpp/bestDivisor.cpp) |
2525
| [Restaurant](https://www.hackerrank.com/challenges/restaurant) | Easy |[![cpp](assets/cpp.png)](cpp/restaurant.cpp) |
2626
| [Reverse Game](https://www.hackerrank.com/challenges/reverse-game) | Easy | |
2727
| [Strange Grid Again](https://www.hackerrank.com/challenges/strange-grid) | Easy | |

cpp/bestDivisor.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <cmath>
2+
#include <cstdio>
3+
#include <vector>
4+
#include <iostream>
5+
#include <algorithm>
6+
using namespace std;
7+
8+
int main() {
9+
int n;
10+
cin >> n;
11+
int best = -1, ans = 1;
12+
for (int i = 1; i <= n; i++) {
13+
if (n % i != 0) {
14+
continue;
15+
}
16+
int s = 0;
17+
int x = i;
18+
while (x > 0) {
19+
s += x % 10;
20+
x /= 10;
21+
}
22+
if (s > best) {
23+
best = s;
24+
ans = i;
25+
}
26+
}
27+
cout << ans << endl;
28+
return 0;
29+
}
30+

0 commit comments

Comments
 (0)