Skip to content

Commit ad7f007

Browse files
authored
Merge pull request ashutosh97#98 from Muskan-goyal6/new-branch
Subsequences in C++
2 parents ed42806 + 28ed88b commit ad7f007

File tree

7 files changed

+224
-0
lines changed

7 files changed

+224
-0
lines changed

.DS_Store

12 KB
Binary file not shown.

Mersort_In_C++/mergeSort.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
2+
#include "../helper.h"
3+
#include <iostream>
4+
using namespace std;
5+
6+
void copyArr(int from[], int si, int ei, int * to) {
7+
// while (si <= ei) *to++ = *si++;
8+
while (si <= ei) {
9+
*to = from[si];
10+
++si;
11+
++to;
12+
}
13+
}
14+
15+
16+
void mergeSortedArray(int arr[], int si, int ei, int mid) {
17+
18+
int tmp_A[100];
19+
int tmp_B[100];
20+
21+
copyArr(arr, si, mid, tmp_A);
22+
copyArr(arr, mid + 1, ei, tmp_B);
23+
24+
int i = 0;
25+
int j = 0;
26+
int k = si;
27+
28+
//while a has elements and b has elements, I have to do something
29+
int size_A = mid - si + 1;
30+
int size_B = ei - (mid + 1) + 1;
31+
32+
while (i < size_A && j < size_B) {
33+
if (tmp_A[i] < tmp_B[j]) {
34+
arr[k] = tmp_A[i];
35+
i++;
36+
k++;
37+
}
38+
else {
39+
arr[k++] = tmp_B[j++];
40+
}
41+
}
42+
43+
while (i < size_A) {
44+
arr[k++] = tmp_A[i++];
45+
}
46+
47+
while (j < size_B) arr[k++] = tmp_B[j++];
48+
49+
50+
}
51+
52+
53+
void mergeSort(int arr[], int si, int ei) {
54+
if (si >= ei) {
55+
//no elements
56+
return;
57+
}
58+
59+
int mid = (si + ei) / 2;
60+
//sort the left part
61+
mergeSort(arr, si, mid);
62+
mergeSort(arr, mid + 1, ei);
63+
64+
mergeSortedArray(arr, si, ei, mid);
65+
66+
}
67+
68+
69+
int main() {
70+
int arr[100];
71+
int n;
72+
cin >> n;
73+
inputArr(arr, n);
74+
75+
mergeSort(arr, 0, n - 1);
76+
77+
printArr(arr, n);
78+
}

Mersort_In_C++/mergesort.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Merge Sort is a Divide and Conquer algorithm. It divides input array in
2+
two halves, calls itself for the two halves and then merges the two sorted halves.
3+
Algorithm:
4+
MergeSort(arr[], l, r)
5+
If r > l
6+
1. Find the middle point to divide the array into two halves:
7+
middle m = (l+r)/2
8+
2. Call mergeSort for first half:
9+
Call mergeSort(arr, l, m)
10+
3. Call mergeSort for second half:
11+
Call mergeSort(arr, m+1, r)
12+
4. Merge the two halves sorted in step 2 and 3:
13+
Call merge(arr, l, m, r)

Subsequences/question.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Print all the subsequences of a string. A String is a subsequence of a given String,
2+
that is generated by deleting some character of a given string without changing
3+
its order.
4+
Example:
5+
Input : abc
6+
Output : a, b, c, ab, bc, ac, abc

Subsequences/subsequences.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//find all the sub sequences of a string
2+
#include <iostream>
3+
using namespace std;
4+
char output[100] = "";
5+
6+
void printSubSeq(char str[], int be, int idx)
7+
{
8+
if (str[be] == '\0')
9+
{
10+
output[idx] = '\0';
11+
cout << output << endl;
12+
return;
13+
}
14+
15+
printSubSeq(str, be + 1, idx);
16+
output[idx] = str[be];
17+
printSubSeq(str, be + 1, idx + 1);
18+
}
19+
20+
21+
int main() {
22+
char str[100];
23+
cin >> str;
24+
25+
26+
printSubSeq(str, 0, 0);
27+
}

nQueen/Question.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
The N Queen is the problem of placing N chess queens on an N×N chessboard so
2+
that no two queens attack each other. For example, following is a solution
3+
for 4 Queen problem.
4+
The expected output is a binary matrix which has Qs for the blocks where
5+
queens are placed and Xs where queens are not placed.
6+
EXAMPLE:
7+
X X Q X
8+
Q X X X
9+
X X X Q
10+
X Q X X

nQueen/nQueen.cpp

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
2+
#include <iostream>
3+
using namespace std;
4+
5+
char board[30][30];
6+
7+
void printBoard(char arr[][30], int n) {
8+
for (int r = 0; r < n; ++r) {
9+
for (int c = 0; c < n; ++c) {
10+
cout << arr[r][c] << " " ;
11+
}
12+
cout << endl;
13+
}
14+
}
15+
16+
void initialiseBoard(int n) {
17+
for (int r = 0; r < n; ++r) {
18+
for (int c = 0; c < n; ++c) {
19+
board[r][c] = 'X';
20+
}
21+
}
22+
}
23+
24+
bool canPlace(char board[][30], int N, int x, int y)
25+
{
26+
//check the row if aqueen already exists
27+
for (int r = 0; r < N; ++r)
28+
{
29+
if (board[r][y] == 'Q')
30+
return false;
31+
}
32+
33+
int rInc[] = { -1, +1, +1, -1};
34+
int cInc[] = { -1, +1, -1, +1};
35+
//check diagonal if queen already exists
36+
for (int dir = 0; dir < 4; ++dir)
37+
{
38+
int rowAdd = rInc[dir];
39+
int colAdd = cInc[dir];
40+
int r = x + rowAdd;
41+
int c = y + colAdd;
42+
//check that r c is within the board
43+
while (r >= 0 && r < N && c >= 0 && c < N)
44+
{
45+
if (board[r][c] == 'Q')
46+
return false;
47+
r = r + rowAdd;
48+
c = c + colAdd;
49+
}
50+
}
51+
return true;
52+
}
53+
54+
bool nqueen(int r, int n)
55+
{
56+
if (r == n)//base case if all queens are placed
57+
{
58+
return true;
59+
}
60+
//for every column, use hit and trial by placing a queen in the each cell of curr Row
61+
for (int c = 0; c < n; ++c)
62+
{
63+
int x = r;
64+
int y = c;
65+
//check if queen can be placed on board[i][c]
66+
if (canPlace(board, n, x, y)==true)
67+
{
68+
board[x][y] = 'Q'; //place queen in each column
69+
bool isSuccessful = nqueen(r + 1, n); //recursion to place rest of queens
70+
if (isSuccessful == true)
71+
return true;
72+
board[x][y] = 'X'; //else unmark the cell or backtrack
73+
}
74+
}
75+
return false; //if queen cannot be placed in any row in this column then return false
76+
}
77+
78+
int main()
79+
{
80+
int n;
81+
cin >> n;
82+
83+
initialiseBoard(n);//initialse all the board with X
84+
bool isSuccessful = nqueen(0, n);
85+
86+
if (isSuccessful) printBoard(board, n);
87+
else cout << "Sorry man! You need to have a larger board!\n";
88+
89+
return 0;
90+
}

0 commit comments

Comments
 (0)