Interview Bit Problem Solving
Interview Bit Problem Solving
1)CHIPS FACTORY:
-------
Example Input
Input 1:
A = [1, 2, 3, 4]
Input 2:
A = [1, 1, 2, 2]
Example Output
Output 1:
4
Output 2:
int n = A.length;
for(int i =1;i<n;i++){
if(A[i] > max){
max = A[i];
cnt++;
}
}
return cnt;
}
}
---------------------
3) Pythagorean Triplets:
Example Input
Input 1:
A = 5
Input 2:
A = 13
Example Output
Output 1:
1
Output 2:
Example Explanation
Explanation 1:
Then only triplet is {3, 4, 5}
Explanation 2:
The triplets are {3, 4, 5}, {6, 8, 10}, {5, 12, 13}.
int count = 0;
----------
4) Diagonal Flip:
Problem Description
Example Input
Input 1:
A = 4
B = [[1, 0],
[0, 1]]
Input 2:
A = [[1, 0],
[1, 0]]
Example Output
Output 1:
[[1, 0],
[0, 1]]
Output 2:
[[1, 1],
[0, 0]]
int n =A.length;
for(int i =0;i<n;i++){
for(int j=0;j<n;j++){
B[i][j] = A[j][i];
}
}
return B;
}
}
--------
5) POsitive negative:
Problem Description
Example Input
Input 1:
A = [1, 2, 3]
Input 2:
A = [1, 0, -1]
Example Output
Output 1:
[3, 0]
Output 2:
[1, 1]
Example Explanation
Explanation 1:
Positive values are [1, 2, 3].
There are no negative values.
Explanation 2:
int pos = 0;
int neg = 0;
}
else if(num < 0){
neg++;
}
}
result[0] = pos;
result[1] = neg;
return result;
}
}
------------
6)
----------
Return an array containing only the occurences with the smallest value's occurence
first.
For example, A = [4, 3, 3], you have to return an array [2, 1], where 2 is the
number of occurences for element 3,
and 1 is the number of occurences for element 4. But, 2 comes first because 3 is
smaller than 4.
---------------
8)Greater of lesser:
Input 1:
A = [1, 2, 3, 4]
B = [5, 6, 7, 8]
C = 4
Output 1:
0
Example Explanation
There are no integers greater than C in A.
There are no integers less than C in B.
int countA = 0;
int countB = 0;
return max;
}
}
---
Input 1:
A = 11
B = 11
Output 1:
1
-----
Digital Root:
Input 1:
A = 99
Output 1:
9
Example Explanation
99 -> 9+9 = 18 -> 1+8 = 9
}
return root;
}
}
--------
Noble Integer :
Input 1:
A = [3, 2, 1, 3]
Output 1:
Explanation 1:
For integer 2, there are 2 greater elements in the array. So, return 1.
Collections.sort(A);
int n = A.size();
for(int i = 0;i<n;i++){
if(A.get(i) >= 0 && A.get(i) == n - i - 1){
if(i == n-1 || A.get(i) < A.get(i+1)){
return 1;
}
}
}
return -1;
}
}
--------
Reverse integer:
You are given an integer N and the task is to reverse the digits of the given
integer. Return 0 if the result overflows and does not fit in a 32 bit signed
integer
Input 1:
x = 123
Input 2:
x = -123
Output 1:
321
Ouput 2:
-321
If the given integer is negative like -123 the output is also negative -321.
if (A < 0) {
isNegative = true;
A = -A;
}
long rev = 0;
while (A > 0) {
int dig = A % 10;
rev = (rev * 10) + dig;
A /= 10;
}
if (isNegative) {
if (rev > Integer.MAX_VALUE)
return 0;
rev = -rev;
} else {
if (rev > Integer.MAX_VALUE || rev < Integer.MIN_VALUE)
return 0;
}
----------