Responsive Ads Here

Saturday 9 September 2017

Minimum sum of factors

problem id:--http://practice.geeksforgeeks.org/problems/minimum-sum-of-factors/0

In this problem we first calculate and store at least one prime factor of every number.
if the given number is prime number then just add 1 to it and that will be our required answer.
is the given number is not a prime then find the prime number by factor array . add it to sum and then divide that element by that prime factor .

code:--

#include <bits/stdc++.h>
#define gc() getchar()
using namespace std;
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;
}

bool visit[(int)1e5+5];
int factor[(int)1e5+5];
vector<int> prime;
void magic()
{
int n=1e5+5;
for(int i=2;i<=n;++i)
{
if(!visit[i])
{
visit[i]=1;
factor[i]=i;
prime.push_back(i);
}
for(int j=0;j<prime.size() && i*prime[j]<=n;++j)
{
factor[i*prime[j]]=prime[j];
visit[i*prime[j]]=1;
}
}
}
int main()
{
magic();
int t;
scan(t);
while(t--)
{
int n;
scan(n);
if(n==1)
{
cout<<n<<'\n';
continue;
}
if(!n || factor[n]==n)
{
cout<<n+1<<'\n';
continue;
}
int sum=0;
while(n!=1)
{
sum+=factor[n];
n/=factor[n];
}
cout<<sum<<'\n';
}
return 0;
}

Friday 8 September 2017

Reverse a Linked List in groups of given size. (Function Problem)

problem id:-http://practice.geeksforgeeks.org/problems/reverse-a-linked-list-in-groups-of-given-size/1

code:--
/*
Please note that it's Function problem i.e.
you need to write your solution in the form Function(s) only.
Driver Code to call/invoke your function would be added by GfG's Online Judge.*/


/*
  Reverse a linked list
  The input list will have at least one element
  Return the node which points to the head of the new LinkedList
  Node is defined as
  struct node
  {
     int data;
     struct Node *next;
  }
*/
node *rev_k(node* head,int k)
    {
    if(head==nullptr)   return head;
    node *prev,*curr,*temp;
    prev=head;
    curr=prev->next;
    while(--k >0 && curr)
        {
        if(curr->next)  temp=curr->next;
        else    temp=nullptr;
        curr->next=prev;
        prev=curr;
        curr=temp;
        }
    return prev;
    }
struct node *reverse (struct node *head, int k)
{
if(head==nullptr)   return head;
node *f_node,*last_node;
f_node=head;
last_node=head;
int j=k;
while(j-- >0 && last_node)    last_node=last_node->next;
node *last=rev_k(head,k);
f_node->next=reverse(last_node,k);
return last;

}

Thursday 10 August 2017

Rod Cutting

problem id:--http://practice.geeksforgeeks.org/problems/rod-cutting/0

In this problem you have to use DP(instead of recursion which takes exponential time).
We create a array val of size n+1.
We then traverse var i from 1 to index n. And take i as the length of the Rod and try to find the max. Value for the regarding value. Answer can be that ith index value of using some combination of val array.


code:--

#include <bits/stdc++.h>
using namespace std;
#define sc(x)   scanf("%d",&x)
#define      rep(i, n)         for(int i = 0; i < (n); ++i)
#define rep2(i,j,n)   for(int i = j; i < (n); ++i)
int arr[105];
int n;
int solve()
{
int val[n+1]={0};
val[0]=0;
int Max;
for(int i=1;i<=n;++i)
{
Max=arr[i];
for(int j=1;j<i;++j) Max=max(Max,val[j]+val[i-j]);
val[i]=Max;
//debug2(i,Max);
}
//rep2(i,1,n+1) pf(val[i]);
return val[n];
}
int main()
{
int t;
sc(t);
while(t--)
{
cin>>n;
rep(i,n) sc(arr[i+1]);
printf("%d\n",solve());
}
return 0;
}

Sunday 6 August 2017

Min value of x Show Topic Tags

problem id:--http://practice.geeksforgeeks.org/problems/min-value-of-x/0

In this problem you need to find the Positive integer root of the given equation
Since given equation is this ax^2+bx+c>=k
First convert it into normal form
ax^+bx+(c-k)>=0
Now find the +ve root of the quadratic solution . and if it's a decimal number Then just take ceil
of that value.  That's all . Happy coding :)

code:--

#include <bits/stdc++.h>

using namespace std;
#define sc(x)   scanf("%d",&x)
int main()
{
int t;
sc(t);
while(t--)
{
double a,b,c,k;
cin>>a>>b>>c>>k;
double x1;
x1=(-b+sqrt(b*b-4*a*(c-k)))/(2*a);
cout<<ceil(x1)<<'\n';
}
return 0;
}


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;
}


Tuesday 25 July 2017

Check Arithmetic Progression

In this question you can use Hashing or priority_queue.
I have used Priority queue(Heap )
First of all push all elements in the heap .I elements is less than or
equal to 2. then AP can be formed . Else go for the approach (I mean)
find the common diff and check through out the array to find that they have
same common diff.That it. :)

problem id:-http://practice.geeksforgeeks.org/problems/check-arithmetic-progression/0
code:-

#include <bits/stdc++.h>

using namespace std;

#define sc(x)   scanf("%d",&x)
#define      rep(i, n)         for(int i = 0; i < (n); ++i)


int main(int argc, char **argv) {
priority_queue<int> pq;
string ans[]={"YES\n","NO\n"};
int t;
sc(t);
while(t--)
{
int n;
sc(n);
int num;
rep(i,n) {sc(num); pq.push(num); }
if(pq.size()<=2)
{
cout<<ans[0];
continue;
}
int num1,num2,diff,temp_diff;
num1=pq.top();
pq.pop();
num2=pq.top();
pq.pop();
diff=num2-num1;
bool flag=0;
while(!pq.empty())
{
num1=num2;
num2=pq.top();
pq.pop();
temp_diff=num2-num1;
if(temp_diff!=diff)
{
flag=1;
break;
}
}
cout<<ans[flag];
}
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;
}