Skip to content

Commit 5883945

Browse files
committed
added interview bit
1 parent 07255ea commit 5883945

10 files changed

+142
-0
lines changed

InterviewBit/Find Duplicate in Array

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+
}

InterviewBit/FizzBuzz

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+
}

InterviewBit/Greatest Common Divisor

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/Max Min

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+
}

InterviewBit/Maximum Consecutive Gap

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
int Solution::maximumGap(const vector<int> &A) {
2+
vector<int>v=A;
3+
int maxm=0;
4+
sort(v.begin(),v.end());
5+
for(int i=0;i<v.size()-1;i++)
6+
{
7+
int diff=v[i+1]-v[i];
8+
maxm=max(diff,maxm);
9+
}
10+
return maxm;
11+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
int Solution::coverPoints(vector<int> &A, vector<int> &B) {
2+
int count=0;
3+
for(int i=0;i<A.size()-1;i++)
4+
{
5+
count+=max(abs(A[i]-A[i+1]),abs(B[i]-B[i+1]));
6+
}
7+
return count;
8+
}

InterviewBit/Power Of Two Integers

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
int Solution::isPower(int A)
2+
{
3+
if(A==1) return 1;
4+
for(int i=2;i<=sqrt(A);i++)
5+
{
6+
float a=log(A)/log(i);
7+
if(floor(a)==a)
8+
return 1;
9+
}
10+
return 0;
11+
}

InterviewBit/Prime Sum

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
bool isPrime(int a)
2+
{
3+
for(int i=2;i<=sqrt(a);i++)
4+
{
5+
if(a%i==0)
6+
{
7+
return false;
8+
}
9+
}
10+
return true;
11+
}
12+
13+
vector<int> Solution::primesum(int A) {
14+
vector<int>v;
15+
for(int i=2;i<=A/2;i++)
16+
{
17+
if(isPrime(i)&&isPrime(A-i))
18+
{
19+
v.push_back(i);
20+
v.push_back(A-i);
21+
return v;
22+
}
23+
}
24+
}

InterviewBit/Sort array with squares!

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
vector<int> Solution::solve(vector<int> &A) {
2+
vector<int>v;
3+
for(int i=0;i<A.size();i++)
4+
{
5+
v.push_back(A[i]*A[i]);
6+
}
7+
sort(v.begin(),v.end());
8+
return v;
9+
}

0 commit comments

Comments
 (0)