Skip to content

Commit 2ecc0ca

Browse files
committed
2 parents 7859670 + 1663be0 commit 2ecc0ca

File tree

77 files changed

+4599
-9
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+4599
-9
lines changed

2D Arrays/rotate_by_90.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Given a square matrix of size N x N. The task is to rotate it by 90 degrees in anti-clockwise direction without using any extra space.
2+
3+
#include<bits/stdc++.h>
4+
#include <stdio.h>
5+
using namespace std;
6+
7+
int main()
8+
{
9+
int t; cin>>t;
10+
11+
while(t--)
12+
{
13+
int n;
14+
cin>>n;
15+
int matrix[n][n];
16+
17+
for(int i = 0; i < n; i++)
18+
{
19+
for(int j = 0; j < n; j++)
20+
{
21+
cin>>matrix[i][j];
22+
}
23+
}
24+
25+
//rotating the matrix by 90 , anticlockwise
26+
// the matrix will have floor(n/2) cycles
27+
for(int i=0; i<n/2; i++)
28+
{
29+
//parsing through each cycle and rotating
30+
for(int j=i; j< n-i-1; j++)
31+
{
32+
//stored current element in temporary variable temp
33+
int temp= matrix[i][j];
34+
35+
//shifted right element to top
36+
matrix[i][j]= matrix[j][n-i-1];
37+
38+
//shifted bottom element to right
39+
matrix[j][n-i-1]= matrix[n-1-i][n-1-j];
40+
41+
//shifted left element to bottom
42+
matrix[n-1-i][n-1-j] = matrix[n-1-j][i];
43+
44+
//assign temp to left
45+
matrix[n-1-j][i]= temp;
46+
}
47+
}
48+
49+
for(int i = 0; i < n; i++)
50+
for(int j=0; j<n; j++)
51+
cout<< matrix[i][j];
52+
cout<<endl;
53+
}
54+
return 0;
55+
}

2D Arrays/transpose.java

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
/*
2+
Title : Find the transpose of the matrix .
3+
User needs to provide the number of rows and columns .
4+
And the user needs to provide the elements of the matrix .
5+
User will get the transpose of the matrix .
6+
i.e Row elements become the column elements and vice- versa .
7+
**** Maximum 10 number of rows and columns can be only entered .
8+
*/
9+
import java.util.Scanner;
10+
import java.lang.*;
11+
class transpose
12+
{
13+
final static int MAXLIMIT=10 ;
14+
private int row;
15+
private int col;
16+
int i ,j ;
17+
private double a[][];
18+
19+
transpose(int rows , int cols) //Constructor
20+
{
21+
row=rows ;
22+
col=cols ;
23+
a=new double[row][col] ;
24+
25+
}
26+
27+
Scanner in=new Scanner(System.in);
28+
29+
public void get ()
30+
{
31+
System.out.println("\n Enter the elements : \n");
32+
for(i=0;i<row;i++)
33+
{
34+
for(j=0;j<col;j++)
35+
{
36+
a[i][j]=in.nextDouble();
37+
}
38+
}
39+
40+
}
41+
42+
public void display ()
43+
{
44+
System.out.println( "\n Matrix A " + row + " * " + col + " : \n") ;
45+
46+
for(i=0;i<row;i++)
47+
{
48+
for(j=0;j<col;j++)
49+
{
50+
System.out.print(a[i][j]+ "\t") ;
51+
}
52+
System.out.println("\n") ;
53+
}
54+
}
55+
56+
public void trans()
57+
{
58+
System.out.println( "\n\n The transpose of the given matrix : \n");
59+
System.out.println( "\n Matrix AT " + col + " * " + row+ " : \n") ;
60+
61+
for(i=0;i<col;i++)
62+
{
63+
for(j=0;j<row;j++)
64+
{
65+
System.out.print(a[j][i]+ "\t") ;
66+
}
67+
System.out.println("\n") ;
68+
}
69+
}
70+
71+
public static void main (String [] args )
72+
{
73+
int rows , cols ;
74+
int opt;
75+
Scanner in=new Scanner(System.in);
76+
do
77+
{ opt=0;
78+
System.out.println("\n Enter the total number of rows and columns respectively : \n");
79+
rows=in.nextInt();
80+
cols=in.nextInt();
81+
if(rows>MAXLIMIT|| cols>MAXLIMIT|| rows<1||cols<1)
82+
{
83+
System.out.println("\n Invalid enteries ! \n Remember , Maximum limit of rows and columns is 10 .\n");
84+
System.out.println(" Do you wish to continue ? \n Press 1 if yes otherwise any to stop . \n");
85+
opt=in.nextInt();
86+
}
87+
88+
else
89+
{
90+
91+
transpose A = new transpose(rows , cols);
92+
A.get();
93+
A.display();
94+
A.trans();
95+
96+
97+
}
98+
} while(opt==1);
99+
100+
}
101+
102+
}
103+
104+
/*
105+
106+
Enter the total number of rows and columns respectively :
107+
108+
13
109+
1
110+
111+
Invalid enteries !
112+
Remember , Maximum limit of rows and columns is 10 .
113+
114+
Do you wish to continue ?
115+
Press 1 if yes otherwise any to stop .
116+
117+
1
118+
119+
Enter the total number of rows and columns respectively :
120+
121+
3
122+
2
123+
124+
Enter the elements :
125+
126+
1
127+
2
128+
3
129+
5
130+
5
131+
5
132+
133+
Matrix A 3 * 2 :
134+
135+
1.0 2.0
136+
137+
3.0 5.0
138+
139+
5.0 5.0
140+
141+
142+
The transpose of the given matrix : \n");
143+
144+
Matrix AT 2 * 3 :
145+
146+
1.0 3.0 5.0
147+
148+
2.0 5.0 5.0
149+
150+
*/

2D Arrays/transpose_of_matrix.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//Write a program to find the transpose of a square matrix of size N*N. The transpose of a matrix is obtained by changing rows to columns and columns to rows.
2+
3+
#include <bits/stdc++.h>
4+
using namespace std;
5+
6+
//Function to find transpose of a matrix.
7+
void transpose(vector<vector<int> > &matrix, int n)
8+
{
9+
for(int i=0; i< n; i++)
10+
{
11+
12+
for(int j=i; j< n; j++)
13+
{
14+
swap(matrix[i][j], matrix[j][i]);
15+
}
16+
}
17+
}
18+
19+
int main()
20+
{
21+
22+
int t;
23+
cin>>t;
24+
25+
while(t--)
26+
{
27+
int n;
28+
cin>>n;
29+
vector<vector<int> > matrix(n);
30+
31+
for(int i=0; i<n; i++)
32+
{
33+
matrix[i].assign(n, 0);
34+
for( int j=0; j<n; j++)
35+
{
36+
cin>>matrix[i][j];
37+
}
38+
}
39+
40+
transpose(matrix,n);
41+
for (int i = 0; i < n; ++i)
42+
{
43+
for (int j = 0; j < n; ++j)
44+
cout<<matrix[i][j]<<" ";
45+
cout<<endl;
46+
}
47+
}
48+
return 0;
49+
50+
}

Arrays/Hourglass_Sum.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
'''
2+
Given a 6X6 2D Array, A:
3+
1 1 1 0 0 0
4+
0 1 0 0 0 0
5+
1 1 1 0 0 0
6+
0 0 0 0 0 0
7+
0 0 0 0 0 0
8+
0 0 0 0 0 0
9+
10+
An hourglass in A is a subset of values with indices falling in this pattern
11+
in A's graphical representation:
12+
a b c
13+
d
14+
e f g
15+
16+
There are 16 hourglasses in A, and an hourglass sum is the sum of an hourglass'
17+
values. Like here, the maximum hourglass sum is 7 for the hourglass in the top
18+
left corner.
19+
20+
Aim: Check all such hourglasses and print the maximum hourglass sum for the
21+
2D array entered by user.
22+
23+
'''
24+
25+
# getting the 2D array as input
26+
arr = []
27+
for _ in range(6):
28+
arr.append(list(map(int, input().rstrip().split())))
29+
30+
res = []
31+
# looping through the 2D array
32+
for x in range(0, 4):
33+
for y in range(0, 4):
34+
# selecting combinations that make an hourglass
35+
s = sum(arr[x][y:y+3]) + arr[x+1][y+1] + sum(arr[x+2][y:y+3])
36+
res.append(s)
37+
# printing out the maximum hourglass sum
38+
print(max(res))
39+
40+
'''
41+
42+
COMPLEXITY:
43+
44+
Time Complexity -> O(N^2)
45+
Space Complexity -> O(N)
46+
47+
Sample Input:
48+
0 0 1 2 3 0
49+
0 0 0 5 0 0
50+
1 0 0 7 1 0
51+
0 0 0 0 0 0
52+
0 1 1 1 0 1
53+
0 0 4 0 0 4
54+
55+
Sample Output:
56+
19
57+
58+
Explanation:
59+
The hourglass with the maximum sum is,
60+
1 2 3
61+
5
62+
0 7 1
63+
with the sum being 1 + 2 + 3 + 5 + 0 + 7 + 1 = 19
64+
65+
'''

Arrays/Leap_Year.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'''
2+
Aim: To check if the entered year is a leap year or not.
3+
4+
'''
5+
6+
def is_leap(year):
7+
# a year is a leap one if it's divisible by 4 and if it's not
8+
# divisible by 100 or is divisible by 400
9+
print((year%4==0 and (year%100!=0 or year%400==0)))
10+
11+
# getting the input
12+
year = int(input().strip())
13+
# displaying the result
14+
is_leap(year)
15+
16+
'''
17+
18+
COMPLEXITY:
19+
20+
Time Complexity -> O(1)
21+
Space Complexity -> O(1)
22+
23+
Sample Input:
24+
2000
25+
26+
Sample Output:
27+
True
28+
29+
Explanation:
30+
2000 % 400 = 0
31+
2000 % 4 = 0
32+
Hence, 2000 was a leap year.
33+
34+
'''

0 commit comments

Comments
 (0)