-
Notifications
You must be signed in to change notification settings - Fork 264
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #373 from ANUKOOL324/codeissues
Enhance Repository with Optimized Solutions of CP Math Problems.
- Loading branch information
Showing
6 changed files
with
276 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// A Simple Implementation Of The Tower Of Hanoi Problem In C | ||
|
||
#include <stdio.h> | ||
|
||
void towerOfHanoi(int n, char source, char destination, char auxiliary) | ||
{ | ||
if (n == 1) | ||
{ | ||
printf("Move disk 1 from rod %c to rod %c\n", source, destination); | ||
return; | ||
} | ||
// Move n-1 disks from source to auxiliary, using destination as temporary | ||
towerOfHanoi(n - 1, source, auxiliary, destination); | ||
|
||
// Move the nth disk from source to destination | ||
printf("Move disk %d from rod %c to rod %c\n", n, source, destination); | ||
|
||
// Move the n-1 disks from auxiliary to destination, using source as temporary | ||
towerOfHanoi(n - 1, auxiliary, destination, source); | ||
} | ||
|
||
int main() | ||
{ | ||
int n; | ||
printf("Enter the no. of Disks: "); | ||
scanf("%d", &n); | ||
|
||
// A, B, and C are rods | ||
towerOfHanoi(n, 'A', 'C', 'B'); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
//FIND THE NUMBER OF THE DIVISIOR | ||
|
||
//Time complexity is sqrt(n) | ||
#include <iostream> | ||
using namespace std; | ||
int main() | ||
{ | ||
int n; | ||
cin >> n; | ||
int div = 0; | ||
for (int i = 1; i * i <= n; i++) // all the divisor of a number is equally distributed across the srt(n) of the n ; | ||
{ | ||
if (n % i == 0) | ||
{ | ||
div++; | ||
if (i != n / i) // const time complexity | ||
{ | ||
div++; | ||
} | ||
} | ||
} | ||
cout << "No Of Divisor::" << div << "\n"; | ||
if (div == 2) | ||
{ | ||
cout << n << "is prime no" << "\n"; | ||
} | ||
else | ||
cout << "composite No " << "\n"; | ||
|
||
return 0; | ||
} | ||
|
||
// NO OF THE DIVISORS FORM THE 1 to n | ||
// time complexity is o(n sqrt(n)) | ||
#include <iostream> | ||
using namespace std; | ||
int countdivisior(int n) | ||
{ | ||
int div = 0; | ||
for (int i = 1; i * i <= n; i++) | ||
{ | ||
if (n % i == 0) | ||
{ | ||
div++; | ||
if (i != n / i) | ||
{ | ||
div++; | ||
} | ||
} | ||
} | ||
return div; | ||
} | ||
int main() | ||
{ | ||
int n; | ||
cin >> n; | ||
for (int i = 1; i <= n; i++) | ||
{ | ||
int divisors = countdivisior(i); | ||
cout << "no of divisors of" << i << ": " << divisors << "\n"; | ||
} | ||
return 0; | ||
} | ||
|
||
// 0(nlog(n)) | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
int main() | ||
{ | ||
int n; | ||
cin >> n; | ||
vector<int> divcount(n + 1, 0); | ||
for (int i = 1; i <= n; i++) // will count for given no -- how many of the numbers it is divisor of | ||
{ | ||
for (int j = i; j <= n; j += i) | ||
{ | ||
divcount[j]++; | ||
} | ||
} | ||
for (int i = 1; i <= n; i++) | ||
{ | ||
cout << i << " : " << divcount[i] << "\n"; | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// GCD-GREATEST COMMON DIVISIOR | ||
|
||
// FOR LCM = A*B/GCD(A,B) | ||
#include <iostream> | ||
using namespace std; | ||
int gcd(int a, int b) | ||
{ | ||
if (b == 0) | ||
{ | ||
return a; | ||
} | ||
return gcd(b, a % b); | ||
} | ||
int main() | ||
{ | ||
int a, b; | ||
cin >> a >> b; | ||
cout << gcd(a, b) << "\n"; | ||
// cout<<"lcm :"<<a*b/(gcd(a,b))<<endl; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Exponent and power a^b | ||
|
||
#include <iostream> | ||
using namespace std; | ||
int pow(int a, long long int b) | ||
{ | ||
if (b == 0) | ||
{ | ||
return 1; | ||
} | ||
int half = pow(a, b / 2); | ||
int ans; | ||
if (b % 2 == 0) | ||
{ | ||
ans = 1LL * half * half; | ||
} | ||
else | ||
{ | ||
ans = 1LL * half * half; | ||
ans = 1LL * ans * a; | ||
} | ||
return ans; | ||
} | ||
int main() | ||
{ | ||
int a, b, m; | ||
cin >> a >> b >> m; | ||
cout << pow(a, b); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
Problem Statement: Non-Degenerate Right Triangles | ||
Satyam is given n distinct points on a 2D coordinate plane. It is guaranteed that 0 ≤ yi ≤ 1 for all the given points (xi, yi). | ||
Your task is to determine how many different non-degenerate right triangles can be formed by choosing three different points as its vertices. | ||
Definitions: | ||
- A non-degenerate right triangle has positive area and an interior 90° angle. | ||
- Two triangles are considered different if there is at least one point that is a vertex of one triangle but not the other. | ||
Input: | ||
- The first line contains an integer t (1 ≤ t ≤ 10^4) — the number of test cases. | ||
- The first line of each test case contains an integer n (3 ≤ n ≤ 2⋅10^5) — the number of points. | ||
- The following n lines contain two integers xi and yi (0 ≤ xi ≤ n, 0 ≤ yi ≤ 1) — the coordinates of the i-th point. All points (xi, yi) are distinct. | ||
Output: | ||
For each test case, output the number of distinct non-degenerate right triangles that can be formed from choosing three points. | ||
Constraints: | ||
The sum of all n over all test cases does not exceed 2⋅10^5. | ||
*/ | ||
|
||
// THE SOLUTION: | ||
|
||
#include <iostream> | ||
#include <map> | ||
#include <vector> | ||
using namespace std; | ||
|
||
#define fo(i, n) for (int i = 0; i < n; i++) | ||
#define int long long | ||
|
||
int main() | ||
{ | ||
ios_base::sync_with_stdio(0); | ||
cin.tie(0); | ||
cout.tie(0); | ||
|
||
int t; | ||
cin >> t; | ||
|
||
while (t--) | ||
{ | ||
int n; | ||
cin >> n; | ||
map<pair<int, int>, int> m; | ||
vector<int> count(2); // To store count of points where y=0 and y=1 | ||
|
||
fo(i, n) | ||
{ | ||
int x, y; | ||
cin >> x >> y; | ||
m[{x, y}] = 1; | ||
count[y]++; | ||
} | ||
|
||
int ans = 0; | ||
for (auto &[i, j] : m) | ||
{ | ||
int x = i.first; | ||
int y = i.second; | ||
|
||
if (m.count({x, 1 - y})) | ||
{ | ||
ans += count[y] - 1; // Right angle triangle sharing a vertical line | ||
} | ||
|
||
if (m.count({x - 1, 1 - y}) && m.count({x + 1, 1 - y})) | ||
{ // trinagle with 90 degree at the y=0 and y=1 point. | ||
ans++; | ||
} | ||
} | ||
|
||
cout << ans << '\n'; | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// SIEVE OF ERATOSTHENES | ||
|
||
// For efficient prime number generation in O(nlog(logn)). | ||
|
||
#include <bits/stdc++.h> | ||
#include <vector> | ||
using namespace std; | ||
int main() | ||
{ | ||
int n; | ||
cin >> n; | ||
vector<int> isprime(n + 1, true); | ||
isprime[0] = isprime[1] = false; | ||
for (int i = 1; i <= n; i++) | ||
{ | ||
if (!isprime[i]) | ||
continue; | ||
// if prime | ||
for (int j = i + i; j <= n; j += i) | ||
{ | ||
isprime[j] = false; | ||
} | ||
} | ||
for (int i = 0; i <= n; i++) | ||
{ | ||
cout << isprime[i] << "\n"; | ||
} | ||
|
||
return 0; | ||
} |