Skip to content

Commit 3eb4e8e

Browse files
authored
MATH (#232)
1 parent b9a17c2 commit 3eb4e8e

10 files changed

+299
-0
lines changed

Maths/CountDigit.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int countDigits(int n)
5+
{
6+
return (log10(n) + 1);
7+
}
8+
9+
int main()
10+
{
11+
int n;
12+
cin >> n;
13+
if (n == 0)
14+
{
15+
cout << 0;
16+
}
17+
else
18+
{
19+
n = abs(n);
20+
cout << countDigits(n);
21+
}
22+
23+
return 0;
24+
}

Maths/highest.cpp

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// You are given a 2D string array A of dimensions N x 2,
2+
3+
// where each row consists of two strings: first is the name of the student, second is their marks.
4+
5+
// You have to find the maximum average mark. If it is a floating point, round it down to the nearest integer less than or equal to the number.
6+
7+
#include <bits/stdc++.h>
8+
using namespace std;
9+
10+
int main()
11+
{
12+
int n;
13+
cin >> n;
14+
vector<vector<string>> A(n, vector<string>(2));
15+
for (int i = 0; i < n; i++)
16+
{
17+
cin >> A[i][0] >> A[i][1];
18+
}
19+
20+
map<string, int> m;
21+
map<string, int> count;
22+
for (int i = 0; i < n; i++)
23+
{
24+
m[A[i][0]] += stoi(A[i][1]);
25+
cout << m[A[i][0]] << endl;
26+
count[A[i][0]]++;
27+
}
28+
int max = 0;
29+
string name;
30+
for (auto i : m)
31+
{
32+
// cout << i.first << " " << i.second << endl;
33+
// cout << i.first << " " << round(i.second / count[i.first]) << endl;
34+
if (round (1.0 * i.second / count[i.first]) > max)
35+
{
36+
max = round(i.second / count[i.first]);
37+
name = i.first;
38+
}
39+
}
40+
cout << name << " " << max << endl;
41+
42+
return 0;
43+
}
44+

Maths/nextSimilar.cpp

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Given a number A in a form of string.
2+
3+
// You have to find the smallest number that has same set of digits as A and is greater than A.
4+
5+
// If A is the greatest possible number with its set of digits, then return -1.
6+
7+
#include <bits/stdc++.h>
8+
using namespace std;
9+
10+
int main()
11+
{
12+
string A;
13+
cin >> A;
14+
int n = A.length();
15+
int i;
16+
for (i = n - 1; i > 0; i--)
17+
{
18+
if (A[i] > A[i - 1])
19+
break;
20+
}
21+
if (i == 0)
22+
{
23+
cout << -1 << endl;
24+
return 0;
25+
}
26+
int x = A[i - 1], smallest = i;
27+
for (int j = i + 1; j < n; j++)
28+
{
29+
if (A[j] > x && A[j] < A[smallest])
30+
smallest = j;
31+
}
32+
33+
swap(A[smallest], A[i - 1]);
34+
sort(A.begin() + i, A.end());
35+
cout << A << endl;
36+
37+
return 0;
38+
}

Maths/power.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int power(int m,int n){
6+
if(n==0){
7+
return 1;
8+
9+
}
10+
if(n%2==0){
11+
return power(m*m,n/2);
12+
}
13+
return m*power(m*m,(n-1)/2);
14+
}
15+
16+
int main(){
17+
int a,b;
18+
cin>>a>>b;
19+
cout<<pow(a, b);
20+
}

Maths/powerIterative.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// 2 ^ 11 , x^n
2+
// 11 -> 1 0 1 1 ;
3+
// x = 2 , n=11 , res =1
4+
// 11/2 = 5 -> 1 res = res*x =2 n=5; x=x*x=4
5+
// 5/2 = 2 -> 1 res = 2 *4 = 8 n=2 x = 16
6+
// 2/2 =1 -> 0 res = 8 n=1 x = 16*16 =256
7+
// 1/2 =0 -> 1 res = 8 *256
8+
// res = 2 * 4 *256
9+
10+
#include <bits/stdc++.h>
11+
12+
using namespace std;
13+
14+
int IterativePower(int x,int n){
15+
int res = 1;
16+
while( n ){
17+
if(n & 1){
18+
res = res * x ;
19+
}
20+
x = x * x ;
21+
n = n>>1;
22+
}
23+
return res;
24+
}
25+
26+
int main(){
27+
int x,n;
28+
cin>>x>>n;
29+
cout<<IterativePower(x, n);
30+
}

Maths/primeFactor.cpp

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
void PrimeFactor(int n)
5+
{
6+
if (n == 1)
7+
return;
8+
9+
int i = 2;
10+
while (n % i == 0)
11+
{
12+
cout << i << ' ';
13+
n = n / i;
14+
}
15+
16+
i = 3;
17+
while (n % i == 0)
18+
{
19+
cout << i << ' ';
20+
n = n / i;
21+
}
22+
23+
for (int i = 5; i * i <= n; i = i + 6)
24+
{
25+
while (n % i == 0)
26+
{
27+
cout << i << ' ';
28+
n = n / i;
29+
}
30+
while (n % (i + 2) == 0)
31+
{
32+
cout << i + 2 << ' ';
33+
n = n / (i + 2);
34+
}
35+
}
36+
if (n > 3)
37+
cout << n << " ";
38+
}
39+
40+
int main()
41+
{
42+
int n;
43+
cin >> n;
44+
PrimeFactor(n);
45+
46+
return 0;
47+
}

Maths/primeNo.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
bool isPrime(int n)
5+
{
6+
if (n == 1)
7+
return false;
8+
9+
if (n == 2 || n == 3)
10+
return true;
11+
12+
if ((n % 2 == 0 )|| (n % 3) == 0)
13+
return false;
14+
15+
for (int i = 5; i * i <= n; i = i + 6)
16+
{
17+
if ((n % i == 0) || (n % (i + 2) == 0))
18+
return false;
19+
}
20+
return true;
21+
}
22+
23+
int main()
24+
{
25+
int n;
26+
cin >> n;
27+
cout << isPrime(n);
28+
29+
return 0;
30+
}

Maths/reverseInteger.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// You are given an integer N and the task is to reverse the digits of the given integer. Return 0 if the result overflows and does not fit in a 32 bit signed integer
2+
3+
#include <bits/stdc++.h>
4+
using namespace std;
5+
6+
int main()
7+
{
8+
int n;
9+
cin >> n;
10+
int rev = 0;
11+
while (n != 0)
12+
{
13+
int rem = n % 10;
14+
n /= 10;
15+
if (rev > INT_MAX / 10 || (rev == INT_MAX / 10 && rem > 7))
16+
return 0;
17+
if (rev < INT_MIN / 10 || (rev == INT_MIN / 10 && rem < -8))
18+
return 0;
19+
rev = rev * 10 + rem;
20+
}
21+
cout << rev << endl;
22+
return 0;
23+
}

Maths/seiveOfEratothenes.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
void printAllPrime(int n){
5+
vector<bool> isPrime(n+1,true);
6+
for(int i=2; i<=n ; i++){
7+
if(isPrime[i]){
8+
cout<<i<<" ";
9+
for(int j= i*i; j<=n; j=j+i){
10+
isPrime[j] = false;
11+
}
12+
}
13+
}
14+
15+
}
16+
17+
int main(){
18+
int n;
19+
cin>>n;
20+
printAllPrime(n);
21+
return 0;
22+
}

Maths/trailingZeroInFactorial.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int countTrailingZeroes(int n)
5+
{
6+
int c =1;
7+
int countZeroes = 0;
8+
while((n/(pow(5,c)))){
9+
countZeroes += (n/(pow(5,c)));
10+
c++;
11+
}
12+
return countZeroes;
13+
}
14+
15+
int main()
16+
{
17+
int n;
18+
cin >> n;
19+
cout << countTrailingZeroes(n);
20+
return 0;
21+
}

0 commit comments

Comments
 (0)