-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVectors.cpp
More file actions
110 lines (93 loc) · 2.38 KB
/
Vectors.cpp
File metadata and controls
110 lines (93 loc) · 2.38 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
109
110
Vectors::
In C++, vector is a dynamic array that stores collection of elements same type in contiguous memory. It has the ability to resize itself automatically when an element is inserted or deleted.
Syntax
Vector is defined as the std::vector class template inside the <vector> header file.
1) vector<int> vec;
2) vector<int> vec={1,2,3,4,5}
3) vector<int> vec(3,0)
// #include <bits/stdc++.h>
// using namespace std;
// int main() {
// vector<char> v = {'a', 'f', 'd'};
// // Inserting 'z' at the back
// v.push_back('z');
// // Inserting 'c' at index 1
// v.insert(v.begin() + 1, 'c');
// for (int i = 0; i < v.size(); i++)
// cout << v[i] << " ";
// return 0;
// }
#include<iostream>
using namespace std;
#include<vector>
int main(){
vector<int>vec={1,2,3,4,5};
for(int i: vec){
cout<<i<<endl;}
}
/Vector Functions
/size,push back,pop_back,front,back,at
#include<iostream>
using namespace std;
#include<vector>
int main(){
vector<int>vec={1,2,3,4,5};
cout<<vec.size()<<endl;
vec.push_back(30); //add the value at last
vec.push_back(40);
cout<<vec.size()<<endl;
vec.pop_back();//delete the last value
cout<<vec.size()<<endl;
cout<<vec.front()<<endl; //return front value means 1
}
#include<iostream>
using namespace std;
#include<vector>
int main(){
int ans=0;
vector<int>vec={1,2,1,3,4,2,5,6};
for(int val:vec){
ans=ans ^= val;
}
cout<<ans<<endl;
return 0;
}
output :4
/SUBARRAYS
/Question: Print all the subArray or subset in the
#include<iostream>
using namespace std;
#include<vector>
int main(){
int arrays[5]={2,3,4,5,6};
int n=5;
for(int start=0;start<n;start++){
for(int end=start;end<n;end++){
for(int i=start;i<end;i++){
cout<<arrays[i];
}
cout<<" ";
}
cout<<endl;
}
}
Maximum SubArrays Sum
/ Kadanes Algorithms
#include<iostream>
using namespace std;
#include <climits>
#include<vector>
int main(){
int arrays[7]={1,-2,4,5,-6,7,8};
int size=7;
int Sum=0;
int maxSum=INT_MIN;
for(int start=0;start<size;start++){
Sum+=arrays[start];
maxSum=max(Sum,maxSum);
if(Sum<0){
Sum=0;
}
}
cout<<"The maximum sum of the subarray is"<<maxSum<<endl;
}