-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ14.cpp
More file actions
108 lines (81 loc) · 2.01 KB
/
Q14.cpp
File metadata and controls
108 lines (81 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// 14. Add Element to the Beginning of an Array Example: Input: nums = [1, 2, 3, 4]; Output: nums = [0,1,2,3,4].
#include <iostream>
using namespace std;
void addElementInBeginning(int arr[], int size, int newElement) {
int updatedSize = size + 1;
int updatedArr[updatedSize];
updatedArr[0] = newElement;
for (int i = 0; i < size; i++) {
updatedArr[i + 1] = arr[i];
}
cout << "Updated array: ";
for (int i = 0; i < updatedSize; i++) {
cout << updatedArr[i] << " ";
}
cout << endl;
}
int main() {
int nums[] = {1, 2, 3, 4};
int size = sizeof(nums) / sizeof(nums[0]);
int newElement = 0;
addElementInBeginning(nums, size, newElement);
return 0;
}
//
#include <iostream>
using namespace std;
void addElementToBeginning(int arr[], int& size, int newElement) {
for (int i = size; i > 0; i--) {
arr[i] = arr[i - 1];
}
arr[0] = newElement;
size++;
cout << "Updated array: ";
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
int main() {
int nums[10] = {1, 2, 3, 4};
int size = 4;
int newElement = 0;
addElementToBeginning(nums, size, newElement);
return 0;
}
//
#include <iostream>
#include <vector>
using namespace std;
void addElementToBeginning(vector<int>& nums, int newElement) {
nums.insert(nums.begin(), newElement);
cout << "Updated array: ";
for (int num : nums) {
cout << num << " ";
}
cout << endl;
}
int main() {
vector<int> nums = {1, 2, 3, 4};
int newElement = 0;
addElementToBeginning(nums, newElement);
return 0;
}
//
#include <iostream>
#include <deque>
using namespace std;
void addElementToBeginning(deque<int>& nums, int newElement) {
nums.push_front(newElement);
cout << "Updated array: ";
for (int num : nums) {
cout << num << " ";
}
cout << endl;
}
int main() {
deque<int> nums = {1, 2, 3, 4};
int newElement = 0;
addElementToBeginning(nums, newElement);
return 0;
}