Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added my solutions to some problems #43

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
23 changes: 23 additions & 0 deletions Bad Triangle/Neha Jhawar/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
Since the array elements are already sorted in ascending order, so sum of 1st 2 elements has to be greater than last element for valid solutions.
*/

#include <bits/stdc++.h>
using namespace std;

int main() {
int te;
cin>>te;
while(te--) {
int n, i;
cin>>n;
vector<long> v(n);
for(i=0; i<n; i++) {
cin>>v[i];
}
if(v[0] + v[1] > v[n-1]) cout<<-1<<endl;
else cout<<1<<" "<<2<<" "<<n<<endl;
}

return 0;
}
67 changes: 67 additions & 0 deletions Chef and Recipe/Neha Jhawar/ChefAndRecipeSolution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
In this problem we have to keep track of 2 things for a possible solution
1. Check if no a particular set of number(ingredients) is appearing together
2. The count of numbers in each set must be unique

So to do that I am using three maps in my solution.
First map 'mp' keeps track of last position of occurence of the number in the vector v
Second map 'count' stores the count of each numbers in the vector vector
Third map 'counter' checks whether a particular element is not repeated in map 'count'

#Please note instead of array A, I am using vector v.
*/



#include <bits/stdc++.h>
using namespace std;

int main() {
int te;
cin>>te;
while(te--) {
int n, i;
cin>>n;
vector<int>v(n);
for(i=0; i<n; i++) {
cin>>v[i];
}
map<int, int> mp, count;
int flag = 0;
for(i=0; i<n; i++) {
count[v[i]]++;
//checks if a particular element is present in map 'mp'
if(mp.find(v[i])!=mp.end()) {
//if the element is present then the difference of value of map corresponding to key and the current element position(i) should be 1
if(i-mp[v[i]]!=1) {
flag=1;
break;
}
mp[v[i]] = i;
}
else {
mp[v[i]]=i;
}

}
if(flag) cout<<"NO"<<endl;
else {
flag = 0;
map<int, int> counter;
map<int, int>::iterator itr;
for(itr=count.begin(); itr!=count.end(); itr++) {
if(counter.find(itr->second) != counter.end()) {
flag=1;
break;
}
else {
counter[itr->second]=1;
}
}
if(flag) cout<<"NO"<<endl;
else cout<<"YES"<<endl;
}
}

return 0;
}
34 changes: 34 additions & 0 deletions Chef and Work/Neha Jhawar/ChefAndWork.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <bits/stdc++.h>
using namespace std;

int main() {
int te;
cin>>te;
while(te--) {
int n, k, i;
cin>>n>>k;
vector<int>v(n);
for(i=0; i<n; i++) {
cin>>v[i];
}
int s=0, count=1, flag=0;
for(i=0; i<n; i++) {
//Checks if any element in the vector is greater than k. If yes then loop will be terminated
if(v[i]>k) {
flag=1;
break;
}
//counting the current weight the Chef is carrying. If the weight is greater than k then the Chef will drop the item and counter will be incremented by 1.
s += v[i];
if(s>k) {
i--;
s=0;
count++;
}
}
if(flag) cout<<-1<<endl;
else cout<<count<<endl;
}

return 0;
}
43 changes: 43 additions & 0 deletions Dynamic Array/Neha Jhawar/DynamicArraySolution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <bits/stdc++.h>

using namespace std;
vector<int> dynamicArray(int n, vector<vector<int>> q) {
vector<vector<int>> v(n);
vector<int>ar;
int i, la=0, sum;
for(i=0; i<q.size(); i++) {
int bla=la, a, b;
if(q[i][0]==1) {
a = (q[i][1]^la)%n;
v[a].push_back(q[i][2]);
}
else {
a = (q[i][1]^la)%n;
b = q[i][2]%v[a].size();
la = v[a][b];
ar.push_back(la);
}
}
return ar;
}

int main()
{
int n, q;
cin>>n>>q;

vector<vector<int>> queries(q, vector<int>(3));

for (int i = 0; i < q; i++) {
for (int j = 0; j < 3; j++) {
cin>>queries[i][j];
}
}

vector<int> result = dynamicArray(n, queries);

for (int i = 0; i < result.size(); i++) {
cout << result[i]<<endl;
}
return 0;
}
21 changes: 21 additions & 0 deletions Left Rotation/Neha Jhawar/LeftRotationSolution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <bits/stdc++.h>

using namespace std;

int main() {
int n,d;
cin>>n>>d;
//d = d%n;
int a[n],i;
for(i=0; i<n; i++) {
cin>>a[i];
}
for(i=d; i<n; i++)
cout<<a[i]<<" ";

for(i=0; i<d; i++)
cout<<a[i]<<" ";

cout<<endl;
return 0;
}
25 changes: 25 additions & 0 deletions Omkar and Password/Neha Jhawar/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
If all the numbers in the array are equal then the length of shortest password is length of array else answer is always 1.
*/

#include <bits/stdc++.h>
using namespace std;

int main() {
int te;
cin>>te;
while(te--) {
int n, c=0, i;
cin>>n;
vector<int> v(n);
for(i=0; i<n; i++) {
cin>>v[i];
}
for(i=1; i<n; i++) {
if(v[i]==v[0]) c++;
}
if(c==n-1) cout<<n<<endl;
else cout<<1<<endl;
}
return 0;
}
28 changes: 28 additions & 0 deletions Stone/Neha Jhawar/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
First take one stone from the second heap and two stones from the third heap. Then take take one stone from the first heap and two stones from the second heap. Keep track of total count of stones and print it.
*/
#include <bits/stdc++.h>
using namespace std;

int main() {
int te;
cin>>te;
while(te--) {
int a, b, c, i;
cin>>a>>b>>c;
int count=0;
while(c>=2 && b>0) {
count+=3;
c -= 2;
b--;
}
while(b>=2 && a>0) {
count+=3;
b -= 2;
a--;
}
cout<<count<<endl;
}

return 0;
}