Skip to content

Commit 784d25e

Browse files
authored
Merge pull request larissalages#131 from d2Anubis/master
added interview bit
2 parents ab2339a + 042cdff commit 784d25e

22 files changed

+337
-0
lines changed

InterviewBit/All_Factors.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
vector<int> Solution::allFactors(int A) {
2+
vector<int>v;
3+
for(int i=1;i<=sqrt(A);i++)
4+
{
5+
if(A%i==0)
6+
{
7+
v.push_back(i);
8+
if(i!=sqrt(A))
9+
{
10+
v.push_back(A/i);
11+
}
12+
}
13+
}
14+
sort(v.begin(),v.end());
15+
return v;
16+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
string Solution::findDigitsInBinary(int A) {
2+
if(A==0)
3+
{
4+
return "0";
5+
}
6+
string num;
7+
while(A>0)
8+
{
9+
string i=to_string(A%2);
10+
num+=i;
11+
A=A/2;
12+
}
13+
reverse(num.begin(),num.end());
14+
return num;
15+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
int Solution::repeatedNumber(const vector<int> &A) {
2+
int res=0;
3+
int n=A.size();
4+
for(int i=0;i<n;i++)
5+
{
6+
res=res^A[i];
7+
}
8+
for(int i=1;i<=n-1;i++)
9+
{
10+
res=res^i;
11+
}
12+
if(res>0)
13+
{
14+
return res;
15+
}
16+
return -1;
17+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
int Solution::firstMissingPositive(vector<int> &A) {
2+
sort(A.begin(),A.end());
3+
int m=0;
4+
while(A[m]<1)
5+
{
6+
m+=1;
7+
}
8+
if(A[m]!=1)
9+
{
10+
return 1;
11+
}
12+
int num=A[m];
13+
for(int i=m+1;i<A.size();i++)
14+
{
15+
if(A[i]!=(num+1))
16+
{
17+
return num+1;
18+
}
19+
num+=1;
20+
}
21+
return num+1;
22+
}

InterviewBit/FizzBuzz.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
vector<string> Solution::fizzBuzz(int A) {
2+
vector<string>v;
3+
for(int i=1;i<=A;i++)
4+
{
5+
if(i%15==0)
6+
{
7+
v.push_back("FizzBuzz");
8+
}
9+
else
10+
{
11+
if(i%5==0)
12+
{
13+
v.push_back("Buzz");
14+
}
15+
else
16+
{
17+
if(i%3==0)
18+
{
19+
v.push_back("Fizz");
20+
}
21+
else
22+
{
23+
24+
v.push_back(to_string(i));
25+
}
26+
27+
}
28+
}
29+
}
30+
return v;
31+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
int Solution::gcd(int A, int B) {
2+
if(B==0)
3+
{
4+
return A;
5+
}
6+
else
7+
{
8+
return gcd(B,A%B);
9+
}
10+
}

InterviewBit/Large_Factorial.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
string Solution::solve(int A) {
2+
string res;
3+
int arr[10000];
4+
arr[0]=1;
5+
int carry=0;
6+
int num;
7+
int arr_size=1;
8+
for(int i=2;i<=A;i++)
9+
{
10+
for(int j=0;j<arr_size;j++)
11+
{
12+
num=arr[j]*i+carry;
13+
arr[j]=num%10;
14+
carry=num/10;
15+
}
16+
while(carry>0)
17+
{
18+
arr[arr_size++]=carry%10;
19+
carry=carry/10;
20+
}
21+
}
22+
for(int i = arr_size-1; i >= 0 ; i--)
23+
res+=to_string(arr[i]);
24+
return res;
25+
}

InterviewBit/Matrix_Median.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
int Solution::findMedian(vector<vector<int> > &A) {
2+
for(int i=A.size()-1;i>0;i--){
3+
for(int j=0;j<A[i].size();j++) A[0].push_back(A[i][j]);
4+
A[i].clear();
5+
}
6+
sort(A[0].begin(),A[0].end());
7+
int n=A[0].size();
8+
if(n%2==0)
9+
{
10+
return (A[0][n/2]+A[0][(n/2)-1]);
11+
}
12+
return A[0][(n/2)];
13+
}

InterviewBit/Max_Min.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
int Solution::solve(vector<int> &A) {
2+
sort(A.begin(),A.end());
3+
int s=A.size();
4+
int sum=A[0]+A[s-1];
5+
return sum;
6+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
int Solution::maxSubArray(const vector<int> &A) {
2+
3+
int max_so_far = INT_MIN, max_ending_here = 0;
4+
5+
for (int i = 0; i < A.size(); i++)
6+
{
7+
max_ending_here = max_ending_here + A[i];
8+
if (max_so_far < max_ending_here)
9+
max_so_far = max_ending_here;
10+
11+
if (max_ending_here < 0)
12+
max_ending_here = 0;
13+
}
14+
return max_so_far;
15+
}

0 commit comments

Comments
 (0)