The code uses a max heap to remove the maximum element present in the data. Heap is used as it helps to perform the above operation in O(log n) time.
Link to code.
Link to code.
#include <iostream>
using namespace std;
//count no of ones in binary representation
int count1(int n)
{
int c=0;
while(n)
{
n=n&(n-1);
cout<<n;
c++;
}
return c;
}
int main(){
cout<<count1(6);
return 0;
}
//find all prime factors
#include <iostream>
#include<math.h>
using namespace std;
int main() {
int n;
cin>>n;
cout<<n<<endl;
int count=0;
for(int i=2;i<=n;i++)
{
count =0;
while(n%i==0){
n/=i;
count++;
}
if(count)
cout<<i<<"<<"<<count<<endl;
}
return 0;
}
//created by Aditya Vijayvergia
//uploaded on thecodingedge.blogspot.com
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node* next;
}*head;
typedef struct node node;
node* rev(int n)
{
node *p,*q,*r,*t2,*t1;
int c=0,start=1;
t2=head;
t1=NULL;
p=head;
q=head->next;
r=q->next;
while(q!=NULL)
{
//printf("c<n-1\n\t%d",p->data);
//getch();
if(c==n-1)
{
//printf("c=n-1");
//getch();
c=0;
if(t1!=NULL)
t1->next=p;
t1=t2;
t2=q;
if(start==1)
{
start=0;
head=p;
}
p=q;
q=p->next;
r=q->next;
}
q->next=p;
p=q;
q=r;
r=r->next;
c++;
}
t1->next=p;
t2->next=NULL;
return head;
}
void main()
{
node *q;
node* p;
int i;
clrscr();
head=(node*)malloc(sizeof(node));
p=head;
//q->next=NULL;
head->data=1;
//p->next=NULL;
for(i=2;i<15;i++)
{
q=(node*)malloc(sizeof(node));
q->next=NULL;
q->data=i;
p->next=q;
p=p->next;
}
p=head;
printf("Initial link list is:\n");
while(p!=NULL)
{
printf("%d->",p->data);
p=p->next;
}
printf("\n\n\n\nUsing n=3\n");
head=rev(6);
p=head;
printf("\n\n\nReversed link list is:\n");
while(p!=NULL)
{
printf("%d->",p->data);
p=p->next;
}
getch();
}
//created by Aditya Vijayvergia //uploaded on thecodingedge.blogspot.com #include<stdio.h> #include<conio.h> #include<ctype.h> #define size 20 struct stack { char data[size][size]; int top; }s; void push(char x[20]) { int i; s.top++; for(i=0;i<strlen(x);i++) s.data[s.top][i]=x[i]; } char* pop() { char *p; if(s.top==-1) return NULL; return s.data[s.top--]; } void main() { char q[50],c[50]; char *a,*b; int i,j,a1,b1; s.top=-1; clrscr(); printf("\nenter postfix expression:"); gets(q); for(j=0;j<strlen(q);j++) { if(q[j]>='a'&&q[j]<='z') { s.data[++s.top][0]=q[j]; //printf("\npush %c",q[j]); } else { b=pop(); a=pop(); //printf("\na=%s\tb=%s",a,b); c[0]=q[j]; a1=strlen(a); b1=strlen(b); for(i=0;i<a1;i++) { c[i+1]=*a; a++; } for(i=0;i<b1;i++) { c[a1+i+1]=*b; b++; } push(c); //printf("\npushed operand %s",c); } a=NULL; b=NULL; } printf("\n\nPrefix expression : %s",pop()); getch(); }
#include<iostream.h> #include<conio.h> #include<process.h> #include<time.h> #include<stdlib.h> //created by Aditya Vijayvergia //uploaded at thecodingedge.blogspot.com int snake[40][2],point[20][2],dead[200][2],nd,len,n=20,m=39,pos[2]; void create() { int i; len=5; srand(time(NULL)); for(i=0;i<len;i++) { snake[i][1]=i; } for(i=0;i<20;i++) { point[i][0]=rand() %20; point[i][1]=rand() %40; } srand(time(NULL)); for(i=0;i<nd;i++) { dead[i][0]=rand() %20; dead[i][1]=rand() %40; } } int found(int x,int y) { for(int i=0;i<len;i++) if(snake[len-1][0]==x&&snake[len-1][1]==y) return 2; else if(snake[i][0]==x&&snake[i][1]==y) return 1; return 0; } int ifdead(int x,int y) { for(int j=0;j<nd;j++) if(dead[j][0]==x&&dead[j][1]==y) return 1; return 0; } int ifpoint(int x,int y) { for(int i=0;i<20;i++) { if(point[i][0]==x&&point[i][1]==y) return 1; } return 0; } void show() { //cout<<"show running"; clrscr(); cout<<"\n"; for(int k=0;k<m+1;k++) cout<<"__"; cout<<"\n"; for(int i=0;i<n;i++) { cout<<"|"; for(int j=0;j<m;j++) { if(found(i,j)==2) cout<<" m"; else if(found(i,j)==1) cout<<" o"; else if(ifpoint(i,j)) cout<<" *"; else if(ifdead(i,j)) cout<<" X"; else cout<<" "; } cout<<"|"; } for(k=0;k<m+1;k++) cout<<"__"; } void changepoint(int x,int y) { for(int i=0;i<20;i++) if(point[i][0]==x&&point[i][1]==y) { point[i][0]=-1; point[i][1]=-1; } } void move(int x,int y) { x=(x+n)%n; y=(y+m)%m; if(ifpoint(x,y)) { pos[0]=x; pos[1]=y; changepoint(x,y); } else if(ifdead(x,y)) { clrscr(); cout<<"\n\n\n\t\tGAME OVER\n\n\t\tScore:"<<len-5; getch(); exit(0); } for(int i=0;i<len-1;i++) for(int j=0;j<2;j++) snake[i][j]=snake[i+1][j]; snake[i][0]=x; snake[i][1]=y; //} show(); if(snake[len-1][0]==pos[0]&&snake[len-1][1]==pos[1]) { len++; if(len-5==20) { clrscr(); cout<<"\n\n\n\t\tVICTORY"; exit(0); } for(i=len-1;i>0;i--) for(j=0;j<2;j++) snake[i][j]=snake[i-1][j]; snake[0][0]=pos[0]; snake[0][1]=pos[1]; pos[0]=-1; pos[1]=-1; } } void menu() { int choice; cout<<"\n\n\tSANKE GAME"; cout<<"\n\n\n1.easy\n2.normal\n3.hard"; cin>>choice; switch(choice) { case 1:nd=50; break; case 2:nd=125; break; case 3:nd=200; break; } } void main() { clrscr(); menu(); create(); show(); pos[0]=-1; pos[1]=-1; char m; while(1) { cout<<"\n\t\t"; cout<<"enter choice:"; cin>>m; switch(m) { case 'd':move(snake[len-1][0],snake[len-1][1]+1); break; case 'w':move(snake[len-1][0]-1,snake[len-1][1]); break; case 's':move(snake[len-1][0]+1,snake[len-1][1]); break; case 'a':move(snake[len-1][0],snake[len-1][1]-1); break; case 'p':exit(0); } } }
Sample output is given below.