Responsive Ads Here
Showing posts with label geeksforgeeks. Show all posts
Showing posts with label geeksforgeeks. Show all posts

Wednesday, 26 July 2017

Largest Permutation


In this problem you need to find the largest permutation of a given array by using at most k swap.
I am using map(in decreasing order ) here because you need to swap the largest element of array with the current index of the orignal array.So first of all insert values into map with there index values.After that traverse from the beginning in the array and compare the top of the map and swap the values. that's all . Happy Coding :P

problem id:-http://practice.geeksforgeeks.org/problems/largest-permutation/0

code:--

#include <bits/stdc++.h>

using namespace std;
#define ll   long long
#define      pii               std::pair<int,int>
#define      vi                std::vector<int>
#define      vll               std::vector<long long>
#define      mp(a,b)           make_pair(a,b)
#define      pb(a)             push_back(a)
#define sc(x)   scanf("%d",&x)
#define scll(x)   scanf("%lld",&x)
#define sc2(x,y)   scanf("%d%d",&x,&y)
#define sc3(x,y,z)   scanf("%d%d%d",&x,&y,&z)
#define     pf(x)   printf("%d ",x)
#define     pf2(x,y)   printf("%d %d ",x,y)
#define     pf3(x,y,z)   printf("%d %d %d ",x,y,z)
#define pfnl()   putchar('\n');
#define      each(it,s)        for(auto it = s.begin(); it != s.end(); ++it)
#define      rep(i, n)         for(int i = 0; i < (n); ++i)
#define rep2(i,j,n)   for(int i = j; i < (n); ++i)
#define      fill(a)           memset(a, 0, sizeof (a))
#define      sortA(v)          sort(v.begin(), v.end())
#define      sortD(v)          sort(v.begin(), v.end(), greater<auto>())
#define      X                 first
#define      Y                 second
#define gc() getchar()

#define debug(x) cerr<<"debug->"<<#x<<"::"<<x<<endl
#define debug2(x,y) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n"
#define debug3(x,y,z) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n"
#define MOD 1000000007

template <typename T> void scan(T &angka){
angka=0;char input=gc();T kali=1;
while(!(48<=input&&input<=57)){ if(input=='-') kali=-1;input=gc();}
while(48<=input&&input<=57) angka=(angka<<3)+(angka<<1)+input-48,input=gc();angka*=kali;
}
int main(int argc, char **argv) {
//std::ios::sync_with_stdio(false);
int t;
sc(t);
int a[10005];
map<int,int,greater<int> > my;
while(t--)
{
int n,k;
sc2(n,k);
my.clear();
rep(i,n) {sc(a[i]); my.insert(mp(a[i],i));}
auto itr=my.begin();
int temp;
for(int i=0;i<n && k;++i)
{
itr=my.begin();
if(itr->X >a[i])
{
temp=a[i];
a[i]=itr->X;
a[itr->Y]=temp;
my[temp]=itr->Y;
--k;
}
my.erase(itr);
}
rep(i,n) pf(a[i]);
putchar('\n');
}
return 0;
}


Thursday, 13 July 2017

Friendly Array


problem id:-http://practice.geeksforgeeks.org/problems/friendly-array/0

code:--

#include <bits/stdc++.h>

using namespace std;
#define  ll       long long
#define      pii                  std::pair<int,int>
#define      vi                   std::vector<int>
#define      vll                  std::vector<long long>
#define      mp(a,b)         make_pair(a,b)
#define      pb(a)             push_back(a)
#define      sc(x)       scanf("%d",&x)
#define  sc2(x,y)       scanf("%d%d",&x,&y)
#define  sc3(x,y,z)      scanf("%d%d%d",&x,&y,&z)
#define      each(it,s)       for(auto it = s.begin(); it != s.end(); ++it)
#define      rep(i, n)         for(int i = 0; i < (n); ++i)
#define      rep2(i,j, n)         for(int i = j; i < (n); ++i)
#define      fill(a)             memset(a, 0, sizeof (a))
#define      sortA(v)         sort(v.begin(), v.end())
#define      sortD(v)         sort(v.begin(), v.end(), greater<auto>())
#define      X                   first
#define      Y                   second

int main(int argc, char **argv) {
//std::ios::sync_with_stdio(false);
int arr[10005];
int t;
sc(t);
while(t--)
{
int n;
sc(n);
rep(i,n)    sc(arr[i]);
sort(arr,arr+n);
if(n==1)    {printf("0\n");continue;}
    int sum=0;
    sum+=arr[1]-arr[0];
    sum+=arr[n-1]-arr[n-2];
rep2(i,1,n-1)    sum+=min(arr[i]-arr[i-1],arr[i+1]-arr[i]);
    printf("%d\n",sum);
}
return 0;
}

Saturday, 8 July 2017

Rightmost different bit

problem id:---http://practice.geeksforgeeks.org/problems/rightmost-different-bit/0

code:--

#include <bits/stdc++.h>

using namespace std;
#define ll   long long
#define      pii               std::pair<int,int>
#define      vi                std::vector<int>
#define      vll               std::vector<long long>
#define      mp(a,b)           make_pair(a,b)
#define      pb(a)             push_back(a)
#define sc(x)   scanf("%d",&x)
#define scll(x)   scanf("%lld",&x)
#define sc2(x,y)   scanf("%d%d",&x,&y)
#define sc3(x,y,z)   scanf("%d%d%d",&x,&y,&z)
#define     pf(x)   printf("%d ",x)
#define     pf2(x,y)   printf("%d %d ",x,y)
#define     pf3(x,y,z)   printf("%d %d %d ",x,y,z)
#define pfnl()   putchar('\n');
#define      each(it,s)        for(auto it = s.begin(); it != s.end(); ++it)
#define      rep(i, n)         for(int i = 0; i < (n); ++i)
#define rep2(i,j,n)   for(int i = j; i < (n); ++i)
#define      fill(a)           memset(a, 0, sizeof (a))
#define      sortA(v)          sort(v.begin(), v.end())
#define      sortD(v)          sort(v.begin(), v.end(), greater<auto>())
#define      X                 first
#define      Y                 second
#define gc() getchar()

#define debug(x) cerr<<"debug->"<<#x<<"::"<<x<<endl
#define debug2(x,y) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n"
#define debug3(x,y,z) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n"
#define MOD 1000000007

template <typename T> void scan(T &angka){
angka=0;char input=gc();T kali=1;
while(!(48<=input&&input<=57)){ if(input=='-') kali=-1;input=gc();}
while(48<=input&&input<=57) angka=(angka<<3)+(angka<<1)+input-48,input=gc();angka*=kali;
}
int main(int argc, char **argv) {
//std::ios::sync_with_stdio(false);
int t;
sc(t);
while(t--)
{
int n,m;
sc2(n,m);
int i=0,ans=-1;
while(n|| m)
{
if((n&1)^(m&1))
{
ans=i+1;
break;
}
n>>=1;
m>>=1;
++i;
}
printf("%d\n",ans);
}
return 0;
}


Sum Terms Nth Row

problem id:--http://practice.geeksforgeeks.org/problems/sum-terms-nth-row/0

#include <bits/stdc++.h>

using namespace std;
#define ll   long long
#define      pii               std::pair<int,int>
#define      vi                std::vector<int>
#define      vll               std::vector<long long>
#define      mp(a,b)           make_pair(a,b)
#define      pb(a)             push_back(a)
#define sc(x)   scanf("%d",&x)
#define scll(x)   scanf("%lld",&x)
#define sc2(x,y)   scanf("%d%d",&x,&y)
#define sc3(x,y,z)   scanf("%d%d%d",&x,&y,&z)
#define     pf(x)   printf("%d ",x)
#define     pf2(x,y)   printf("%d %d ",x,y)
#define     pf3(x,y,z)   printf("%d %d %d ",x,y,z)
#define pfnl()   putchar('\n');
#define      each(it,s)        for(auto it = s.begin(); it != s.end(); ++it)
#define      rep(i, n)         for(int i = 0; i < (n); ++i)
#define rep2(i,j,n)   for(int i = j; i < (n); ++i)
#define      fill(a)           memset(a, 0, sizeof (a))
#define      sortA(v)          sort(v.begin(), v.end())
#define      sortD(v)          sort(v.begin(), v.end(), greater<auto>())
#define      X                 first
#define      Y                 second
#define gc() getchar()

#define debug(x) cerr<<"debug->"<<#x<<"::"<<x<<endl
#define debug2(x,y) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n"
#define debug3(x,y,z) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n"
#define MOD 1000000007

template <typename T> void scan(T &angka){
angka=0;char input=gc();T kali=1;
while(!(48<=input&&input<=57)){ if(input=='-') kali=-1;input=gc();}
while(48<=input&&input<=57) angka=(angka<<3)+(angka<<1)+input-48,input=gc();angka*=kali;
}

int sum(int n)
{
return (n*(n+1))/2;
}
int main(int argc, char **argv) {
//std::ios::sync_with_stdio(false);
int t;
sc(t);
while(t--)
{
int n;
sc(n);
int s=n*(n-1);
int e=s+2*n;
cout<<sum(e)-sum(s)<<'\n';
}
return 0;
}


Wednesday, 5 July 2017

Count pair sum

problem id:--http://practice.geeksforgeeks.org/problems/count-pair-sum/0

code:--

#include <bits/stdc++.h>

using namespace std;
#define ll   long long
#define      pii               std::pair<int,int>
#define      vi                std::vector<int>
#define      vll               std::vector<long long>
#define      mp(a,b)           make_pair(a,b)
#define      pb(a)             push_back(a)
#define sc(x)   scanf("%d",&x)
#define scll(x)   scanf("%lld",&x)
#define sc2(x,y)   scanf("%d%d",&x,&y)
#define sc3(x,y,z)   scanf("%d%d%d",&x,&y,&z)
#define     pf(x)   printf("%d ",x)
#define     pf2(x,y)   printf("%d %d ",x,y)
#define     pf3(x,y,z)   printf("%d %d %d ",x,y,z)
#define pfnl()   putchar('\n');
#define      each(it,s)        for(auto it = s.begin(); it != s.end(); ++it)
#define      rep(i, n)         for(int i = 0; i < (n); ++i)
#define rep2(i,j,n)   for(int i = j; i < (n); ++i)
#define      fill(a)           memset(a, 0, sizeof (a))
#define      sortA(v)          sort(v.begin(), v.end())
#define      sortD(v)          sort(v.begin(), v.end(), greater<auto>())
#define      X                 first
#define      Y                 second
#define gc() getchar()

#define debug(x) cerr<<"debug->"<<#x<<"::"<<x<<endl
#define debug2(x,y) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n"
#define debug3(x,y,z) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n"
#define MOD 1000000007

template <typename T> void scan(T &angka){
angka=0;char input=gc();T kali=1;
while(!(48<=input&&input<=57)){ if(input=='-') kali=-1;input=gc();}
while(48<=input&&input<=57) angka=(angka<<3)+(angka<<1)+input-48,input=gc();angka*=kali;
}
int main(int argc, char **argv) {
//std::ios::sync_with_stdio(false);
int arr[100005];
set<int> myset;
int n,m,num,k;
int t;
sc(t);
while(t--)
{
myset.clear();
sc2(n,m);
rep(i,n) {sc(num); myset.insert(num); }
rep(i,m) sc(arr[i]);
sc(k);
int count=0;
rep(i,m) if(myset.find(k-arr[i])!=myset.end()) ++count;
printf("%d\n",count);
}
return 0;
}


K-th distinct element

problem id:--http://practice.geeksforgeeks.org/problems/k-th-distinct-element/0

code:--

#include <bits/stdc++.h>

using namespace std;
#define ll   long long
#define      pii               std::pair<int,int>
#define      vi                std::vector<int>
#define      vll               std::vector<long long>
#define      mp(a,b)           make_pair(a,b)
#define      pb(a)             push_back(a)
#define sc(x)   scanf("%d",&x)
#define scll(x)   scanf("%lld",&x)
#define sc2(x,y)   scanf("%d%d",&x,&y)
#define sc3(x,y,z)   scanf("%d%d%d",&x,&y,&z)
#define     pf(x)   printf("%d ",x)
#define     pf2(x,y)   printf("%d %d ",x,y)
#define     pf3(x,y,z)   printf("%d %d %d ",x,y,z)
#define pfnl()   putchar('\n');
#define      each(it,s)        for(auto it = s.begin(); it != s.end(); ++it)
#define      rep(i, n)         for(int i = 0; i < (n); ++i)
#define rep2(i,j,n)   for(int i = j; i < (n); ++i)
#define      sortA(v)          sort(v.begin(), v.end())
#define      sortD(v)          sort(v.begin(), v.end(), greater<auto>())
#define      X                 first
#define      Y                 second
#define gc() getchar()

#define debug(x) cerr<<"debug->"<<#x<<"::"<<x<<endl
#define debug2(x,y) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n"
#define debug3(x,y,z) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n"
#define MOD 1000000007

template <typename T> void scan(T &angka){
angka=0;char input=gc();T kali=1;
while(!(48<=input&&input<=57)){ if(input=='-') kali=-1;input=gc();}
while(48<=input&&input<=57) angka=(angka<<3)+(angka<<1)+input-48,input=gc();angka*=kali;
}
int h[100005],arr[100005];
int main(int argc, char **argv) {
//std::ios::sync_with_stdio(false);
int t;
sc(t);
while(t--)
{
fill(h,h+100001,0);
int n,num,k;
sc2(n,k);
rep(i,n) {sc(arr[i]);++h[arr[i]];}
int count=0,ans=-1;
rep(i,n) {if(h[arr[i]]==1) ++count; if(count==k) {ans=arr[i]; break; } }
printf("%d\n",ans);
}
return 0;
}


First Come First Serve

problem id:-http://practice.geeksforgeeks.org/problems/first-come-first-serve/0

code:--

#include <bits/stdc++.h>

using namespace std;
#define ll   long long
#define      pii               std::pair<int,int>
#define      vi                std::vector<int>
#define      vll               std::vector<long long>
#define      mp(a,b)           make_pair(a,b)
#define      pb(a)             push_back(a)
#define sc(x)   scanf("%d",&x)
#define scll(x)   scanf("%lld",&x)
#define sc2(x,y)   scanf("%d%d",&x,&y)
#define sc3(x,y,z)   scanf("%d%d%d",&x,&y,&z)
#define     pf(x)   printf("%d ",x)
#define     pf2(x,y)   printf("%d %d ",x,y)
#define     pf3(x,y,z)   printf("%d %d %d ",x,y,z)
#define pfnl()   putchar('\n');
#define      each(it,s)        for(auto it = s.begin(); it != s.end(); ++it)
#define      rep(i, n)         for(int i = 0; i < (n); ++i)
#define rep2(i,j,n)   for(int i = j; i < (n); ++i)
#define      sortA(v)          sort(v.begin(), v.end())
#define      sortD(v)          sort(v.begin(), v.end(), greater<auto>())
#define      X                 first
#define      Y                 second
#define gc() getchar()

#define debug(x) cerr<<"debug->"<<#x<<"::"<<x<<endl
#define debug2(x,y) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n"
#define debug3(x,y,z) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n"
#define MOD 1000000007

template <typename T> void scan(T &angka){
angka=0;char input=gc();T kali=1;
while(!(48<=input&&input<=57)){ if(input=='-') kali=-1;input=gc();}
while(48<=input&&input<=57) angka=(angka<<3)+(angka<<1)+input-48,input=gc();angka*=kali;
}
int arr[1005],h[100005];//h stands for hashing
int main() {
int t;
sc(t);
while(t--)
{
int n,k;
fill(h,h+100005,0);
sc2(n,k);
rep(i,n) {sc(arr[i]); ++h[arr[i]];}// we save every element frequency in array h
int ans=-1;
rep(i,n) if(h[arr[i]]==k) {ans=arr[i]; break;}
printf("%d\n",ans);
}
return 0;
}


Pair array product sum

problem id:--http://practice.geeksforgeeks.org/problems/pair-array-product-sum/0

code:--

#include <bits/stdc++.h>

using namespace std;
#define ll   long long
#define      pii               std::pair<int,int>
#define      vi                std::vector<int>
#define      vll               std::vector<long long>
#define      mp(a,b)           make_pair(a,b)
#define      pb(a)             push_back(a)
#define sc(x)   scanf("%d",&x)
#define scll(x)   scanf("%lld",&x)
#define sc2(x,y)   scanf("%d%d",&x,&y)
#define sc3(x,y,z)   scanf("%d%d%d",&x,&y,&z)
#define     pf(x)   printf("%d ",x)
#define     pf2(x,y)   printf("%d %d ",x,y)
#define     pf3(x,y,z)   printf("%d %d %d ",x,y,z)
#define pfnl()   putchar('\n');
#define      each(it,s)        for(auto it = s.begin(); it != s.end(); ++it)
#define      rep(i, n)         for(int i = 0; i < (n); ++i)
#define rep2(i,j,n)   for(int i = j; i < (n); ++i)
#define      fill(a)           memset(a, 0, sizeof (a))
#define      sortA(v)          sort(v.begin(), v.end())
#define      sortD(v)          sort(v.begin(), v.end(), greater<auto>())
#define      X                 first
#define      Y                 second
#define gc() getchar()

#define debug(x) cerr<<"debug->"<<#x<<"::"<<x<<endl
#define debug2(x,y) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n"
#define debug3(x,y,z) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n"
#define MOD 1000000007

template <typename T> void scan(T &angka){
angka=0;char input=gc();T kali=1;
while(!(48<=input&&input<=57)){ if(input=='-') kali=-1;input=gc();}
while(48<=input&&input<=57) angka=(angka<<3)+(angka<<1)+input-48,input=gc();angka*=kali;
}
int main(int argc, char **argv) {
//std::ios::sync_with_stdio(false);
int t;
sc(t);
while(t--)
{
int n,num;
sc(n);
int count1=0,count2=0;
rep(i,n) {sc(num); if(num==1) ++count1; else if(num==2) ++count2; }
n-=count1;
ll Ans=count2*(n-count2) + ((ll)(n-count2)*(n-count2-1))/2;
printf("%lld\n",Ans);
}
return 0;
}


Tuesday, 4 July 2017

Balance with respect to an array

problem id:--http://practice.geeksforgeeks.org/problems/balance-with-respect-to-an-array/0

code:--

#include <bits/stdc++.h>

using namespace std;
#define ll   long long
#define      pii               std::pair<int,int>
#define      vi                std::vector<int>
#define      vll               std::vector<long long>
#define      mp(a,b)           make_pair(a,b)
#define      pb(a)             push_back(a)
#define sc(x)   scanf("%d",&x)
#define scll(x)   scanf("%lld",&x)
#define sc2(x,y)   scanf("%d%d",&x,&y)
#define sc3(x,y,z)   scanf("%d%d%d",&x,&y,&z)
#define     pf(x)   printf("%d ",x)
#define     pf2(x,y)   printf("%d %d ",x,y)
#define     pf3(x,y,z)   printf("%d %d %d ",x,y,z)
#define pfnl()   putchar('\n');
#define      each(it,s)        for(auto it = s.begin(); it != s.end(); ++it)
#define      rep(i, n)         for(int i = 0; i < (n); ++i)
#define rep2(i,j,n)   for(int i = j; i < (n); ++i)
#define      fill(a)           memset(a, 0, sizeof (a))
#define      sortA(v)          sort(v.begin(), v.end())
#define      sortD(v)          sort(v.begin(), v.end(), greater<auto>())
#define      X                 first
#define      Y                 second
#define gc() getchar()

#define debug(x) cerr<<"debug->"<<#x<<"::"<<x<<endl
#define debug2(x,y) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n"
#define debug3(x,y,z) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n"
#define MOD 1000000007

template <typename T> void scan(T &angka){
angka=0;char input=gc();T kali=1;
while(!(48<=input&&input<=57)){ if(input=='-') kali=-1;input=gc();}
while(48<=input&&input<=57) angka=(angka<<3)+(angka<<1)+input-48,input=gc();angka*=kali;
}
int arr[(int)1e5+5];
int binary_floor(int num,int n)
{
int l=0,r=n,mid;
while(l<=r)
{
mid=(l+r)/2;
if(arr[mid]==num) return -1;
if(arr[mid]<num) l=mid+1;
else r=mid-1;
}
if(arr[mid]>num)   --mid; // here we double check the condition for floor  if its true then we decrease 1
return mid;
}

int binary_ceil(int num,int n)
{
int l=0,r=n,mid;
while(l<=r)
{
mid=(l+r)/2;
if(arr[mid]==num) return -1;
if(arr[mid]>num) r=mid-1;
else l=mid+1;
}
if(arr[mid]<num) ++mid;   // here we double check the condition if its true then we increase 1
return mid;
}

int main() {
string ans[]={"Not Balanced\n","Balanced\n"};
int t;
sc(t);
while(t--)
{
int n;
sc(n);
rep(i,n) sc(arr[i]);
int num;
sc(num);
bool flag;
if(num<arr[0] || num>arr[n-1]) flag=1;
else
{
int l,r;
l=binary_floor(num,n-1);
r=binary_ceil(num,n-1);
if(l==-1 || r==-1) flag=1;
else flag=(2*num==arr[l]+arr[r]);//This condition is equvalent to (x-floor(x))==(ceil(x)-x)
}
cout<<ans[flag];
}
return 0;
}

Tuesday, 27 June 2017

Minimum move to front operations

problem id:--http://practice.geeksforgeeks.org/problems/minimum-move-to-front-operations/0

code:--
#include <bits/stdc++.h>

using namespace std;
#define ll   long long
#define      pii               std::pair<int,int>
#define      vi                std::vector<int>
#define      vll               std::vector<long long>
#define      mp(a,b)           make_pair(a,b)
#define      pb(a)             push_back(a)
#define sc(x)   scanf("%d",&x)
#define scll(x)   scanf("%lld",&x)
#define sc2(x,y)   scanf("%d%d",&x,&y)
#define sc3(x,y,z)   scanf("%d%d%d",&x,&y,&z)
#define     pf(x)   printf("%d ",x)
#define     pf2(x,y)   printf("%d %d ",x,y)
#define     pf3(x,y,z)   printf("%d %d %d ",x,y,z)
#define pfnl()   putchar('\n');
#define      each(it,s)        for(auto it = s.begin(); it != s.end(); ++it)
#define      rep(i, n)         for(int i = 0; i < (n); ++i)
#define rep2(i,j,n)   for(int i = j; i < (n); ++i)
#define      fill(a)           memset(a, 0, sizeof (a))
#define      sortA(v)          sort(v.begin(), v.end())
#define      sortD(v)          sort(v.begin(), v.end(), greater<auto>())
#define      X                 first
#define      Y                 second
#define gc() getchar()

#define debug(x) cerr<<"debug->"<<#x<<"::"<<x<<endl
#define debug2(x,y) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n"
#define debug3(x,y,z) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n"
#define MOD 1000000007

template <typename T> void scan(T &angka){
angka=0;char input=gc();T kali=1;
while(!(48<=input&&input<=57)){ if(input=='-') kali=-1;input=gc();}
while(48<=input&&input<=57) angka=(angka<<3)+(angka<<1)+input-48,input=gc();angka*=kali;
}

int arr[0x65];

int main(int argc, char **argv) {
//std::ios::sync_with_stdio(false);
int t;
scan(t);
while(t--)
{
int n,num;
scan(n);
rep2(i,1,n+1) { scan(num); arr[num]=i;}// save every element index in array
int Ans=0,count=0;
for(int i=n-1;i>=1;--i) if(arr[i]>arr[i+1]) ++Ans,arr[i]=-Ans;
        //put new element index (for which the condition holds true) to be minimum
pf(Ans);
pfnl();
}
return 0;
}


Monday, 26 June 2017

Maximize Dot Product

problem id:;--http://practice.geeksforgeeks.org/problems/maximize-dot-product/0

code:--

#include <bits/stdc++.h>

using namespace std;
#define ll   long long
#define      pii               std::pair<int,int>
#define      vi                std::vector<int>
#define      vll               std::vector<long long>
#define      mp(a,b)           make_pair(a,b)
#define      pb(a)             push_back(a)
#define sc(x)   scanf("%d",&x)
#define scll(x)   scanf("%lld",&x)
#define sc2(x,y)   scanf("%d%d",&x,&y)
#define sc3(x,y,z)   scanf("%d%d%d",&x,&y,&z)
#define     pf(x)   printf("%d ",x)
#define     pf2(x,y)   printf("%d %d ",x,y)
#define     pf3(x,y,z)   printf("%d %d %d ",x,y,z)
#define pfnl()   putchar('\n');
#define      each(it,s)        for(auto it = s.begin(); it != s.end(); ++it)
#define      rep(i, n)         for(int i = 0; i < (n); ++i)
#define rep2(i,j,n)   for(int i = j; i < (n); ++i)
#define      fill(a)           memset(a, 0, sizeof (a))
#define      sortA(v)          sort(v.begin(), v.end())
#define      sortD(v)          sort(v.begin(), v.end(), greater<auto>())
#define      X                 first
#define      Y                 second
#define gc() getchar()

#define debug(x) cerr<<"debug->"<<#x<<"::"<<x<<endl
#define debug2(x,y) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n"
#define debug3(x,y,z) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n"
#define MOD 1000000007

template <typename T> void scan(T &angka){
angka=0;char input=gc();T kali=1;
while(!(48<=input&&input<=57)){ if(input=='-') kali=-1;input=gc();}
while(48<=input&&input<=57) angka=(angka<<3)+(angka<<1)+input-48,input=gc();angka*=kali;
}

ll a[0x405],b[0x405];
ll dp[0x405][0x405];
ll Magic(ll a[],ll b[],int n,int m){
rep(i,n+1)  rep(j,n+1)  dp[i][j]=0;
for(int i=1;i<=n;++i)
for(int j=i;j<=m;++j)
dp[i][j]=max(dp[i][j-1],dp[i-1][j-1]+b[j-1]*a[i-1]);
//Multiply every element of second array with first array
//Save corresponding index values of product into dp
return dp[n][m];
}

int main(int argc, char **argv) {
//std::ios::sync_with_stdio(false);
int t;
sc(t);
while(t--)
{
int n,m;
sc2(n,m);
rep(i,n) scan(a[i]);
rep(i,m) scan(b[i]);
ll Ans=(n>m)?Magic(b,a,m,n):Magic(a,b,n,m);// Put smaller array first
printf("%lld\n",Ans);
}
return 0;
}

Minimum Points To Reach Destination

problem id:--http://practice.geeksforgeeks.org/problems/minimum-points-to-reach-destination/0

code:--

#include <bits/stdc++.h>

using namespace std;
#define ll   long long
#define      pii               std::pair<int,int>
#define      vi                std::vector<int>
#define      vll               std::vector<long long>
#define      mp(a,b)           make_pair(a,b)
#define      pb(a)             push_back(a)
#define sc(x)   scanf("%d",&x)
#define scll(x)   scanf("%lld",&x)
#define sc2(x,y)   scanf("%d%d",&x,&y)
#define sc3(x,y,z)   scanf("%d%d%d",&x,&y,&z)
#define     pf(x)   printf("%d ",x)
#define     pf2(x,y)   printf("%d %d ",x,y)
#define     pf3(x,y,z)   printf("%d %d %d ",x,y,z)
#define pfnl()   putchar('\n');
#define      each(it,s)        for(auto it = s.begin(); it != s.end(); ++it)
#define      rep(i, n)         for(int i = 0; i < (n); ++i)
#define rep2(i,j,n)   for(int i = j; i < (n); ++i)
#define      fill(a)           memset(a, 0, sizeof (a))
#define      sortA(v)          sort(v.begin(), v.end())
#define      sortD(v)          sort(v.begin(), v.end(), greater<auto>())
#define      X                 first
#define      Y                 second
#define gc() getchar()

#define debug(x) cerr<<"debug->"<<#x<<"::"<<x<<endl
#define debug2(x,y) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n"
#define debug3(x,y,z) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n"
#define MOD 1000000007

template <typename T> void scan(T &angka){
angka=0;char input=gc();T kali=1;
while(!(48<=input&&input<=57)){ if(input=='-') kali=-1;input=gc();}
while(48<=input&&input<=57) angka=(angka<<3)+(angka<<1)+input-48,input=gc();angka*=kali;
}
int a[0x10][0x10];
int r,c,ans,ans_min;
void magic(int i,int j,int Max,int Min){
if(i==r && j==c)
{
Max+=a[i][j];
Min=min(Min,Max);
if(ans_min<=Min ) ans_min=Min;
return;
// Reaching at the end and then calculating which path have the lowest minimum
}
if(i==r+1 ||j==c+1) return;
magic(i,j+1,Max+a[i][j],min(Max+a[i][j],Min));
magic(i+1,j,Max+a[i][j],min(Max+a[i][j],Min));
}
int main(int argc, char **argv) {
//std::ios::sync_with_stdio(false);
rep(i,0x10) a[0][i]=-40;
rep(i,0x10) a[i][0]=-40;
a[0][1]=0,a[1][0]=0;
int t;
sc(t);
while(t--)
{
int Max,Min;
sc2(r,c);
rep2(i,1,r+1) rep2(j,1,c+1) sc(a[i][j]);
Min=INT_MAX,Max=0,ans_min=-INT_MAX;
magic(1,1,Max,Min);
ans=(ans_min>0)?1:-ans_min+1;
pf(ans);
pfnl();
}
return 0;
}

Friday, 23 June 2017

Maximize The Array

problem id:--http://practice.geeksforgeeks.org/problems/maximize-the-array/0

code:--

#include <bits/stdc++.h>

using namespace std;
#define ll   long long
#define      pii               std::pair<int,int>
#define      vi                std::vector<int>
#define      vll               std::vector<long long>
#define      mp(a,b)           make_pair(a,b)
#define      pb(a)             push_back(a)
#define sc(x)   scanf("%d",&x)
#define scll(x)   scanf("%lld",&x)
#define sc2(x,y)   scanf("%d%d",&x,&y)
#define sc3(x,y,z)   scanf("%d%d%d",&x,&y,&z)
#define     pf(x)   printf("%d ",x)
#define     pf2(x,y)   printf("%d %d ",x,y)
#define     pf3(x,y,z)   printf("%d %d %d ",x,y,z)
#define pfnl()   putchar('\n');
#define      each(it,s)        for(auto it = s.begin(); it != s.end(); ++it)
#define      rep(i, n)         for(int i = 0; i < (n); ++i)
#define rep2(i,j,n)   for(int i = j; i < (n); ++i)
#define      fill(a)           memset(a, 0, sizeof (a))
#define      sortA(v)          sort(v.begin(), v.end())
#define      sortD(v)          sort(v.begin(), v.end(), greater<auto>())
#define      X                 first
#define      Y                 second

#define debug(x) cerr<<"debug->"<<#x<<"::"<<x<<endl
#define debug2(x,y) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n"
#define debug3(x,y,z) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n"
#define MOD 1000000007

int main(int argc, char **argv) {
//std::ios::sync_with_stdio(false);
int a[0xa],b[0xa];
bool hash[0xa];
vi ans(10);
int t;
sc(t);
while(t--)
{
ans.clear();
fill(hash);
int n;
sc(n);
rep(i,n) {sc(a[i]); hash[a[i]]=1;}
rep(i,n) {sc(b[i]); hash[b[i]]=1;}
int j=10,count=0;
while(j>=0 && count!=n){
   --j;
if(hash[j]) ++count;
}
rep(i,n) if(hash[b[i]] && j<=b[i])   {pf(b[i]); hash[b[i]]=0;   }
rep(i,n) if(hash[a[i]] && j<=a[i])   {pf(a[i]); hash[a[i]]=0;   }
pfnl();
}
return 0;
}

Keypad typing

problem id:--http://practice.geeksforgeeks.org/problems/keypad-typing/0

code:--

#include <stdio.h>

int main() {
int t,i;
char str[100];
scanf("%d ",&t);
while(t--)
    {
    scanf("%s ",str);
    i=0;
    while(str[i]!='\0')
        {
        switch(str[i])
            {
            case 'a':
            case 'b':
            case 'c': printf("2");
                      break;
            case 'd':
            case 'e':
            case 'f':printf("3");
                      break;
            case 'g':
            case 'h':
            case 'i':printf("4");
                      break;
            case 'j':
            case 'k':
            case 'l':printf("5");
                      break;
            case 'm':
            case 'n':
            case 'o':printf("6");
                      break;
            case 'p':
            case 'q':
            case 'r':
            case 's':printf("7");
                      break;
            case 't':
            case 'u':
            case 'v':printf("8");
                      break;        
            case 'w':
            case 'x':
            case 'y':
            case 'z':printf("9");
                      break;
            }
        i++;
        }
    printf("\n");
    }
    return 0;
}

Tuesday, 20 June 2017

Count the elements

problem id:--http://practice.geeksforgeeks.org/problems/count-the-elements/0

code:--

#include <bits/stdc++.h>

using namespace std;
#define ll   long long
#define      pii               std::pair<int,int>
#define      vi                std::vector<int>
#define      vll               std::vector<long long>
#define      mp(a,b)           make_pair(a,b)
#define      pb(a)             push_back(a)
#define sc(x)   scanf("%d",&x)
#define scll(x)   scanf("%lld",&x)
#define sc2(x,y)   scanf("%d%d",&x,&y)
#define sc3(x,y,z)   scanf("%d%d%d",&x,&y,&z)
#define     pf(x)   printf("%d ",x)
#define     pf2(x,y)   printf("%d %d ",x,y)
#define     pf3(x,y,z)   printf("%d %d %d ",x,y,z)
#define pfnl()   putchar('\n');
#define      each(it,s)        for(auto it = s.begin(); it != s.end(); ++it)
#define      rep(i, n)         for(int i = 0; i < (n); ++i)
#define rep2(i,j,n)   for(int i = j; i < (n); ++i)
#define      fill(a)           memset(a, 0, sizeof (a))
#define      sortA(v)          sort(v.begin(), v.end())
#define      sortD(v)          sort(v.begin(), v.end(), greater<auto>())
#define      X                 first
#define      Y                 second

#define debug(x) cerr<<"debug->"<<#x<<"::"<<x<<endl
#define debug2(x,y) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n"
#define debug3(x,y,z) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n"
#define MOD 1000000007

int main(int argc, char **argv) {
//std::ios::sync_with_stdio(false);
int a[105],b[105];
int t;
sc(t);
while(t--)
{
int n;
sc(n);// Read n
rep(i,n) sc(a[i]); // Take input in array
int num;
fill(b);
rep(i,n) {sc(num); ++b[num]; }// Take frequency of second array into "b" Array
rep2(i,1,101) b[i]+=b[i-1];// Find commulatice frequency of array b
printf("%d",b[a[0]]);
rep2(i,1,n) printf(",%d",b[a[i]]);  //Print According to First array
pfnl();
}
return 0;
}

Find Smallest Missing in Sorted

problem id:--http://practice.geeksforgeeks.org/problems/find-smallest-missing-in-sorted/0

code:--

#include <bits/stdc++.h>

using namespace std;
#define ll   long long
#define      pii               std::pair<int,int>
#define      vi                std::vector<int>
#define      vll               std::vector<long long>
#define      mp(a,b)           make_pair(a,b)
#define      pb(a)             push_back(a)
#define sc(x)   scanf("%d",&x)
#define scll(x)   scanf("%lld",&x)
#define sc2(x,y)   scanf("%d%d",&x,&y)
#define sc3(x,y,z)   scanf("%d%d%d",&x,&y,&z)
#define     pf(x)   printf("%d ",x)
#define     pf2(x,y)   printf("%d %d ",x,y)
#define     pf3(x,y,z)   printf("%d %d %d ",x,y,z)
#define pfnl()   putchar('\n');
#define      each(it,s)        for(auto it = s.begin(); it != s.end(); ++it)
#define      rep(i, n)         for(int i = 0; i < (n); ++i)
#define rep2(i,j,n)   for(int i = j; i < (n); ++i)
#define      fill(a)           memset(a, 0, sizeof (a))
#define      sortA(v)          sort(v.begin(), v.end())
#define      sortD(v)          sort(v.begin(), v.end(), greater<auto>())
#define      X                 first
#define      Y                 second

#define debug(x) cerr<<"debug->"<<#x<<"::"<<x<<endl
#define debug2(x,y) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n"
#define debug3(x,y,z) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n"
#define MOD 1000000007

int binary(int a[],int n,int m){
int mid,l=0,r=n-1;
while(l<=r){
mid=(l+r)/2;
if(a[mid]==mid && a[mid+1]!=mid+1) return mid+1;
                //we want  to find an element upto which a[mid]=mid and then the next element is our answer
if(a[mid]==mid) l=mid+1;
else r=mid-1;
}
return mid;
}

int main(int argc, char **argv) {
//std::ios::sync_with_stdio(false);
int arr[1005];
int t;
sc(t);
while(t--)
{
int n,m;
sc2(n,m);
rep(i,n) sc(arr[i]);
int ans;
if(arr[0]) ans=0;
else if(arr[n-1]==n-1)  ans=n;
    else ans=binary(arr,n,m);
pf(ans);
pfnl();
}
return 0;
}

Four Elements

problem id:--http://practice.geeksforgeeks.org/problems/four-elements/0

code:--

#include <bits/stdc++.h>

using namespace std;
#define ll   long long
#define      pii               std::pair<int,int>
#define      vi                std::vector<int>
#define      vll               std::vector<long long>
#define      mp(a,b)           make_pair(a,b)
#define      pb(a)             push_back(a)
#define sc(x)   scanf("%d",&x)
#define scll(x)   scanf("%lld",&x)
#define sc2(x,y)   scanf("%d%d",&x,&y)
#define sc3(x,y,z)   scanf("%d%d%d",&x,&y,&z)
#define     pf(x)   printf("%d ",x)
#define     pf2(x,y)   printf("%d %d ",x,y)
#define     pf3(x,y,z)   printf("%d %d %d ",x,y,z)
#define pfnl()   putchar('\n');
#define      each(it,s)        for(auto it = s.begin(); it != s.end(); ++it)
#define      rep(i, n)         for(int i = 0; i < (n); ++i)
#define rep2(i,j,n)   for(int i = j; i < (n); ++i)
#define      fill(a)           memset(a, 0, sizeof (a))
#define      sortA(v)          sort(v.begin(), v.end())
#define      sortD(v)          sort(v.begin(), v.end(), greater<auto>())
#define      X                 first
#define      Y                 second

#define debug(x) cerr<<"debug->"<<#x<<"::"<<x<<endl
#define debug2(x,y) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n"
#define debug3(x,y,z) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n"
#define MOD 1000000007

int binary_loc(int arr[],int n,int sum){
int mid,l=0,r=n-1;
while(l<=r){
mid=(l+r)/2;
if(arr[mid]<sum) l=mid+1;
else r=mid-1;
}
return mid;

}

bool find_sum(int arr[],int n,int sum){
    unordered_set<int> mymap;
rep(i,n-3){
rep2(j,i+1,n-1){
   mymap.clear();
rep2(k,j+1,n){
int temp=arr[i]+arr[j]+arr[k];
if(mymap.find(-temp)!=mymap.end()) return 1;
mymap.insert(arr[k]-sum);
}
}
}

return 0;
}

int main(int argc, char **argv) {
//std::ios::sync_with_stdio(false);
int arr[105];
int t;
sc(t);
while(t--)
{
int n,sum;
sc(n);
rep(i,n) sc(arr[i]);
sc(sum);
sort(arr,arr+n);//sorting is better than comparing in O(n^3)
//debug(binary_loc(arr,n,sum));
//we'll use binary_loc to find the upper bound upto which we have check in arrya
//that means binary_loc returns the index of an array such that
//arr[binary_loc()] val is greater than sum or equal to sum
// and we don't want to compare beyond sum value in array
bool ans=find_sum(arr,binary_loc(arr,n,sum)+1,sum);
cout<<ans;
pfnl();
}
return 0;
}

Sunday, 18 June 2017

Longest Palindromic Subsequence

problem id:--http://practice.geeksforgeeks.org/problems/longest-palindromic-subsequence/0

code:--

#include <bits/stdc++.h>

using namespace std;
#define ll   long long
#define      pii               std::pair<int,int>
#define      vi                std::vector<int>
#define      vll               std::vector<long long>
#define      mp(a,b)           make_pair(a,b)
#define      pb(a)             push_back(a)
#define sc(x)   scanf("%d",&x)
#define scll(x)   scanf("%lld",&x)
#define sc2(x,y)   scanf("%d%d",&x,&y)
#define sc3(x,y,z)   scanf("%d%d%d",&x,&y,&z)
#define     pf(x)   printf("%d ",x)
#define     pf2(x,y)   printf("%d %d ",x,y)
#define     pf3(x,y,z)   printf("%d %d %d ",x,y,z)
#define pfnl()   putchar('\n');
#define      each(it,s)        for(auto it = s.begin(); it != s.end(); ++it)
#define      rep(i, n)         for(int i = 0; i < (n); ++i)
#define rep2(i,j,n)   for(int i = j; i < (n); ++i)
//#define      fill(a)           memset(a, 0, sizeof (a))
#define      sortA(v)          sort(v.begin(), v.end())
#define      sortD(v)          sort(v.begin(), v.end(), greater<auto>())
#define      X                 first
#define      Y                 second

#define debug(x) cerr<<"debug->"<<#x<<"::"<<x<<endl
#define debug2(x,y) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n"
#define debug3(x,y,z) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n"
#define MOD 1000000007
int dp[1005][1005];

int palind_substr(string str)
{
int n=str.length();
rep(i,n) rep(j,n) dp[i][j]=0;
rep(i,n) dp[i][i]=1;
int l=1;
while(l<n)
{
int i=0;
while(i<n-l)
{
if(str[i]==str[i+l]) {dp[i][i+l]=2; if(l>1) dp[i][i+l]+=dp[i+1][i+l-1];}
else dp[i][i+l]=max(dp[i][i+l-1],dp[i+1][i+l]);
++i;
}
++l;
}
//rep(i,n) {rep(j,n) pf(dp[i][j]); cout<<'\n'; }
return dp[0][n-1];
}

int main(int argc, char **argv) {
//std::ios::sync_with_stdio(false);
int t;
sc(t);
while(t--)
{
string str;
cin>>str;
int ans=palind_substr(str);
cout<<ans<<'\n';
}
return 0;
}