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

Create POSAND codechef #9

Open
wants to merge 19 commits 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
30 changes: 30 additions & 0 deletions 2 sum solution
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target)
{
int size = nums.size();

// x, target-x -> add to target.

map<int, int> pos_map;

for(int i=0; i<size;i++)
{
int curr_num = nums[i];

int to_find = target-curr_num;

if (pos_map[to_find]!=0)
{
// currnum + to_find = target
return vector<int>{i, pos_map[to_find]-1};

}

pos_map[curr_num] = i+1;
// done :)

}

return vector<int>{-1,-1};
}
55 changes: 55 additions & 0 deletions 39.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//https://leetcode.com/problems/combination-sum/
//Difficulty Level: Medium
//Tags: Array, Backtracking
//since we have to print all, and not just count or max or min, we won't use DP but backtracking

class Solution {
public:
vector<vector<int>> combinationSum(vector<int>& candidates, int target)
{
sort(candidates.begin(), candidates.end());

//Note that we don't have duplicates in the array, else we had to delete the duplicates here bcoz
//we can already take each element multiple times, so duplicate elements don't make a difference

int index = candidates.size()-1;
while(index >=0 && candidates[index] > target)
{
index--;
}

vector<int> v;
vector<vector<int>> res; //stores result
backtrack(candidates, target, 0, index, v, res);
return res;
}

void backtrack(vector<int> candidates, int target, int curr_sum, int index, vector<int> v, vector<vector<int>>& res)
{
if(curr_sum == target) //if the sum of elements of v add up to target, push v to result vector
{
res.push_back(v);
return;
}

//check all the elements <= target - curr_sum
for(int i=index; i>=0; i--)
{
curr_sum += candidates[i];

if(curr_sum > target) //don't include the element if sum is exceeding the target
{
curr_sum -= candidates[i];
continue;
}
v.push_back(candidates[i]);

//backtrack to find rest of the elements of v
//note that we have passed 'i' and not 'i+1' since we could include the same element any no. of times
backtrack(candidates, target, curr_sum, i, v, res);

curr_sum -= candidates[i];
v.pop_back();
}
}
};
21 changes: 21 additions & 0 deletions 56.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public:
vector<vector<int>> merge(vector<vector<int>>& intervals) {
vector<int>arr(2);
vector<vector<int>>ans;
if(intervals.size()==0) return ans;
sort(intervals.begin(),intervals.end());
arr[0]=intervals[0][0];arr[1]=intervals[0][1];
for(int i=1;i<intervals.size();i++)
{ if(arr[1]>=intervals[i][0])
arr[1]=max(intervals[i][1],arr[1]);
else
{ ans.push_back({arr[0],arr[1]});
arr[0]=intervals[i][0];
arr[1]=intervals[i][1];
}
}
ans.push_back({arr[0],arr[1]});
return ans;
}
};
35 changes: 35 additions & 0 deletions 59.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Solution {
public:

vector<vector<int>> generateMatrix(int n) {
vector<vector<int>> matrix;
matrix.resize(n, std::vector<int>(n, -1));

int top = 0;
int bottom = n-1;
int left = 0;
int right = n-1;
int val = 1;
while (left<=right) {

for (int i = left;i<=right;i++) {
matrix[left][i] = val++;
}
top++;

for (int i = top;i<=bottom;i++) {
matrix[i][right] = val++;
}
right--;
for (int i = right;i>=left;i--) {
matrix[bottom][i] = val++;
}
bottom--;
for (int i = bottom;i>=top;i--) {
matrix[i][left] = val++;
}
left++;
}
return matrix;
}
};
24 changes: 24 additions & 0 deletions 60.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution {
public:
string getPermutation(int n, int k) {
string st(n,'0');
int fact=1;
for(int i=1;i<=n;i++){
fact*=i;
st[i-1]+=(i);
}
for(int i=0;i<n;i++){
int block_num=fact/(n-i);
fact/=(n-i);
int first_index=i+(k-1)/(block_num);
char c=st[first_index];
k=k-(((k-1)/(block_num))*(block_num));
for(int j=first_index;j>=i+1;j--){
st[j]=st[j-1];
}
st[i]=c;
cout<<st<<" "<<k<<" ";
}
return st;
}
};
124 changes: 124 additions & 0 deletions 84.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
class Solution {
public:
int largestRectangleArea(vector<int>& A) {
int n=A.size();
stack<int>s;
vector<int>nsr;
for(int i=n-1;i>=0;i--)
{
if(s.empty())
{
nsr.push_back(n);
}
else if(A[s.top()]<A[i])
{
nsr.push_back(s.top());
}
else if(A[s.top()]>=A[i])
{
while(!s.empty()&&A[s.top()]>=A[i])
{
s.pop();
}
if(s.empty())
{
nsr.push_back(n);
}
else
nsr.push_back(s.top());
}
s.push(i);
}
reverse(nsr.begin(),nsr.end());
stack<int>ss;
vector<int>nsl;
for(int i=0;i<n;i++)
{
if(ss.empty())
{
nsl.push_back(-1);
}
else if(A[ss.top()]<A[i])
{
nsl.push_back(ss.top());
}
else if(A[ss.top()]>=A[i])
{
while(!ss.empty()&&A[ss.top()]>=A[i])
ss.pop();
if(ss.empty())
{
nsl.push_back(-1);
}
else
nsl.push_back(ss.top());
}
ss.push(i);
}
int ans=0;
for(int i=0;i<n;i++)
{
ans=max(ans,(nsr[i]-nsl[i]-1)*A[i]);
}
return ans; n=A.size();
stack<int>s;
vector<int>nsr;
for(int i=n-1;i>=0;i--)
{
if(s.empty())
{
nsr.push_back(n);
}
else if(A[s.top()]<A[i])
{
nsr.push_back(s.top());
}
else if(A[s.top()]>=A[i])
{
while(!s.empty()&&A[s.top()]>=A[i])
{
s.pop();
}
if(s.empty())
{
nsr.push_back(n);
}
else
nsr.push_back(s.top());
}
s.push(i);
}
reverse(nsr.begin(),nsr.end());
stack<int>ss;
vector<int>nsl;
for(int i=0;i<n;i++)
{
if(ss.empty())
{
nsl.push_back(-1);
}
else if(A[ss.top()]<A[i])
{
nsl.push_back(ss.top());
}
else if(A[ss.top()]>=A[i])
{
while(!ss.empty()&&A[ss.top()]>=A[i])
ss.pop();
if(ss.empty())
{
nsl.push_back(-1);
}
else
nsl.push_back(ss.top());
}
ss.push(i);
}
int ans=0;
for(int i=0;i<n;i++)
{
ans=max(ans,(nsr[i]-nsl[i]-1)*A[i]);
}
return ans;
}
};
42 changes: 42 additions & 0 deletions Median_of_two_sorted_arrays.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
class Solution {
public:
double find(vector<int>& nums1, vector<int>& nums2)
{
int n = nums1.size(),m = nums2.size();

int l = 0,h = n;
while(l<=h)
{
int part1= (l+h)/2;
int part2=(n+m+1)/2 - part1;

int l1=(part1==0)? INT_MIN: nums1[part1-1];
int r1= (part1==n)? INT_MAX: nums1[part1];
int l2=(part2==0)? INT_MIN: nums2[part2-1];
int r2= (part2==m)? INT_MAX: nums2[part2];

if(l1<=r2 && l2<=r1)
{
if((n+m)%2)
return (double)max(l1,l2);
else
{
return (double)(max(l1,l2)+ min(r1,r2))/2.0;
}
}

else if(l1>r2)
h=part1-1;
else
l=part1+1;
}
return 0;
}
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int n=nums1.size(),m=nums2.size();
if(n>m)return find(nums2,nums1);
else return find(nums1,nums2);

}

};
56 changes: 56 additions & 0 deletions POSAND codechef
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define test int t; cin>>t; while(t--)
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define f0(i,n) for( ll i = 0; i < ( n ); i ++)
#define f1(i,n) for(i = 1; i <= ( n ); i ++)
#define f(i,m,n) for(auto i = ( m ); i <= ( n ); i ++)
#define nl cout<<"\n"
#define MOD 1000000007
#define fi first
#define se second
#define all(a) a.begin(),a.end()
#define rall(v) v.rbegin(), v.rend()
#define SZ(x) ((ll)x.size())
#define mem(a, b) memset(a, b, sizeof(a));
#define make_graph(k) int x,y; f0(i,k){cin>>x>>y; adj[x].pb(y); adj[y].pb(x);}
#define dekh cout<<"dekh"


typedef vector<ll> vl;

bool isPowerOfTwo (int x)
{
/* First x in the below expression is for the case when x is 0 */
return x && (!(x&(x-1)));
}

int main()
{
fast
test
{
ll n; cin>>n;
if(n==1){cout<<"1";nl;continue;}

if(isPowerOfTwo(n)){cout<<"-1";nl;continue;}

if(n==3){cout<<"2 3 1";nl;continue;}
ll i;
cout<<"2 3 1 ";
for(i=4 ; i<=n ; i++)
{
if(isPowerOfTwo(i)){cout<<i+1<<" ";continue;}

if(isPowerOfTwo(i-1)){cout<<i-1<<" ";continue;}

cout<<i<<" ";
}
nl;
}
}

Loading