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
28 changes: 5 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,5 @@
# Competitive Programming Assignment

Solve the following problems:
1. Given an array of size n arrange the array in such a way that all even numbers comes before all odd numbers and the order should be maintained
Example - 13 25 6 24 45 2
Output - 6 24 2 13 25 45


2. Given an array of size n print the array element and it's frequency. (Order not mandatory)
Example - 2 4 2 2 3 3 3 5
Output:
2 - 3
3 - 3
4 - 1
5 - 1

# Instructions to upload

- Fork the repository
- Create a new branch (with your name)
- Upload files in the new branch.
- Create a pull request.

Arnab Chatterjee
B.tech CSE AI
1st year
F4
211003003032
77 changes: 77 additions & 0 deletions first.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include <iostream>
#include <vector>
using namespace std;

vector<int> makeArray(int n)
{
vector<int> ar;
for(int i=0;i<n;i++)
{
int t;
cin >> t;
ar.push_back(t);
}
cout << endl;

return ar;
}

void usingTime(vector<int> arr,int n)
{
for(int i=0;i<n;i++)
{
if(arr[i]%2!=0)
{
arr.push_back(arr[i]);
arr.erase(arr.begin()+i);
}
}
cout << "time : " << endl;
for(int i:arr)
{
cout << i << endl;
}
}

void usingSpace(vector<int> arr,int n)
{
vector<int> even,odd;

for(int i=0;i<n;i++)
{
if(arr[i] %2 == 0)
{
even.push_back(i);
}
else
{
odd.push_back(i);
}
}

cout << "space : " << endl;
for(int i:even)
{
cout << arr[i] << endl;
}
for(int i:odd)
{
cout << arr[i] << endl;
}
}

int main()
{
int n;
cout << "Enter length of array : ";
cin >> n;
vector<int> arr = makeArray(n);

//approch using time complexity O(n^2)
usingTime(arr,n);

// approch using space complexity O(n)
usingSpace(arr,n);

return 0;
}
21 changes: 21 additions & 0 deletions second.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <iostream>
#include <vector>
#include <unordered_map>
#define rep(i,a,b) for(int i=a;i<b;i++)
using namespace std;

int main()
{
int n;
cin >> n;
vector<int> arr;
unordered_map<int,int> m;
rep(i,0,n) {int temp; cin >> temp; arr.push_back(temp);}
rep(i,0,n) m[arr[i]]++;
unordered_map<int,int> ::iterator it = m.begin();
while(it!=m.end()){
cout << it->first << "-" << it->second << endl;
it++;
}
return 0;
}