Skip to content

Commit 7c01aac

Browse files
committed
solved unionIntersection
1 parent 6ed4303 commit 7c01aac

File tree

2 files changed

+133
-9
lines changed

2 files changed

+133
-9
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
2+
/*******************************************************************************************************
3+
/* ___ _ _ _ _
4+
/* / _ \ | | (_)| | | |
5+
/* / /_\ \__ __ ___ | | __ _ ___ _ __ _ | |_ | |__ _ __ ___ ___
6+
/* | _ |\ \ /\ / / / _ \| | / _` | / _ \ | '__|| || __|| '_ \ | '_ ` _ \ / __|
7+
/* | | | | \ V V / | __/| || (_| || (_) || | | || |_ | | | || | | | | |\__ \
8+
/* \_| |_/ \_/\_/ \___||_| \__, | \___/ |_| |_| \__||_| |_||_| |_| |_||___/
9+
/* __/ |
10+
/* |___/
11+
/******************************************************************************************************/
12+
13+
/***********************************
14+
/* Reverse An Array
15+
/***********************************/
16+
17+
// Library imports
18+
#include <iostream>
19+
using namespace std;
20+
21+
//Prototypes for the algorithms
22+
int unionAndIntersection(int* arr,int len, int* arr2,int len2);
23+
void someSortingAlgorithm(int*,int);
24+
int removeDuplicates(int*,int);
25+
26+
int main(){
27+
// Lines to handle user input go here, be descriptive...
28+
// use cin, cout
29+
int arr[] = {1, 3, 4, 5, 7};
30+
int arr2[] = {2, 3, 5, 6};
31+
32+
int arrayLength = sizeof(arr)/sizeof(arr[0]);
33+
int arrayLength2 = sizeof(arr2)/sizeof(arr2[0]);
34+
// Call your algorithms here
35+
unionAndIntersection(arr, arrayLength, arr2, arrayLength2);
36+
37+
// Union[] = {1, 2, 3, 4, 5, 6, 7}
38+
// Intersection[] = {3,5}
39+
40+
return 0;
41+
}
42+
43+
/**********************************************************
44+
/* <unionAndIntersection>
45+
/*
46+
/* @description : description here
47+
/* @param : Integer<variable> (Datatype<variable_name>)
48+
/* @return : datatype (int,float,double etc)
49+
/* @complexity : Big O notation. (O(n),O(n^2) etc)
50+
/* @explanation : Detailed description about what the algorithm does
51+
/* @author : <Your name> @<GitHub handle> (John Doe @johndoe)
52+
/**********************************************************/
53+
int unionAndIntersection(int* arr,int len, int* arr2,int len2){ //simple one first
54+
// Algorithm content goes here.
55+
//
56+
int unionArray[len+len2];
57+
int counter = 0;
58+
59+
// UNION START
60+
// *************************************************************
61+
for(int i=0;i<len;i++){
62+
unionArray[i] = arr[i];
63+
}
64+
65+
for(int j=0; j<len2; j++){
66+
unionArray[j+len] = arr2[j];
67+
}
68+
69+
cout<<endl;
70+
71+
someSortingAlgorithm(unionArray, len+len2);
72+
73+
int totalLen = removeDuplicates(unionArray,len+len2);
74+
75+
cout<<"Union: ";
76+
for(int i=0;i<totalLen;i++){
77+
cout<<unionArray[i]<<" ";
78+
}
79+
cout<<endl;
80+
//UNION END
81+
//// *************************************************************
82+
83+
// INTERSECTION START
84+
// *************************************************************
85+
cout<<"Intersection: ";
86+
for(int i=0;i<len;i++){
87+
for(int j=0;j<len2;j++){
88+
if(arr[i]==arr2[j]){
89+
cout<<arr[i]<<" ";
90+
}
91+
}
92+
}
93+
cout<<endl;
94+
return* arr;
95+
}
96+
97+
98+
void someSortingAlgorithm(int* arr,int len){
99+
100+
//Bubble sort. Yeah, don't use this in real life situations
101+
for(int i=0;i<len-1;i++){
102+
for(int j=i;j<len;j++){
103+
if(arr[i]>arr[j]){
104+
int temp = arr[i];
105+
arr[i] = arr[j];
106+
arr[j] = temp;
107+
}
108+
109+
}
110+
}
111+
}
112+
113+
int removeDuplicates(int *arr,int len){
114+
int temp[len];
115+
int counter =0;
116+
117+
for(int i=0;i<len;i++){
118+
if(arr[i]==arr[i+1]){
119+
continue;
120+
}
121+
else{
122+
temp[counter++]=arr[i];
123+
}
124+
}
125+
126+
//Transferring values from temp array to original pointer
127+
for(int i=0;i<counter;i++){
128+
arr[i]=temp[i];
129+
}
130+
//Returning the original array.
131+
return counter;
132+
}

Arrays and Strings/README.md

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,22 +65,14 @@
6565

6666
arr1[] = {1, 3, 4, 5, 7}
6767
arr2[] = {2, 3, 5, 6}
68-
68+
6969
Union[] = {1, 2, 3, 4, 5, 6, 7}
7070
Intersection= {3,5}
7171

7272

7373
TODO :
7474

7575

76-
77-
* Given two sorted arrays, find their union and intersection.
78-
79-
For example, if the input arrays are:
80-
arr1[] = {1, 3, 4, 5, 7}
81-
arr2[] = {2, 3, 5, 6}
82-
Then your program should print Union as {1, 2, 3, 4, 5, 6, 7} and Intersection as {3, 5}.
83-
8476
* Given an array A[], write a function that segregates even and odd numbers. The functions should put all even numbers first, and then odd numbers.
8577

8678
Example

0 commit comments

Comments
 (0)