Skip to content

Commit 76fe09c

Browse files
committed
Day-54 2 CTCI problems 2
1 parent 665df76 commit 76fe09c

File tree

3 files changed

+140
-4
lines changed

3 files changed

+140
-4
lines changed

README.md

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
| Current Status| Stats |
66
| :------------: | :----------: |
7-
| Total Problems | 73 |
8-
| Current Streak | 53 |
9-
| Longest Streak | 53 ( August 17, 2015 - October 8, 2015 ) |
7+
| Total Problems | 75 |
8+
| Current Streak | 54 |
9+
| Longest Streak | 54 ( August 17, 2015 - October 9, 2015 ) |
1010

1111
</center>
1212

@@ -64,7 +64,21 @@ Include contains single header implementation of data structures and some algori
6464
| How many bit flip operation would require to convert number A to B. | [countNumberOfBitFlips.cpp](bit_manipulation/countNumberOfBitFlips.cpp)|
6565
| Given a number x and two positions (from right side) in binary representation of x, write a function that swaps n right bits at given two positions and returns the result. It is also given that the two sets of bits do not overlap.|[swapSetOfBits.cpp](bit_manipulation/swapSetOfBits.cpp)|
6666
| Add two numbers without using any arithmetic operators | [addition_without_operators.cpp](bit_manipulation/addition_without_operators.cpp)
67-
| Louise and Richard play a game. They have a counter set to N. Louise gets the first turn and the turns alternate thereafter. In the game, they perform the following operations: <ul><li>If N is not a power of 2, reduce the counter by the largest power of 2 less than N.</li></ul><ul><li>If N is a power of 2, reduce the counter by half of N.</li></ul> The resultant value is the new N which is again used for subsequent operations.The game ends when the counter reduces to 1, i.e., N == 1, and the last person to make a valid move wins. <ul><li> Given N, your task is to find the winner of the game. If they set counter to 1, Richard wins, because its Louise' turn and she cannot make a move.</li></ul><ul><li>Input Format : -The first line contains an integer T, the number of testcases. T lines follow. Each line contains N, the initial number set in the counter.|[counter_game.cpp](bit_manipulation/counter_game.cpp)|
67+
| Louise and Richard play a game. They have a counter set to N. Louise gets the first turn and the turns alternate thereafter. In the game, they perform the following operations.
68+
69+
If N is not a power of 2, reduce the counter by the largest power of 2 less than N.
70+
If N is a power of 2, reduce the counter by half of N.
71+
The resultant value is the new N which is again used for subsequent operations.
72+
The game ends when the counter reduces to 1, i.e., N == 1, and the last person to make a valid move wins.
73+
74+
Given N, your task is to find the winner of the game.
75+
76+
If they set counter to 1, Richard wins, because its Louise' turn and she cannot make a move.
77+
78+
Input Format
79+
The first line contains an integer T, the number of testcases.
80+
T lines follow. Each line contains N, the initial number set in the counter.
81+
|[counter_game.cpp](bit_manipulation/counter_game.cpp)|
6882

6983
### Cracking the coding interview problems
7084
| Problem | Solution |
@@ -76,6 +90,8 @@ Include contains single header implementation of data structures and some algori
7690
| Problem 3 : Edition 6: URLify: Replace all the spaces in a string with '%20'. Preferebly Inplace |[1-3-URLify.cpp](cracking_the_coding_interview_problems/1-3-URLify.cpp)|
7791
| Problem 4 : Edition 6: Given a string, write a function to check if it is a permutation of a pallindrome.|[1-4-pallindrome-permutations.cpp ](cracking_the_coding_interview_problems/1-4-pallindrome-permutations.cpp)|
7892
| Problem 5 : Edition 6: There are three possible edits that can be performed on a string - Insert a char, Delete a char, Replace a char. Given two strings, determine if they are one or 0 edit away.|[1-5-one-edit-away.cpp ](cracking_the_coding_interview_problems/1-5-one-edit-away.cpp)|
93+
| Problem 6: Implement a method to perform basic string compression. Example string **aabcccccaaa** should be compressed to **a2b1c5a3**, however if compressed string is bigger than original string, return original string| [1-6-string-compression.cpp](cracking_the_coding_interview_problems/1-6-string-compression.cpp)|
94+
| Problem 7: Rotate the matrix clockwise( & anticlockwise) by 90 degrees| [1-7-matrix-rotation.cpp](cracking_the_coding_interview_problems/1-7-matrix-rotation.cpp)|
7995

8096

8197
### Tree Problems
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <iostream>
2+
#include <string>
3+
4+
5+
std::string compress(std::string str)
6+
{
7+
size_t original_length = str.length();
8+
if (original_length < 2) {
9+
return str;
10+
}
11+
std::string out{""};
12+
int count = 1;
13+
for( size_t i = 1; i < original_length; ++i ) {
14+
if (str[i-1] == str[i]) {
15+
++count;
16+
} else {
17+
out += str[i-1];
18+
out += std::to_string(count);
19+
count = 1;
20+
}
21+
if (out.length() >= original_length) {
22+
return str;
23+
}
24+
}
25+
out += str[original_length-1];
26+
out += std::to_string(count);
27+
if (out.length() >= original_length) {
28+
return str;
29+
}
30+
return out;
31+
}
32+
33+
int main()
34+
{
35+
std::string str, out;
36+
std::cout << "Enter a string:\n";
37+
std::cin >> str;
38+
out = compress(str);
39+
if (str.compare(out)) {
40+
std::cout << str << " can be compressed to " << out << std::endl;
41+
} else {
42+
std::cout << str << " can not be compressed\n";
43+
}
44+
return 0;
45+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include<iostream>
2+
3+
void helper_transpose(int **matrix, int N)
4+
{
5+
for( int i = 0; i < N; ++i ) {
6+
for( int j = i+1; j < N; ++j ) {
7+
if ( i != j ) {
8+
std::swap(matrix[i][j], matrix[j][i]);
9+
}
10+
}
11+
}
12+
}
13+
14+
void helper_reverse( int * row, int N ) {
15+
for ( int i = 0; i < N/2; ++i ) {
16+
std::swap(row[i], row[N-i-1]);
17+
}
18+
}
19+
20+
void rotate1(int ** matrix, int N) {
21+
//transpose matrix
22+
helper_transpose(matrix, N);
23+
// reverse each row
24+
for ( int i = 0; i < N; ++i ) {
25+
helper_reverse(matrix[i], N);
26+
}
27+
}
28+
29+
30+
void rotate2( int ** matrix, int N ) {
31+
for( int i = 0; i < N/2; ++i ) {
32+
for( int j = i; j < N-i-1; ++j ) {
33+
int temp = matrix[i][j];
34+
matrix[i][j] = matrix[j][N-i-1];
35+
matrix[j][N-i-1] = matrix[N-i-1][N-j-1];
36+
matrix[N-i-1][N-j-1]= matrix[N-j-1][i];
37+
matrix[N-j-1][i] = temp;
38+
}
39+
}
40+
}
41+
42+
void printMatrix( int ** matrix, int N) {
43+
for ( int i = 0; i < N; ++i ) {
44+
for( int j = 0; j < N; ++j ) {
45+
std::cout << matrix[i][j] << " ";
46+
}
47+
std::cout << std::endl;
48+
}
49+
}
50+
51+
52+
int main() {
53+
int N;
54+
std::cout << "Enter N for NxN matrix:";
55+
std::cin >> N;
56+
int ** matrix = new int*[N];
57+
for ( int i = 0; i < N; ++i ) {
58+
matrix[i] = new int[N];
59+
}
60+
61+
for ( int i = 0; i < N; ++i) {
62+
for ( int j = 0; j < N; ++j ) {
63+
std::cin >> matrix[i][j];
64+
}
65+
}
66+
67+
std::cout << "Rotated matrix by 90 (clockwise):\n";
68+
rotate1(matrix, N);
69+
printMatrix(matrix, N);
70+
71+
std::cout << "Rotated matrix again by 90(anticlockwise):\n";
72+
rotate2(matrix, N);
73+
printMatrix(matrix, N);
74+
return 0;
75+
}

0 commit comments

Comments
 (0)