Skip to content

Commit 5104af1

Browse files
committed
added question descriptions and additional programs and modified folder structure
1 parent 8c70071 commit 5104af1

23 files changed

+140
-95
lines changed

2D Arrays/matrix_rotation.png

-5.13 KB
Binary file not shown.

2D Arrays/matrix_spiral .png

-8.04 KB
Binary file not shown.

Arrays/CommonElements.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
// Finding common elements between two arrays in which the elements are distinct.
2-
// Eg:
1+
/*Finding common elements between two arrays in which the elements are distinct.
2+
Eg:
33
4-
// 1 2 3 4 5 12 8
5-
// 6 7 3 4 2 9 11
4+
1 2 3 4 5 12 8
5+
6 7 3 4 2 9 11
66
7-
// 1 2 3 4 5 8 12
8-
// 2 3 4 6 7 9 11
9-
10-
// O/P:
11-
// 2 3 4
7+
1 2 3 4 5 8 12
8+
2 3 4 6 7 9 11
129
10+
O/P:
11+
2 3 4
12+
*/
1313
#include<iostream>
1414
#include<vector>
1515
#include<algorithm>

Arrays/CommonElementsOptimal.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
/*Finding common elements between two arrays in which the elements are distinct.
2+
Eg:
3+
4+
1 2 3 4 5 12 8
5+
6 7 3 4 2 9 11
6+
7+
1 2 3 4 5 8 12
8+
2 3 4 6 7 9 11
9+
10+
O/P:
11+
2 3 4
12+
*/
113
#include<algorithm>
214
#include<iostream>
315
#include<vector>

Arrays/kadane's_algorithm.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
/*
2+
Given an array arr of N integers. Find the contiguous sub-array with maximum sum.
3+
4+
For Example:
5+
6+
Input:
7+
N = 5
8+
arr[] = {1,2,3,-2,5}
9+
Output: 9
10+
*/
111
#include<stdio.h>
212
#include<limits.h>
313

Backtracking/eightQueen.c

Lines changed: 51 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,77 @@
1-
#include<stdio.h>
1+
/*
2+
The eight queens problem is the problem of placing eight queens on an 8×8 chessboard such that none of them attack one another
3+
(no two are in the same row, column, or diagonal). More generally, the n queens problem places n queens on an n×n chessboard.
4+
*/
5+
#include <stdio.h>
26
void solutionFinder(int tot_queens);
3-
void NQueenSolver(int queenPositions[],int tot_queens,int queenNum);
4-
int isSafe(int queenPositions[],int tot_queens,int choice,int queenNum);
7+
void NQueenSolver(int queenPositions[], int tot_queens, int queenNum);
8+
int isSafe(int queenPositions[], int tot_queens, int choice, int queenNum);
59
#define SAFE 1
610
#define UNSAFE 0
7-
void printBoard(int queenPositions[],int tot_queens);
11+
void printBoard(int queenPositions[], int tot_queens);
812
int main()
913
{
1014
solutionFinder(8);
1115
return 0;
1216
}
1317
void solutionFinder(int tot_queens)
1418
{
15-
int queenPositions[tot_queens];
16-
int ind;
17-
for(ind=0; ind< tot_queens; ind++)
18-
queenPositions[ind]=-1;//no queens placed at start
19-
NQueenSolver(queenPositions,tot_queens,0);
20-
19+
int queenPositions[tot_queens];
20+
int ind;
21+
for (ind = 0; ind < tot_queens; ind++)
22+
queenPositions[ind] = -1; //no queens placed at start
23+
NQueenSolver(queenPositions, tot_queens, 0);
2124
}
22-
void NQueenSolver(int queenPositions[],int tot_queens,int queenNum)
25+
void NQueenSolver(int queenPositions[], int tot_queens, int queenNum)
2326
{
2427
//choices are 0 to tot_queens-1
2528
int choice;
26-
if(tot_queens==queenNum)//
29+
if (tot_queens == queenNum) //
2730
{
28-
printBoard(queenPositions,tot_queens);
29-
getch();
30-
return;
31+
printBoard(queenPositions, tot_queens);
32+
getch();
33+
return;
3134
}
32-
for(choice=0;choice<tot_queens;choice++)
35+
for (choice = 0; choice < tot_queens; choice++)
3336
{
34-
if(isSafe(queenPositions,tot_queens,choice,queenNum)==SAFE)
37+
if (isSafe(queenPositions, tot_queens, choice, queenNum) == SAFE)
3538
{
3639
//do certain things
37-
queenPositions[queenNum]=choice;
38-
NQueenSolver(queenPositions,tot_queens,queenNum+1);
39-
//undo
40-
queenPositions[queenNum]=-1;
40+
queenPositions[queenNum] = choice;
41+
NQueenSolver(queenPositions, tot_queens, queenNum + 1);
42+
//undo
43+
queenPositions[queenNum] = -1;
4144
}
4245
}
4346
}
44-
int isSafe(int queenPositions[],int tot_queens,int choice,int queenNum)
47+
int isSafe(int queenPositions[], int tot_queens, int choice, int queenNum)
4548
{
46-
int row,col;
47-
//left top
48-
for(row=queenNum-1,col=choice-1;row>=0 && col>=0 ;row--,col--)
49-
if(queenPositions[row]==col)
50-
return UNSAFE;
51-
//top
52-
for(row=queenNum-1,col=choice;row>=0 ;row--)
53-
if(queenPositions[row]==col)
54-
return UNSAFE;
55-
//right top
56-
for(row=queenNum-1,col=choice+1;row>=0 && col<tot_queens ;row--,col++)
57-
if(queenPositions[row]==col)
58-
return UNSAFE;
59-
return SAFE;
49+
int row, col;
50+
//left top
51+
for (row = queenNum - 1, col = choice - 1; row >= 0 && col >= 0; row--, col--)
52+
if (queenPositions[row] == col)
53+
return UNSAFE;
54+
//top
55+
for (row = queenNum - 1, col = choice; row >= 0; row--)
56+
if (queenPositions[row] == col)
57+
return UNSAFE;
58+
//right top
59+
for (row = queenNum - 1, col = choice + 1; row >= 0 && col < tot_queens; row--, col++)
60+
if (queenPositions[row] == col)
61+
return UNSAFE;
62+
return SAFE;
6063
}
61-
void printBoard(int queenPositions[],int tot_queens)
64+
void printBoard(int queenPositions[], int tot_queens)
6265
{
63-
int row,col;
64-
for(printf("\n"),row=0;row<tot_queens;row++,printf("\n"))
65-
{
66-
for(col= 0 ;col< tot_queens;col++)
67-
{
68-
if(queenPositions[row]==col)
69-
printf(" Q ");
70-
else
71-
printf(" - ");
72-
}
73-
}
66+
int row, col;
67+
for (printf("\n"), row = 0; row < tot_queens; row++, printf("\n"))
68+
{
69+
for (col = 0; col < tot_queens; col++)
70+
{
71+
if (queenPositions[row] == col)
72+
printf(" Q ");
73+
else
74+
printf(" - ");
75+
}
76+
}
7477
}

Data Structures/Linked Lists/SLL.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/*
2+
An SLL is a type of linked list in which each node consists of only two fields viz., data field and the address field.
3+
The address field is a pointer containing the address of the next node in the list.
4+
*/
15
#include<iostream>
26

37
using namespace std;

Data Structures/Stack/BalancedParantheses.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/*
2+
Given an expression string exp, write a program to examine whether the pairs and the orders of “{“, “}”,
3+
“(“, “)”, “[“, “]” are correct in exp.
4+
*/
15
#include <bits/stdc++.h>
26
using namespace std;
37
bool isBalanced(string);

Divide and Conquer/DCPower.class

-724 Bytes
Binary file not shown.
-1.2 KB
Binary file not shown.

0 commit comments

Comments
 (0)