-
Notifications
You must be signed in to change notification settings - Fork 335
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
71b4352
commit 3899345
Showing
1,625 changed files
with
99,586 additions
and
2 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,62 @@ | ||
// { Driver Code Starts | ||
//Initial Template for C++ | ||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
// } Driver Code Ends | ||
//User function Template for C++ | ||
|
||
class Solution | ||
{ | ||
public: | ||
int exactly3Divisors(int n) | ||
{ | ||
cout << "Numbers with 3 divisors : "<< endl; | ||
for(int i = 2; i * i <= n; i++) | ||
{ | ||
// Checks for prime | ||
if (isPrime(i)) //if prime checks if its square is less than or equal to n | ||
if (i * i <= n) | ||
{ | ||
// Print numbers | ||
cout << i * i << " "; | ||
} | ||
} | ||
} | ||
return 0; | ||
} | ||
//method for checking whether number is prime or not | ||
bool isPrime(int n) | ||
{ | ||
if (n == 0 || n == 1) | ||
return false; | ||
for(int i = 2; i * i <= n; i++) | ||
{ | ||
if (n % i == 0) | ||
return false; | ||
} | ||
return true; | ||
} | ||
}; | ||
|
||
// { Driver Code Starts. | ||
|
||
int main() | ||
{ | ||
int T; | ||
|
||
//taking testcases | ||
cin >> T; | ||
while (T--) | ||
{ | ||
int N; | ||
|
||
//taking N | ||
cin >> N; | ||
Solution ob; | ||
//calling function exactly3Divisors() | ||
cout << ob.exactly3Divisors(N) << endl; | ||
} | ||
return 0; | ||
} // } Driver Code Ends |
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,44 @@ | ||
// { Driver Code Starts | ||
//Initial Template for C++ | ||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
// } Driver Code Ends | ||
//User function Template for C++ | ||
|
||
class Solution | ||
{ | ||
public: | ||
//Complete this function | ||
double termOfGP(int A, int B, int N) | ||
{ | ||
//first finding ratio | ||
double ratio = double(B)/double(A); | ||
//Nth term according to the formula | ||
double ans = A*(pow(ratio,N-1)); | ||
return ans; | ||
} | ||
}; | ||
|
||
// { Driver Code Starts. | ||
|
||
int main() | ||
{ | ||
int T; //testcases total | ||
cin >> T; //input the testcases | ||
|
||
for (int i = 0; i < T; i++) //white testcases are not exhausted | ||
{ | ||
int A, B; | ||
cin >> A >> B; //input first and second term of gp | ||
int N; | ||
cin >> N; //input n | ||
Solution ob; | ||
cout << floor(ob.termOfGP(A, B, N)) << endl; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
// } Driver Code Ends |
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,61 @@ | ||
/* | ||
Given an array of size N-1 such that it only contains distinct integers in the range of 1 to N. Find the missing element. | ||
Example 1: | ||
Input: | ||
N = 5 | ||
A[] = {1,2,3,5} | ||
Output: 4 | ||
Example 2: | ||
Input: | ||
N = 10 | ||
A[] = {1,2,3,4,5,6,7,8,10} | ||
Output: 9 | ||
Your Task : | ||
You don't need to read input or print anything. | ||
Complete the function MissingNumber() that takes array and N as input parameters and returns the value of the missing number. | ||
Expected Time Complexity: O(N) | ||
Expected Auxiliary Space: O(1) | ||
*/ | ||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
class Solution | ||
{ | ||
public: | ||
int MissingNumber(vector<int> &array, int n) | ||
{ | ||
// Your code goes here | ||
int i=0,sum=0,missing_num; | ||
int sum_of_n=n*(n+1)/2;//sum of first n natural numbers | ||
for(i=0;i<n-1;i++) | ||
sum+=array[i];//sum of elements present in array. | ||
missing_num=sum_of_n-sum;//missing element | ||
return missing_num; | ||
} | ||
}; | ||
|
||
int main() | ||
{ | ||
int t; | ||
cin >> t; | ||
while (t--) | ||
{ | ||
int n; | ||
cin >> n; | ||
|
||
vector<int> array(n - 1); | ||
for (int i = 0; i < n - 1; ++i) | ||
cin >> array[i]; | ||
Solution obj; | ||
cout << obj.MissingNumber(array, n) << "\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,41 @@ | ||
// { Driver Code Starts | ||
//Initial Template for C++ | ||
|
||
#include <iostream> | ||
using namespace std; | ||
|
||
// } Driver Code Ends | ||
//User function Template for C++ | ||
|
||
class Solution | ||
{ | ||
public: | ||
//Complete this function | ||
int modInverse(int a, int m) | ||
{ | ||
for (int x = 1; x < m; x++) | ||
if (((a%m) * (x%m)) % m == 1) | ||
return x; | ||
} | ||
}; | ||
|
||
// { Driver Code Starts. | ||
|
||
int main() | ||
{ | ||
int T; | ||
|
||
//taking testcases | ||
cin >> T; | ||
while (T--) | ||
{ | ||
int a, m; | ||
|
||
//taking input a and m | ||
cin >> a >> m; | ||
Solution ob; | ||
//calling function modInverse() | ||
cout << ob.modInverse(a, m) << endl; | ||
} | ||
return 0; | ||
} // } Driver Code Ends |
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,45 @@ | ||
// { Driver Code Starts | ||
//Initial Template for C++ | ||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
// } Driver Code Ends | ||
//User function Template for C++ | ||
|
||
class Solution | ||
{ | ||
public: | ||
bool isPrime(int N) | ||
{ | ||
for(int i=2;i<=sqrt(N);i++) | ||
{ | ||
if(N%i==0) | ||
return false; | ||
} | ||
return true; | ||
//Your code here | ||
} | ||
}; | ||
|
||
// { Driver Code Starts. | ||
|
||
int main() | ||
{ | ||
int T; //testcases | ||
cin >> T; //input testcases | ||
while (T--) //while testcase have not been exhausted | ||
{ | ||
int N; | ||
cin >> N; //input n | ||
Solution ob; | ||
if (ob.isPrime(N)) | ||
cout << "Yes"; | ||
else | ||
cout << "No"; | ||
|
||
cout << endl; | ||
} | ||
return 0; | ||
} | ||
// } Driver Code Ends |
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,65 @@ | ||
// { Driver Code Starts | ||
// Initial template for C++ | ||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
// } Driver Code Ends | ||
// User function Template for C++ | ||
|
||
class Solution | ||
{ | ||
public: | ||
vector<int> quadraticRoots(int a, int b, int c) | ||
{ | ||
vector<int> ans; | ||
if(a==0) | ||
{ | ||
ans.push_back(-1); | ||
return ans; | ||
} | ||
int d = b*b - 4*a*c; | ||
double sqrt_val = sqrt(abs(d)); | ||
if(d>0) | ||
{ | ||
double root1 = (double)(-b + sqrt_val)/(2*a); | ||
double root2 = (double)(-b - sqrt_val)/(2*a); | ||
ans.push_back(root1); | ||
ans.push_back(root2); | ||
} | ||
else if(d==0) | ||
{ | ||
double root = -(double)b/(2*a); | ||
ans.push_back(root); | ||
ans.push_back(root); | ||
} | ||
else | ||
{ | ||
ans.push_back(-1); | ||
} | ||
return ans; | ||
// code here | ||
} | ||
}; | ||
|
||
// { Driver Code Starts. | ||
|
||
int main() | ||
{ | ||
int T; | ||
cin >> T; | ||
while (T--) | ||
{ | ||
int a, b, c; | ||
cin >> a >> b >> c; | ||
Solution ob; | ||
vector<int> roots = ob.quadraticRoots(a, b, c); | ||
if (roots.size() == 1 && roots[0] == -1) | ||
cout << "Imaginary"; | ||
else | ||
for (int i = 0; i < roots.size(); i++) | ||
cout << roots[i] << " "; | ||
cout << endl; | ||
} | ||
return 0; | ||
} // } Driver Code Ends |
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,36 @@ | ||
// { Driver Code Starts | ||
//Initial template for C++ | ||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
// } Driver Code Ends | ||
//User function template for C++ | ||
|
||
class Solution | ||
{ | ||
public: | ||
long long getSmallestDivNum(long long n) | ||
{ | ||
long long ans = 1; | ||
for(long long i=1;i<=n;i++) | ||
ans = (ans*i)/(__gcd(ans,i)); | ||
return ans; | ||
// code here | ||
} | ||
}; | ||
|
||
// { Driver Code Starts. | ||
int main() | ||
{ | ||
int t; | ||
cin >> t; | ||
while (t--) | ||
{ | ||
int n; | ||
cin >> n; | ||
Solution ob; | ||
cout << ob.getSmallestDivNum(n) << endl; | ||
} | ||
return 0; | ||
} // |
Oops, something went wrong.