Skip to content

Commit ca0ab18

Browse files
author
Parth Garg
committed
arrays
1 parent 1a74e06 commit ca0ab18

File tree

43 files changed

+1739
-0
lines changed

Some content is hidden

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

43 files changed

+1739
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
//Time: O(n) for unsorted array
5+
//Time: O(logn) for sorted array (Binary Search)
6+
int linearSearch(int arr[], int n, int key)
7+
{
8+
for (int i = 0; i < n; i++)
9+
{
10+
if (arr[i] == key)
11+
{
12+
return i;
13+
}
14+
}
15+
16+
return -1;
17+
}
18+
19+
int main()
20+
{
21+
int arr[] = {3, 2, 4, 1, 5};
22+
int n = sizeof(arr) / sizeof(int);
23+
int key = 1;
24+
25+
cout << linearSearch(arr, n, key) << endl;
26+
return 0;
27+
}

05. Arrays/02 Insert/02 Insert.cpp

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
//Time: O(n) insert at begin
5+
//Time: O(1) insert at end
6+
int insert(int arr[], int n, int key, int pos)
7+
{
8+
for (int i = n - 1; i >= pos - 1; i--)
9+
{
10+
arr[i + 1] = arr[i];
11+
}
12+
arr[pos - 1] = key;
13+
n++;
14+
15+
return n;
16+
}
17+
18+
void display(int arr[], int n)
19+
{
20+
for (int i = 0; i < n; i++)
21+
{
22+
cout << arr[i] << " ";
23+
}
24+
cout << endl;
25+
26+
return ;
27+
}
28+
29+
int main()
30+
{
31+
int arr[] = {3, 2, 4, 1, 5};
32+
int n = sizeof(arr) / sizeof(int);
33+
int key = 6;
34+
int pos = 3;
35+
36+
display(arr, n);
37+
n = insert(arr, n, key, pos);
38+
display(arr, n);
39+
40+
return 0;
41+
}

05. Arrays/03 Delete/03 Delete.cpp

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
//Time: O(n) delete at begin
5+
//Time: O(1) delete at end
6+
int del(int arr[], int n, int key)
7+
{
8+
int idx, i;
9+
for (i = 0; i < n; i++)
10+
{
11+
if (key == arr[i])
12+
{
13+
idx = i;
14+
break;
15+
}
16+
}
17+
18+
if (i == n)
19+
{
20+
return n;
21+
}
22+
23+
for (int i = idx; i < n - 1; i++)
24+
{
25+
arr[i] = arr[i + 1];
26+
}
27+
n--;
28+
29+
return n;
30+
}
31+
32+
void display(int arr[], int n)
33+
{
34+
for (int i = 0; i < n; i++)
35+
{
36+
cout << arr[i] << " ";
37+
}
38+
cout << endl;
39+
40+
return ;
41+
}
42+
43+
int main()
44+
{
45+
int arr[] = {3, 2, 4, 1, 5};
46+
int n = sizeof(arr) / sizeof(int);
47+
int key = 4;
48+
49+
display(arr, n);
50+
n = del(arr, n, key);
51+
display(arr, n);
52+
return 0;
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
//Time: O(n)
5+
int largest(int a[], int n)
6+
{
7+
int max_element = INT_MIN;
8+
for (int i = 0; i < n; i++)
9+
{
10+
if (a[i] > max_element)
11+
{
12+
max_element = a[i];
13+
}
14+
}
15+
16+
return max_element;
17+
}
18+
19+
int main()
20+
{
21+
int arr[] = {3, 2, 4, 5, 1};
22+
int n = sizeof(arr) / sizeof(int);
23+
24+
cout << largest(arr, n) << endl;
25+
return 0;
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
int largest(int a[], int n)
5+
{
6+
int max = INT_MIN;
7+
for (int i = 0; i < n; i++)
8+
{
9+
if (a[i] > max)
10+
{
11+
max = a[i];
12+
}
13+
}
14+
15+
return max;
16+
}
17+
18+
//Time: O(n) (two traversals)
19+
int secondLargest(int arr[], int n)
20+
{
21+
int max = largest(arr, n);
22+
int res = INT_MIN;
23+
int i;
24+
for (i = 0; i < n; i++)
25+
{
26+
if (res < arr[i] and arr[i] != max)
27+
{
28+
res = arr[i];
29+
}
30+
}
31+
32+
if(res==INT_MIN)
33+
{
34+
return -1;
35+
}
36+
37+
return res;
38+
}
39+
40+
int main()
41+
{
42+
int arr[] = {3, 2, 4, 1, 5};
43+
int n = sizeof(arr) / sizeof(int);
44+
45+
cout << secondLargest(arr, n) << endl;
46+
return 0;
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
//Time: O(n) (one traversal)
5+
int secondLargest(int arr[], int n)
6+
{
7+
int greatest, secondGreatest;
8+
greatest = secondGreatest = -1;
9+
10+
for (int i = 0; i < n; i++)
11+
{
12+
if (arr[i] > greatest)
13+
{
14+
secondGreatest = greatest;
15+
greatest = arr[i];
16+
}
17+
if (arr[i]<greatest and arr[i]>secondGreatest)
18+
{
19+
secondGreatest = arr[i];
20+
}
21+
}
22+
23+
return secondGreatest;
24+
}
25+
26+
int main()
27+
{
28+
int arr[] = {3, 2, 4, 1, 5};
29+
int n = sizeof(arr) / sizeof(int);
30+
31+
cout << secondLargest(arr, n) << endl;
32+
return 0;
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
//Time: O(n)
5+
bool sorted(int a[], int n)
6+
{
7+
if (n <= 1)
8+
{
9+
return true;
10+
}
11+
12+
for (int i = 0; i < n - 1; i++)
13+
{
14+
if (a[i] > a[i + 1])
15+
{
16+
return false;
17+
}
18+
}
19+
20+
return true;
21+
}
22+
23+
int main()
24+
{
25+
int arr[] = {3, 4, 5, 1, 2};
26+
int n = sizeof(arr) / sizeof(int);
27+
28+
cout << sorted(arr, n) << endl;
29+
return 0;
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
//Time: O(n)
5+
void reverse(int a[], int n)
6+
{
7+
if (n <= 1)
8+
{
9+
return ;
10+
}
11+
12+
int i = 0;
13+
int j = n - 1;
14+
while (i < j)
15+
{
16+
swap(a[i], a[j]);
17+
i++;
18+
j--;
19+
}
20+
}
21+
22+
void display(int a[], int n)
23+
{
24+
for (int i = 0; i < n; i++)
25+
{
26+
cout << a[i] << " ";
27+
}
28+
cout << endl;
29+
}
30+
31+
int main()
32+
{
33+
int arr[] = {3, 2, 4, 1, 5};
34+
int n = sizeof(arr) / sizeof(int);
35+
36+
display(arr, n);
37+
reverse(arr, n);
38+
display(arr, n);
39+
return 0;
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
//Time: O(n)
5+
int removeDuplicate(int a[], int n)
6+
{
7+
int res=1;
8+
for(int i=1;i<n;i++)
9+
{
10+
if(a[i]!=a[res-1])
11+
{
12+
a[res]=a[i];
13+
res++;
14+
}
15+
}
16+
17+
return res;
18+
}
19+
20+
void display(int a[], int n)
21+
{
22+
for (int i = 0; i < n; i++)
23+
{
24+
cout << a[i] << " ";
25+
}
26+
cout << endl;
27+
}
28+
29+
int main()
30+
{
31+
int arr[] = {10,20,20,30,30,30};
32+
int n = sizeof(arr) / sizeof(int);
33+
34+
display(arr, n);
35+
n = removeDuplicate(arr, n);
36+
display(arr, n);
37+
38+
return 0;
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
//Time: O(n^2)
5+
void moveZeros(int a[], int n)
6+
{
7+
for (int i = 0; i < n; i++)
8+
{
9+
if (a[i] == 0)
10+
{
11+
for (int j = i + 1; j < n; j++)
12+
{
13+
if (a[j] != 0)
14+
{
15+
swap(a[i], a[j]);
16+
break;
17+
}
18+
}
19+
}
20+
}
21+
}
22+
23+
void display(int a[], int n)
24+
{
25+
for (int i = 0; i < n; i++)
26+
{
27+
cout << a[i] << " ";
28+
}
29+
cout << endl;
30+
}
31+
32+
int main()
33+
{
34+
int arr[] = {0, 3, 2, 1, 0, 4, 7, 0, 2};
35+
int n = sizeof(arr) / sizeof(int);
36+
37+
display(arr, n);
38+
moveZeros(arr, n);
39+
display(arr, n);
40+
41+
return 0;
42+
}

0 commit comments

Comments
 (0)