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

Monday, 19 June 2017

Day of the Programmer

problem id:--https://www.hackerrank.com/challenges/day-of-the-programmer/problem

code:--

#include <iostream>
using namespace std;

bool isleap_julian(int n)
{
return (n%400==0);
}

bool isleap_gregorian(int n)
{
return ((n%400==0) || ( n%100!=0 && n%4==0));
}

int main() {
int day=13,month=9,year;
cin>>year;
if(year<1918)  day+=-isleap_julian(year);
else if(year==1918) day+=13-isleap_gregorian(year);
else day+=-isleap_gregorian(year);
printf("%d.0%d.%d",day,month,year);
return 0;
}

Non-Divisible Subset

problem id:--https://www.hackerrank.com/challenges/non-divisible-subset/problem

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 arr[100005];
int freq[105]={0};
int n,k;
sc2(n,k);
rep(i,n) sc(arr[i]);
rep(i,n) arr[i]=arr[i]%k , ++freq[arr[i]];
int upper_bound=ceil(k/2.0);
int ans=0;
//rep(i,n) pf(freq[i]);
ans=(!(k&1) && freq[upper_bound]);
ans+=(freq[0]!=0);
//debug(upper_bound);
rep2(i,1,upper_bound){
//debug2(freq[i],freq[k-i]);
ans+=max(freq[i],freq[k-i]);
}
printf("%d\n",ans);

return 0;
}

Sunday, 18 June 2017

Transform to Palindrome

problem id:--https://www.hackerrank.com/contests/w33/challenges/transform-to-palindrome

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 graph[100005];
int dp[1005][1005];
int arr[1005];


int root(int id)
{
while(id!=graph[id])
id=graph[id];
return id;
}

bool isconnected(int n,int m)
{
return (root(n)==root(m));
}

void connect(int n,int m)
{
int loc=root(n);
graph[loc]=root(m);
//debug3(loc,n,a[loc]);
}

int palind_substr(int n)
{
//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(graph[arr[i]]==graph[arr[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 n,k,m;
sc3(n,k,m);
rep2(i,1,n+1) graph[i]=i;
int num1,num2;
while(k--)
{
cin>>num1>>num2;
if(!isconnected(num1,num2))
connect(num1,num2);
}
rep2(i,1,n+1) {int temp=i;graph[temp]=root(temp); }
rep(i,m) sc(arr[i]);
int ans=palind_substr(m);
//rep2(i,1,n+1) pf(graph[i]);
pf(ans);
return 0;
}

Thursday, 15 June 2017

Counting Valleys

problem id:--https://www.hackerrank.com/challenges/counting-valleys

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);
map<char,int> m;
m['U']=1;
m['D']=-1;
int n;
sc(n);
int diff=0;
string str;
cin>>str;
int i=0,count=0;
while(str[i])
    {
    diff+=m[str[i]];
    if(diff==0 && str[i]=='U')
        ++count;
    ++i;
    }
pf(count);
return 0;
}

Wednesday, 14 June 2017

Sequence Equation

problem id:-https://www.hackerrank.com/challenges/permutation-equation

code:--
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include<map>
using namespace std;


int main() {
    int n,num;
    map<int,int> m;
    scanf("%d",&n);
    for(int i=1;i<=n;++i)
    {
        scanf("%d",&num);
        m.insert(make_pair(num,i));
    }
    for(auto itr=m.begin();itr!=m.end();++itr)
    {
        printf("%d \n",m[itr->second]);
    }
       
    return 0;
}

Flipping the Matrix

problem id:--https://www.hackerrank.com/challenges/flipping-the-matrix

code:--

// c++ 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 arr[260][260];
int q;
sc(q);
int n;
while(q--)
{
sc(n);
rep(i,2*n) rep(j,2*n) sc(arr[i][j]);
int ans=0;
rep(i,n) rep(j,n) ans+=max(arr[2*n-i-1][2*n-j-1],max(arr[i][j],max(arr[i][2*n-j-1],arr[2*n-i-1][j])));
pf(ans);
pfnl();
}
return 0;
}



//java code:-

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
int[][] arr=new int[260][260];
int q=sc.nextInt();
int n;
while(q-- >0)
{
n=sc.nextInt();
for(int i=0;i<2*n;++i)
{
for(int j=0;j<2*n;++j)
{
arr[i][j]=sc.nextInt();
}
}
int ans=0;
for(int i=0;i<n;++i)
{
for(int j=0;j<n;++j)
{
ans+=Math.max(Math.max(arr[i][j],arr[2*n-i-1][j]),Math.max(arr[i][2*n-j-1],arr[2*n-i-1][2*n-j-1]));
}
}
System.out.println(ans);
}
  }
}

The Full Counting Sort


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() {
//std::ios::sync_with_stdio(false);
int n;
sc(n);
vector<int> arr(n);
vector<string> str(n);
vector<string> ans(n);
int hash[1000005]={0};
rep(i,n)    { sc(arr[i]); ++hash[arr[i]]; cin>>str[i];if(2*i<(n-1)) str[i]="-";  }
rep2(i,1,n) hash[i]+=hash[i-1];
for(int i=n-1;i>=0;--i)
    {
    ans[hash[arr[i]]-1]=str[i];
    --hash[arr[i]];
}
for(int i=0;i<n;++i)
    cout<<ans[i]<<" ";
return 0;
}

Friday, 9 June 2017

Find the Running Median using heap(priority queue)

problem id:--https://www.hackerrank.com/challenges/find-the-running-median
                     http://practice.geeksforgeeks.org/problems/find-median-in-a-stream/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 all(v)   v.begin(),v.end()
#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
priority_queue<double> l,r;
int  n1,n2;
double root;
void magic()
{
if(n1!=0 && n2!=0)
{
if(root<min(l.top(),-r.top()) || root>max(l.top(),-r.top()))
{
int temp=root;
root=l.top();
l.pop();
l.push(temp);
}
}
if(n1+2==n2)
{
l.push(root);
root=-r.top();
r.pop();
--n2;
++n1;
}
else if(n1==2+n2)
{
r.push(-root);
root=l.top();
l.pop();
--n1,++n2;
}
}
int main(int argc, char **argv) {
n1=0,n2=0;
int n;
double num,temp;
cin>>n;
--n;
cin>>root;
cout<<root<<"\n";
while(n--)
{
temp=0;
cin>>num;
if(root>num){l.push(num);++n1; }
else{r.push(-num);++n2;}
magic();
if(n1==n2) {cout<<root<<"\n";continue;}
temp=(n1>n2)? l.top():-r.top();
cout<<(root+temp)/2;
cout<<endl;
}
return 0;
}

Saturday, 13 May 2017

Breaking the Records

problem id:-https://www.hackerrank.com/challenges/breaking-best-and-worst-records

code:--

#include <bits/stdc++.h>

using namespace std;

vector < int > getRecord(vector < int > s){
    int min=s[0],max=s[0];
    vector<int> v(2);
    v[0]=0,v[1]=0;
    for(vector<int> ::iterator itr=s.begin();itr!=s.end();++itr)
        {
        if(min>*itr)
            min=*itr,++v[1];
        else if(max<*itr)
            max=*itr,++v[0];
        }
    return v;
}

int main() {
    int n;
    cin >> n;
    vector<int> s(n);
    for(int s_i = 0; s_i < n; s_i++){
       cin >> s[s_i];
    }
    vector < int > result = getRecord(s);
    string separator = "", delimiter = " ";
    for(auto val: result) {
        cout<<separator<<val;
        separator = delimiter;
    }
    cout<<endl;
    return 0;
}

rotate the array for any number of times using a trick

problem id:--https://www.hackerrank.com/challenges/linkedin-practice-array-left-rotation

code:--

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
   int n,d;
    cin>>n>>d;// d is the number of times we want  to rotate the array
    int a[n];
    for(int i=0;i<n;++i)
        cin>>a[i];
// we only need to keep track of the first element after d rotation
// and that will be dth index
//so for any other index we add i
//i.e if we want to find ith index after d rotations then that will be (d+i)%n  index
    for(int i=0;i<n;++i)
        cout<<a[(d+i)%n]<<" ";
    return 0;
}

Factorial of large number (even 100!) using array

code:--

#include<iostream>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int a[500]={0};
a[0]=1;
int l=1;
int carry=0,num;
for(int i=2;i<=n;++i)
{
for(int j=0;j<l;++j)
{
num=a[j]*i+carry;
a[j]=num%10;
carry=num/10;
}
while(carry)
a[l]=carry%10,++l,carry/=10;
}
for(int i=l-1;i>=0;--i)
cout<<a[i];
cout<<'\n';
}
return 0;
}