Skip to content

Commit 3320686

Browse files
committed
added all arrays assignment programs
1 parent 428ffbe commit 3320686

File tree

5 files changed

+255
-0
lines changed

5 files changed

+255
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ This repo includes:
5050
- [Linear Search](./milestone2/arrays/linearsearch.cpp)
5151
- [Arrange Numbers in Array](./milestone2/arrays/ArrangeNumbersinArray.cpp)
5252
- [Swap Alternate](./milestone2/arrays/SwapAlternate.cpp)
53+
- [Find Unique](./milestone2/arrays/FindUnique.cpp)
54+
- [Find Duplicate](./milestone2/arrays/FindDuplicate.cpp)
55+
- [Intersection of Two Arrays II](./milestone2/arrays/IntersectionofTwoArraysII.cpp)
56+
- [Pair Sum](./milestone2/arrays/PairSum.cpp)
57+
- [Triplet Sum](./milestone2/arrays/TripletSum.cpp)
58+
- [Sort 0 1](./milestone2/arrays/Sort01.cpp)
5359

5460
# Coding Ninjas
5561
## Rate my Repo ⭐!!!

milestone2/arrays/FindDuplicate.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// You have been given an integer array/list(ARR) of size N which contains numbers from 0 to (N - 2).
2+
// Each number is present at least once. That is, if N = 5, the array/list constitutes values ranging from 0 to 3 and among these, there is a single integer value that is present twice.
3+
// You need to find and return that duplicate number present in the array.
4+
// Note : Duplicate number is always present in the given array/list.
5+
6+
// My Code
7+
int duplicateNumber(int *arr, int size)
8+
{
9+
int count, res;
10+
for(int i=0; i<size; i++){
11+
int j= arr[i];
12+
count = 0;
13+
for(int k=0; k<size; k++){
14+
if (arr[k] == j){
15+
count++;
16+
}
17+
}
18+
if(count > 1){
19+
res = arr[i];
20+
}
21+
}
22+
return res;
23+
}
24+
25+
// Main Code
26+
#include <iostream>
27+
using namespace std;
28+
29+
#include "solution.h"
30+
31+
int main()
32+
{
33+
34+
int t;
35+
cin >> t;
36+
37+
while (t--)
38+
{
39+
int size;
40+
cin >> size;
41+
int *input = new int[size];
42+
43+
for (int i = 0; i < size; i++)
44+
{
45+
cin >> input[i];
46+
}
47+
48+
cout << duplicateNumber(input, size) << endl;
49+
}
50+
51+
return 0;
52+
}
53+
54+
// CN Code
55+
int duplicateNumber(int *arr, int size)
56+
{
57+
for(int i=0; i<size; i++){
58+
for(int j=i+1; j<size; j++){
59+
if(arr[i] == arr[j]){
60+
return arr[i];
61+
}
62+
}
63+
}
64+
}

milestone2/arrays/FindUnique.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// You have been given an integer array/list(ARR) of size N. Where N is equal to [2M + 1].
2+
// Now, in the given array/list, 'M' numbers are present twice and one number is present only once.
3+
// You need to find and return that number which is unique in the array/list.
4+
// Note: Unique element is always present in the array/list according to the given condition.
5+
6+
//My Code
7+
int findUnique(int *arr, int size)
8+
{
9+
int count, res;
10+
for(int i=0; i<size; i++){
11+
int j= arr[i];
12+
count = 0;
13+
for(int k=0; k<size; k++){
14+
if (arr[k] == j){
15+
count++;
16+
}
17+
}
18+
if(count == 1){
19+
res = arr[i];
20+
}
21+
}
22+
return res;
23+
}
24+
25+
// Main Code
26+
#include <iostream>
27+
#include "solution.h"
28+
using namespace std;
29+
30+
int main()
31+
{
32+
33+
int t;
34+
cin >> t;
35+
36+
while (t--)
37+
{
38+
int size;
39+
cin >> size;
40+
int *input = new int[size];
41+
42+
for (int i = 0; i < size; ++i)
43+
{
44+
cin >> input[i];
45+
}
46+
47+
cout << findUnique(input, size) << endl;
48+
}
49+
50+
return 0;
51+
}
52+
53+
// CN Code
54+
int findUnique(int *arr, int size)
55+
{
56+
for(int i=0; i<size; i++){
57+
int j=0;
58+
for(; j<size; j++){
59+
if(i != j && arr[i] == arr[j]){
60+
break;
61+
}
62+
}
63+
if(j == size){
64+
return arr[i];
65+
}
66+
}
67+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// You have been given two integer arrays/list(ARR1 and ARR2) of size N and M, respectively.
2+
// You need to print their intersection; An intersection for this problem can be defined when both the arrays/lists contain a particular value or to put it in other words,
3+
// when there is a common value that exists in both the arrays/lists.
4+
// Note : Input arrays/lists can contain duplicate elements.
5+
// The intersection elements printed would be in the order they appear in the first array/list(ARR1)
6+
7+
// My Code
8+
void intersection(int *input1, int *input2, int size1, int size2)
9+
{
10+
for(int i=0; i<size1; i++){
11+
for (int j=0; j<size2; j++){
12+
if(input1[i] == input2[j]){
13+
cout<< input1[i] << " ";
14+
input2[j] = -1;
15+
break;
16+
}
17+
}
18+
}
19+
}
20+
21+
// Main Code
22+
#include <iostream>
23+
#include <algorithm>
24+
using namespace std;
25+
#include "solution.h"
26+
27+
int main()
28+
{
29+
int t;
30+
cin >> t;
31+
while (t--)
32+
{
33+
34+
int size1, size2;
35+
36+
cin >> size1;
37+
int *input1 = new int[size1];
38+
39+
for (int i = 0; i < size1; i++)
40+
{
41+
cin >> input1[i];
42+
}
43+
44+
cin >> size2;
45+
int *input2 = new int[size2];
46+
47+
for (int i = 0; i < size2; i++)
48+
{
49+
cin >> input2[i];
50+
}
51+
52+
intersection(input1, input2, size1, size2);
53+
54+
delete[] input1;
55+
delete[] input2;
56+
57+
cout << endl;
58+
}
59+
60+
return 0;
61+
}

milestone2/arrays/Sort01.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// You have been given an integer array/list(ARR) of size N that contains only integers, 0 and 1.
2+
// Write a function to sort this array/list. Think of a solution which scans the array/list only once and don't require use of an extra array/list.
3+
// Note: You need to change in the given array/list itself.
4+
// Hence, no need to return or print anything.
5+
6+
// My Code
7+
void sortZeroesAndOne(int *input, int size)
8+
{
9+
int zero = 0;
10+
for (int i=0; i<size; i++){
11+
if (input[i] == 0){
12+
int temp = input[zero];
13+
input[zero] = input[i];
14+
input[i] = temp;
15+
zero++;
16+
}
17+
}
18+
}
19+
20+
// Main Code
21+
#include <iostream>
22+
#include <algorithm>
23+
using namespace std;
24+
25+
#include "solution.h"
26+
27+
int main()
28+
{
29+
30+
int t;
31+
cin >> t;
32+
33+
while (t--)
34+
{
35+
int size;
36+
37+
cin >> size;
38+
int *input = new int[size];
39+
40+
for (int i = 0; i < size; ++i)
41+
{
42+
cin >> input[i];
43+
}
44+
45+
sortZeroesAndOne(input, size);
46+
47+
for (int i = 0; i < size; ++i)
48+
{
49+
cout << input[i] << " ";
50+
}
51+
52+
cout << endl;
53+
delete[] input;
54+
}
55+
56+
return 0;
57+
}

0 commit comments

Comments
 (0)