Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions Basic of Programming/Arrays/Union of 2 array/q.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<a href="https://www.geeksforgeeks.org/find-union-and-intersection-of-two-unsorted-arrays/">Link </a>
<pre>Given two arrays A and B of size N and M respectively. The task is to find union between these two arrays.
Union of the two arrays can be defined as the set containing distinct elements from both the arrays. If there are repetitions, then only one occurrence of element should be printed in union.

Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case consist of three lines. The first line of each test case contains two space separated integers N and M, where N is the size of array A and M is the size of array B. The second line of each test case contains N space separated integers denoting elements of array A. The third line of each test case contains M space separated integers denoting elements of array B.

Output:
Correspoding to each test case, print the count of union elements of the two arrays.

Expected Time Complexity: O(N + M).
Expected Auxiliary Space: O(N + M).

Constraints:
1 ≤ T ≤ 100
1 ≤ N, M ≤ 105
1 ≤ A[i], B[i] < 105

Example:
Input:
2
5 3
1 2 3 4 5
1 2 3
6 2
85 25 1 32 54 6
85 2
Output:
5
7

Explanation:
Testcase 1: 1, 2, 3, 4 and 5 are the elements which comes in the union set of both arrays.
Testcase 2: 1 , 2 , 6 , 25 , 32 , 54 and 85 are the elements which comes in the union set of both arrays. </pre>
25 changes: 25 additions & 0 deletions Basic of Programming/Arrays/Union of 2 array/sol.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <iostream>
#include<unordered_map>
using namespace std;

int main() {
//code
int t;
cin>>t;
while(t--){
int n,m;
cin>>n;
cin>>m;
int arr1[n],arr2[m];
for(int i=0;i<n;i++)cin>>arr1[i];
for(int i=0;i<m;i++)cin>>arr2[i];

unordered_map <int,int> mp;
for(int i=0;i<n;i++)mp[arr1[i]]++;
for(int j=0;j<m;j++)mp[arr2[j]]++;

cout<<mp.size()<<endl;

}
return 0;
}
12 changes: 12 additions & 0 deletions Basic of Programming/Arrays/move negative no to left/Question.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

<h2>Move all negative numbers to beginning and positive to end with constant extra space</h2>
<h3>
An array contains both positive and negative numbers in random order. Rearrange the array elements so that all negative numbers appear before all positive numbers.
</h3>
<pre>
Examples :

Input: -12, 11, -13, -5, 6, -7, 5, -3, -6
Output: -12 -13 -5 -7 -3 -6 11 6 5
Note: Order of elements is not important here.
</pre>
70 changes: 70 additions & 0 deletions Basic of Programming/Arrays/move negative no to left/sol.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//quick sort partion approch
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
void rearrange(int arr[],int n){
int j=0;
for(int i=0;i<n;i++){

if(arr[i]<0){
if(i!=j)
swap(arr[i],arr[j]);

j++;
}
}


}

void printArray(int arr[], int n)
{
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
}

int main() {

int arr[] = { 1, 2, -3, 4, 5, 6, -7, 9,-8 };
int n = sizeof(arr) / sizeof(arr[0]);
rearrange(arr, n);
printArray(arr, n);
return 0;
}

//two pointer approch
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
void rearrange(int arr[],int n){
int i=0, j=n-1;
while(i<j){
if(arr[i]<0)i++;

if(arr[j]>0)j--;

if(arr[i]>0 && arr[j]<0){
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
j--;
i++;
}

}
}

void printArray(int arr[], int n)
{
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
}

int main() {

int arr[] = { 1, 2, -3, 4, 5, 6, -7, 9,-8 };
int n = sizeof(arr) / sizeof(arr[0]);
rearrange(arr, n);
printArray(arr, n);
return 0;
}