forked from msameerfarooq/Hacktoberfest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
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
378e06c
commit 57442bd
Showing
91 changed files
with
6,179 additions
and
170 deletions.
There are no files selected for viewing
Empty file.
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,79 @@ | ||
/****** | ||
Trailing zeroes in factorial | ||
For an integer N find the number of trailing zeroes in N!. | ||
Example 1: | ||
Input: | ||
N = 6 | ||
Output: | ||
0 | ||
Explanation: | ||
5! = 120 so the number of trailing zero is 1. | ||
Example 2: | ||
Input: | ||
N = 5 | ||
Output: | ||
2 | ||
Explanation: | ||
4! = 24 so the number of trailing zero is 0. | ||
Your Task: | ||
You don't need to read input or print anything. Your task is to complete the function trailingZeroes() which take an integer N as an input parameter and returns the count of trailing zeroes in the N!. | ||
Expected Time Complexity: O(logN) | ||
Expected Auxiliary Space: O(1) | ||
Constraints: | ||
1 <= N <= 109 | ||
****/ | ||
//User function Template for C++ | ||
|
||
|
||
// Driver Code Starts | ||
#include<bits/stdc++.h> | ||
using namespace std; | ||
// Driver Code Ends | ||
|
||
class Solution | ||
{ | ||
public: | ||
int trailingZeroes(int N) | ||
{ | ||
// Write Your Code here | ||
int m=0; | ||
while(N>0){ | ||
m= m+N/5; | ||
N=N/5;} | ||
return m; | ||
} | ||
|
||
int trailingZeroes2(int N) | ||
{ | ||
// Write Your Code here | ||
int ans = 0; | ||
while(N/5) | ||
{ | ||
ans+=N/5; | ||
N/=5; | ||
} | ||
return ans; | ||
} | ||
}; | ||
|
||
// Driver Code Starts. | ||
int main() | ||
{ | ||
int t; | ||
cin >> t; | ||
while (t--) | ||
{ | ||
int N; | ||
cin >> N; | ||
Solution ob; | ||
int ans = ob.trailingZeroes(N); | ||
cout<<ans<<endl; | ||
} | ||
return 0; | ||
} | ||
//Driver Code Starts. |
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,69 @@ | ||
/** | ||
2. Lucky Numbers | ||
Lucky numbers are subset of integers. Rather than going into much theory, let us see the process of arriving at lucky numbers, | ||
Take the set of integers | ||
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,…… | ||
First, delete every second number, we get following reduced set. | ||
1, 3, 5, 7, 9, 11, 13, 15, 17, 19,………… | ||
Now, delete every third number, we get | ||
1, 3, 7, 9, 13, 15, 19,….…. | ||
Continue this process indefinitely…… | ||
Any number that does NOT get deleted due to above process is called “lucky”. | ||
Example 1: | ||
Input: | ||
N = 5 | ||
Output: 0 | ||
Explanation: 5 is not a lucky number | ||
as it gets deleted in the second | ||
iteration. | ||
Example 2: | ||
Input: | ||
N = 19 | ||
Output: 1 | ||
Explanation: 19 is a lucky number | ||
Your Task: | ||
You don't need to read input or print anything. You only need to complete the function isLucky() that takes N as parameter and returns either False if the N is not lucky else True. | ||
Expected Time Complexity: O(sqrt(N)). | ||
Expected Auxiliary Space: O(sqrt(N)). | ||
Constraints: | ||
1 <= N <= 105 | ||
*/ | ||
// Driver Code Starts | ||
#include <stdio.h> | ||
#include <stdbool.h> | ||
// Driver Code Ends | ||
|
||
// Complete the function | ||
// n: Input n | ||
// Return true if the given number is a lucky number else return False | ||
|
||
bool isLucky(int n) { | ||
int l=2; | ||
while (l<=n){ | ||
if(n%l==0)return 0; | ||
n=n-n/l++; | ||
}// code here | ||
return 1; | ||
} | ||
|
||
// Driver Code Starts. | ||
|
||
int main(){ | ||
int T; | ||
scanf("%d", &T); | ||
while(T--){ | ||
int n; | ||
scanf("%d", &n); | ||
//calling isLucky() function | ||
if(isLucky(n)) | ||
printf("1\n");//printing "1" if isLucky() returns true | ||
else | ||
printf("0\n");//printing "0" if isLucky() returns false | ||
} | ||
|
||
} // 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,76 @@ | ||
/** | ||
3.Final Destination | ||
Consider a 2d plane and a Robot which is located at (0,0) who can move only one unit step at a time in any direction i.e. if its initial position is (x,y), he can go to positions (x + 1, y), (x - 1, y), (x, y + 1) or (x, y - 1). Now Given three integers a,b (denoting the final position where the robot has to reach), and x. Find out if the Robot can reach the final position in exactly x steps. | ||
Example 1: | ||
Input: | ||
a = 5, b = 5, x = 11 | ||
Output: | ||
0 | ||
Explanation: | ||
No matter how the Robot moves, | ||
the Robot won't be able to reach | ||
point (5,5) in exactly 11 moves. | ||
Example 2: | ||
Input: | ||
a = 10, b = 15, x = 25 | ||
Output: | ||
1 | ||
Explanation: | ||
The Robot can move 10 times towards | ||
positive x-axis and then 15 times | ||
towards positive y-axis to reach (10,15). | ||
Your Task: | ||
You don't need to read input or print anything. Your task is to complete the function canReach() which takes 3 Integers a,b and x as input and returns the answer. | ||
Expected Time Complexity: O(1) | ||
Expected Auxiliary Space: O(1) | ||
Constraints: | ||
-109 <= a,b <= 109 | ||
1 <= x <= 2*109 | ||
**/ | ||
|
||
// { Driver Code Starts | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
// } Driver Code Ends | ||
class Solution { | ||
public: | ||
int canReach(long long a, long long b, long long x) { | ||
// code here | ||
a= abs(a); | ||
b= abs(b); | ||
long long c= a+b; | ||
|
||
if(a==0 && b==0 && x%2!=0){ return 0;} | ||
if(c>x){ return 0;} | ||
if(c==x){ return 1;} | ||
if(c<x){ return (((x-c)%2)?0:1);} | ||
} | ||
}; | ||
|
||
// { Driver Code Starts. | ||
int main() { | ||
int t; | ||
cin >> t; | ||
while (t--) { | ||
long long a,b,x; | ||
|
||
cin>>a>>b>>x; | ||
|
||
Solution ob; | ||
cout << ob.canReach(a,b,x) << 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,67 @@ | ||
/** | ||
4. Number of paths | ||
The problem is to count all the possible paths from top left to bottom right of a MxN matrix with the constraints that from each cell you can either move to right or down. | ||
Example 1: | ||
Input: | ||
M = 3 and N = 3 | ||
Output: 6 | ||
Explanation: | ||
Let the given input 3*3 matrix is filled | ||
as such: | ||
A B C | ||
D E F | ||
G H I | ||
The possible paths which exists to reach | ||
'I' from 'A' following above conditions | ||
are as follows:ABCFI, ABEHI, ADGHI, ADEFI, | ||
ADEHI, ABEFI | ||
Example 2: | ||
Input: | ||
M = 2 and N = 8 | ||
Output: 8 | ||
Your Task: | ||
You don't need to read input or print anything. Your task is to complete the function numberOfPaths() which takes the integer M and integer N as input parameters and returns the number of paths.. | ||
Expected Time Complexity: O(m + n - 1)) | ||
Expected Auxiliary Space: O(1) | ||
Constraints: | ||
1 ≤ M, N ≤ 10 | ||
**/ | ||
// { Driver Code Starts | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
|
||
// } Driver Code Ends | ||
long long numberOfPaths(int m, int n) | ||
{ | ||
// Code Here | ||
if (m == 1 || n == 1) | ||
return 1; | ||
|
||
return numberOfPaths(m - 1, n) + numberOfPaths(m, n - 1); | ||
} | ||
// { Driver Code Starts. | ||
|
||
|
||
int main() | ||
{ | ||
int t; | ||
cin>>t; | ||
while(t--) | ||
{ | ||
int n,m; | ||
cin>>m>>n; | ||
cout << numberOfPaths(m, 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,20 @@ | ||
|
||
|
||
int matSearch (int N, int M, int matrix[][M], int x) | ||
{ | ||
if (x<matrix[0][0])return 0; | ||
for(int i=0;i<N;i++){ | ||
if(x<matrix[i][0])return 0; | ||
if(x>=matrix[i][0] && i==N-1){ | ||
for(int j=0;j<M;j++) { | ||
if(matrix[i][j]==x)return 1; | ||
} | ||
} | ||
if(x>=matrix[i][0] && x<matrix[i+1][0]){ | ||
for(int j=0;j<M;j++) { | ||
if(matrix[i][j]==x)return 1; | ||
} | ||
} | ||
}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,52 @@ | ||
/* | ||
Here we have created a function that returns sum of two integers. | ||
Without using any of the arithmetic operators (+, ++, –, -, .. etc). | ||
Sum of two bits can be obtained by performing XOR (^) of the two bits. | ||
Carry bits can be obtained by performing AND (&) of two bits. | ||
This approach is a simple Half Adder logic that can be used to add 2 single bits, but we can extend this logic for integers. | ||
If x and y don’t have set bits at same position(s), then bitwise XOR (^) of x and y gives the sum of x and y. | ||
To incorporate common set bits also, bitwise AND (&) is used. Bitwise AND of x and y gives all carry bits. | ||
We calculate (x & y) << 1 and add it to x ^ y to get the required result. | ||
Example: | ||
Input: 5,6 | ||
Output: 11 | ||
below is the c++ implementation for the above mentioned question | ||
*/ | ||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
int Add(int x, int y) | ||
{ | ||
while (y != 0) // Iterating untill there is no carry | ||
{ | ||
// carry should be unsigned to | ||
// deal with -ve numbers | ||
// carry now contains common | ||
//set bits of x and y | ||
|
||
unsigned carry = x & y; | ||
|
||
// Sum of bits of x and y where at | ||
//least one of the bits is not set | ||
|
||
x = x ^ y; | ||
|
||
// Carry is shifted by one so that adding | ||
// it to x gives the required sum | ||
|
||
y = carry << 1; | ||
} | ||
return x; | ||
} | ||
|
||
// Driver code | ||
int main() | ||
{ | ||
cout << Add(15, 32); | ||
return 0; | ||
} |
Oops, something went wrong.