Skip to content

Commit 0de01be

Browse files
solved on 12/9/23
1 parent c44fb31 commit 0de01be

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

CS_25_FindTheSingleElement.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int getSingleElement(vector<int> &arr)
5+
{
6+
int l = 0, h = arr.size() - 1, m = (l + h) / 2;
7+
while (l < h)
8+
{
9+
if (m % 2 == 1)
10+
{
11+
m--;
12+
}
13+
if (arr[m] != arr[m + 1])
14+
{
15+
h = m;
16+
}
17+
else
18+
{
19+
l = m + 2;
20+
}
21+
m = (l + h) / 2;
22+
}
23+
return arr[l];
24+
}
25+
26+
int main()
27+
{
28+
int n;
29+
cout << "Enter the size of the array: ";
30+
cin >> n;
31+
vector<int> arr(n);
32+
cout << "Enter the elements of the array: ";
33+
for (int i = 0; i < n; i++)
34+
cin >> arr[i];
35+
cout << "The single element in the array is: ";
36+
cout << getSingleElement(arr) << endl;
37+
return 0;
38+
}

LC_7_RotateImage.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
void rotate(vector<vector<int>> &m)
5+
{
6+
int n = m.size();
7+
// transpose
8+
for (int i = 0; i < n; i++)
9+
{
10+
for (int j = i; j < m[i].size(); j++)
11+
{
12+
swap(m[i][j], m[j][i]);
13+
}
14+
}
15+
16+
// reverse
17+
for (int i = 0; i < n; i++)
18+
{
19+
reverse(m[i].begin(), m[i].end());
20+
}
21+
}
22+
23+
int main()
24+
{
25+
int n;
26+
cout << "Enter the size of the matrix: ";
27+
cin >> n;
28+
vector<vector<int>> m(n, vector<int>(n));
29+
cout << "Enter the elements of the matrix: ";
30+
for (int i = 0; i < n; i++)
31+
for (int j = 0; j < n; j++)
32+
cin >> m[i][j];
33+
cout << "The rotated matrix is: " << endl;
34+
rotate(m);
35+
for (int i = 0; i < n; i++)
36+
{
37+
for (int j = 0; j < m[i].size(); j++)
38+
{
39+
cout << m[i][j] << " ";
40+
}
41+
cout << endl;
42+
}
43+
return 0;
44+
}

0 commit comments

Comments
 (0)