File tree Expand file tree Collapse file tree 2 files changed +82
-0
lines changed Expand file tree Collapse file tree 2 files changed +82
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments