Skip to content

Commit 46da882

Browse files
Merge branch 'smv1999:master' into patch-1
2 parents 45ebfa9 + d47fa0a commit 46da882

34 files changed

+2280
-5
lines changed

Arrays/trappingrainwater.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Given an array of numbers which are heights to the container, we need to find upto what maximum value water can be filled in this vessel
2+
// of different heights.
3+
// Time complexity - O(n)
4+
//Space complexity - O(1)
5+
#include<bits/stdc++.h>
6+
using namespace std;
7+
8+
int main(){
9+
int arr[]={1,8,6,2,5,4,8,3,7}; //array of diff heights
10+
int l=0,n=sizeof(arr)/sizeof(int),r=n-1;
11+
int max_area=0,h,a,b;
12+
//2 pointers algorithm
13+
14+
while(l<r){
15+
h=min(arr[l],arr[r]); //choosing the minimum of both heights -left and right side
16+
b=r-l; //base = right height index - left
17+
a=h*b;
18+
max_area=max(a,max_area); //idea to maximize the area to trap more water
19+
if(arr[l]<=arr[r]){
20+
l++; //if right ride height is greater move left pointer to choose max of heights
21+
}
22+
else{
23+
r--; //else move right pointer to choose next max height
24+
}
25+
}
26+
cout<<"Maximum water that can be trapped is : "<<max_area;
27+
return 0;
28+
29+
}

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+
*/
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/* You're given a number N. Print the first N lines of the below-given pattern.
2+
3+
1 2 3 4 5
4+
10 9 8 7 6
5+
11 12 13 14 15
6+
20 19 18 17 16
7+
21 22 23 24 25
8+
30 29 28 27 26
9+
Input:
10+
First-line will contain the number N.
11+
Output:
12+
Print the first N lines of the given pattern.
13+
14+
Constraints
15+
1≤N≤200
16+
Sample Input 1:
17+
4
18+
Sample Output 1:
19+
1 2 3 4 5
20+
10 9 8 7 6
21+
11 12 13 14 15
22+
20 19 18 17 16
23+
Sample Input 2:
24+
2
25+
Sample Output 2:
26+
1 2 3 4 5
27+
10 9 8 7 6
28+
EXPLANATION:
29+
In the first example, we'll print the first 4 lines of the given pattern.
30+
In the second example, we'll print the first 2 lines of the given pattern. */
31+
32+
import java.util.*;
33+
import java.lang.*;
34+
import java.io.*;
35+
36+
/* Name of the class has to be "Main" only if the class is public. */
37+
class Codechef
38+
{
39+
public static void main (String[] args) throws java.lang.Exception
40+
{
41+
Scanner sc= new Scanner(System.in);
42+
int n= sc.nextInt();
43+
for (int i=1; i<=n; i++)
44+
{
45+
if(i%2!=0)
46+
{
47+
for(int j=(5*(i-1)+1);j<=(5*i);j++)
48+
{
49+
System.out.print(j+" ");
50+
}
51+
System.out.println();
52+
}
53+
else{
54+
for(int j=(5*i);j>=(5*(i-1)+1);j--)
55+
{
56+
System.out.print(j+" ");
57+
}
58+
System.out.println();
59+
}
60+
61+
62+
}
63+
}
64+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/* Chef went to a shop and buys a pens and b pencils. Each pen costs x units and each pencil costs y units. Now find what is the total amount Chef will spend to buy a pens and b pencils.
2+
3+
Input:
4+
First-line will contain 4 space separated integers a, b, x and y respectively.
5+
Output:
6+
Print the answer in a new line.
7+
8+
Constraints
9+
1≤a,b,x,y≤103
10+
Sample Input 1:
11+
2 4 4 5
12+
Sample Output 1:
13+
28
14+
Sample Input 2:
15+
1 1 4 8
16+
Sample Output 2:
17+
12
18+
EXPLANATION:
19+
In the first example, total cost is (2 * 4 + 4 * 5) = 28.
20+
In the second example, total cost is (1 * 4 + 1 * 8) = 12. */
21+
22+
23+
#include <stdio.h>
24+
25+
int main(void) {
26+
27+
int a,b,x,y;
28+
scanf("%d %d %d %d",&a,&b,&x,&y);
29+
int pen_cost= a*x;
30+
int pencil_cost=b*y;
31+
int total=pen_cost+pencil_cost;
32+
printf("%d",total);
33+
34+
return 0;
35+
36+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* You are given a number N and find all the distinct factors of N.
2+
3+
Input:
4+
First-line will contain the number N.
5+
Output:
6+
In the first line print number of distinct factors of N.
7+
In the second line print all distinct factors in ascending order separated by space.
8+
Constraints
9+
1≤N≤106
10+
Sample Input 1:
11+
4
12+
Sample Output 1:
13+
3
14+
1 2 4
15+
Sample Input 2:
16+
6
17+
Sample Output 2:
18+
4
19+
1 2 3 6
20+
EXPLANATION:
21+
In the first example, all factors of 4 are 1, 2, 4.
22+
In the second example, all factors of 6 are 1, 2, 3, 6. */
23+
24+
#include <stdio.h>
25+
26+
int main(void) {
27+
28+
int n;
29+
scanf("%d",&n);
30+
int c=0;
31+
int a[n];
32+
33+
for(int i=1;i<=n;i++)
34+
{
35+
if(n%i==0)
36+
{
37+
a[c]=i;
38+
c++;
39+
}
40+
}
41+
printf("%d \n",c);
42+
for(int i=0;i<c;i++)
43+
{
44+
printf("%d ",a[i]);
45+
}
46+
47+
return 0;
48+
}

0 commit comments

Comments
 (0)