Skip to content

Commit 8edc364

Browse files
authored
Merge branch 'smv1999:master' into master
2 parents 596d0f0 + eaa02af commit 8edc364

File tree

13 files changed

+941
-5
lines changed

13 files changed

+941
-5
lines changed

Backtracking/Knight's_Tour.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*Moving according to the rules of chess knight must visit each square exactly once. Print the order of each the cell in which they are visited.*/
2+
import java.util.*;
3+
public class MyClass {
4+
public static void main(String args[]) {
5+
Scanner sc = new Scanner(System.in);
6+
int n = sc.nextInt();
7+
// provide start position
8+
int r = sc.nextInt();
9+
int c = sc.nextInt();
10+
int board[][] = new int[n][n];
11+
// The 8 possible moves where a knight can move from its current position
12+
int posx[] = {-1,-1,1,1,-2,-2,2,2};
13+
int posy[] = {-2,2,-2,2,-1,1,-1,1};
14+
path(board, r, c, 1, posx, posy);
15+
}
16+
public static boolean path(int board[][], int row, int col, int move, int posx[], int posy[]) {
17+
board[row][col] = move;
18+
// if all the positions are visited then print the result
19+
if (move == board.length*board.length) {
20+
// printing result in matrix form
21+
for (int i = 0; i < board.length; i++) {
22+
for (int j = 0; j < board.length; j++)
23+
if (board[i][j] < 10)
24+
System.out.print("0" + board[i][j] + " ");
25+
else
26+
System.out.print(board[i][j] + " ");
27+
System.out.println();
28+
}
29+
return true;
30+
}
31+
for (int i = 0; i < 8; i++) {
32+
int newx = row + posx[i];
33+
int newy = col + posy[i];
34+
// checking if the new position is a valid move and not visited yet
35+
if (validmove(newx, newy, board) && board[newx][newy] == 0) {
36+
if (path(board, newx, newy, move + 1, posx, posy)) {
37+
return true;
38+
}
39+
}
40+
}
41+
// if position is not valid mark position as unvisited in the matrix
42+
board[row][col] = 0;
43+
return false;
44+
}
45+
// checks that the new position is not outside the matrix of n*n
46+
static boolean validmove(int newx, int newy, int board[][]) {
47+
if (newx >= 0 && newx < board.length && newy >= 0 && newy < board.length)
48+
return true;
49+
return false;
50+
}
51+
}
52+
53+
/*Input
54+
8
55+
0
56+
0
57+
Output
58+
01 08 15 22 03 10 19 64
59+
16 23 02 09 18 21 04 11
60+
07 14 17 24 05 12 63 20
61+
34 25 06 13 44 27 52 61
62+
55 46 33 26 53 62 43 28
63+
32 35 54 45 40 29 60 51
64+
47 56 37 30 49 58 39 42
65+
36 31 48 57 38 41 50 59 */

Backtracking/N-Queen Problem.cpp

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/* N-Queen Problem
2+
3+
4+
You have to place n queens on an nxn chessboard.
5+
Idea: Try all the possible positions for the queen. isSafe functions check whether
6+
the current position is safe or not
7+
8+
*/
9+
10+
#include<iostream>
11+
using namespace std;
12+
13+
14+
bool isSafe(int board[][10], int i, int j, int n){
15+
//Check in col
16+
17+
for(int row=0;row<i;row++){
18+
if(board[row][j] == 1){
19+
return false;
20+
}
21+
}
22+
//Left diagonal
23+
int x=i;
24+
int y=j;
25+
26+
while(x>=0 && y>=0){
27+
if(board[x][y]==1){
28+
return false;
29+
}
30+
x--;
31+
y--;
32+
}
33+
//Right diagonal
34+
x=i;
35+
y=j;
36+
37+
while(x>=0 && y>=0){
38+
if(board[x][y]==1){
39+
return false;
40+
}
41+
x--;
42+
y++;
43+
}
44+
//The position is safe
45+
return true;
46+
47+
48+
}
49+
50+
bool nQueen(int board[][10], int i,int n){
51+
if(i==n){//base case
52+
// Place Queeen in n rows (0..n-1)
53+
//print the board
54+
for(int i=0;i<n;i++){
55+
for(int j=0;j<n;j++){
56+
if(board[i][j]==1){
57+
cout<<"Q ";
58+
}
59+
else{
60+
cout<<"_ ";
61+
}
62+
}
63+
cout<<endl;
64+
}
65+
return true;
66+
}
67+
//Recursive case
68+
69+
for(int j=0;j<n;j++){
70+
//Check i,j th position is safe to place the queen or not.
71+
if(isSafe(board,i,j,n)){
72+
//Place the Queen ,i, j in the right position
73+
board[i][j]=1;
74+
75+
if(nQueen(board,i+1,n)){
76+
return true;
77+
}
78+
//Means i,j is not the right position
79+
board[i][j]=0; //backtracking
80+
}
81+
}
82+
//coudln't place the queen in current position.
83+
return false;
84+
}
85+
86+
87+
int main(){
88+
int n;
89+
cin>>n;
90+
91+
int board[10][10]={0};
92+
nQueen(board,0,n);
93+
94+
return 0;
95+
96+
}
97+
98+
/* Input:
99+
4
100+
101+
Output:
102+
103+
_ Q _ _
104+
_ _ _ Q
105+
Q _ _ _
106+
_ _ Q _
107+
108+
*/

Data Structures/Linked Lists/Singly Linked List/Single-linked-list-operations.py

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ def __init__(self, data=None, next=None): # Creation of Node
66
self.data = data
77
self.next = next
88

9+
910
class LinkedList:
1011
def __init__(self):
1112
self.head = None # head points the first node
@@ -88,13 +89,51 @@ def insert_values(self, data_list):
8889
for data in data_list:
8990
self.insert_at_end(data)
9091

92+
# removing element at linkedlist with Value
93+
def removeval(self, value):
94+
count = 0
95+
temp = self.head
96+
while temp:
97+
if value != temp.data:
98+
count += 1
99+
temp = temp.next
100+
if count == self.length():
101+
print("Value is not present")
102+
103+
else:
104+
if value == self.head.data:
105+
self.head = self.head.next
106+
return
107+
temp = self.head
108+
while temp:
109+
if value == temp.next.data:
110+
temp.next = temp.next.next
111+
break
112+
113+
temp = temp.next
114+
91115

92116
if __name__ == '__main__':
93117
node1 = LinkedList()
94-
node1.insert_values(["python","C++","C","Java"])
95-
node1.insert_at(1,"Javascript")
96-
node1.remove_at(2)
118+
ins = list(input("Enter a values to be inserted by giving space[eg: python c++ java] : ").rstrip().split())
119+
node1.insert_values(ins)
120+
node1.print()
121+
ind = int(input("Enter the index to be added: "))
122+
val = input('Enter the value: ')
123+
node1.insert_at(ind, val)
124+
node1.print()
125+
remm = int(input('Enter the index to be removed: '))
126+
node1.remove_at(remm)
127+
node1.print()
128+
remval = input('Enter the value to be removed: ')
129+
node1.removeval(remval)
130+
node1.print()
131+
inss = list(input("Enter a values to be inserted by giving space[eg: 45 30 22] : ").rstrip().split())
132+
node1.insert_values(inss)
133+
node1. print()
134+
inend = int(input('Enter the number to be inserted at the end: '))
135+
node1.insert_at_end(inend)
97136
node1.print()
98-
node1.insert_values([45,7,12,567,99])
99-
node1.insert_at_end(67)
137+
remval1 = input('Enter the value to be removed: ')
138+
node1.removeval(remval1)
100139
node1.print()
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
3+
Given the head of a singly linked list, group all the nodes with odd indices together followed by the nodes
4+
with even indices, and return the reordered list.
5+
6+
The first node is considered odd, and the second node is even, and so on.
7+
8+
Note that the relative order inside both the even and odd groups should remain as it was in the input.
9+
10+
You must solve the problem in O(1) extra space complexity and O(n) time complexity.
11+
12+
Input: head = [1,2,3,4,5]
13+
Output: [1,3,5,2,4]
14+
15+
Constraints:
16+
n == number of nodes in the linked list
17+
0 <= n <= 104
18+
-106 <= Node.val <= 106
19+
20+
*/
21+
22+
/*
23+
Time Complexity: O(N), where N is the size of the array
24+
Space Complexity: O(1), Constant Space
25+
*/
26+
27+
/*
28+
29+
Given the head of a singly linked list, group all the nodes with odd indices together followed by the nodes
30+
with even indices, and return the reordered list.
31+
32+
The first node is considered odd, and the second node is even, and so on.
33+
34+
Note that the relative order inside both the even and odd groups should remain as it was in the input.
35+
36+
You must solve the problem in O(1) extra space complexity and O(n) time complexity.
37+
38+
Input: head = [1,2,3,4,5]
39+
Output: [1,3,5,2,4]
40+
41+
Constraints:
42+
n == number of nodes in the linked list
43+
0 <= n <= 104
44+
-106 <= Node.val <= 106
45+
46+
*/
47+
48+
/*
49+
Time Complexity: O(N), where N is the size of the array
50+
Space Complexity: O(1), Constant Space
51+
*/
52+
53+
#include <bits/stdc++.h>
54+
using namespace std;
55+
56+
struct ListNode // Linked list node
57+
{
58+
int val;
59+
ListNode *next;
60+
ListNode(int x)
61+
{
62+
val = x;
63+
next = NULL;
64+
}
65+
};
66+
67+
ListNode *oddEvenList(ListNode *head)
68+
{
69+
int len = 1;// variable to store length of linked List
70+
ListNode *end = head;
71+
if (head == NULL)
72+
return NULL;
73+
while (end->next != NULL) // calculation of length
74+
{
75+
end = end->next;
76+
len++;
77+
}
78+
if (len <= 2)
79+
return head;
80+
ListNode *Nend = end; // pointer to point to the new end of the list
81+
ListNode *temp = head;
82+
while (temp != end)
83+
{
84+
ListNode *t = temp->next;
85+
temp->next = t->next; // breaking the link
86+
temp = temp->next;
87+
t->next = NULL;
88+
Nend->next = t; // adding the node to the end
89+
Nend = Nend->next; // updating end
90+
if (t == end)
91+
break;
92+
}
93+
return head;
94+
}
95+
void printList(ListNode *head) // function to print the list
96+
{
97+
ListNode *temp = head;
98+
while (temp != NULL)
99+
{
100+
cout<<temp->val<<" ";
101+
temp = temp->next;
102+
}
103+
cout<<"\n";
104+
}
105+
int main() // driver function
106+
{
107+
int data = 1; // adding the elements to the list
108+
ListNode *head = new ListNode(data);
109+
ListNode *tail = head;
110+
for (int i = 2; i<=5 ; i++)
111+
{
112+
tail->next = new ListNode(i);
113+
tail = tail->next;
114+
}
115+
116+
head = oddEvenList(head);
117+
printList(head);
118+
119+
return 0;
120+
}

0 commit comments

Comments
 (0)