When to Meet? C++ Solution

Technical lead of a development team has to send out a meeting invite to all his team members to discuss on a project they are working on.

There are N people in his team(including himself). He has the calendar of all the meeting participants, ie. he knows when each member
is busy. With this information, he has to schedule a meeting for T minutes, ie. find a slot of T minutes during which all participants
are free.

Input:
A single line of input. The first integer on the line represents T, the second interger represents N(N>=2). Then N pairs of strings
follows, each representing a period in time of a particular day during which that number is busy. The period will be of the format
“HHMM HHMM”. For example, if a pair reads, “0900 1500”, then the member is busy from 9AM to 3PM during the day, Note that the time is in
24-hour format. Note that the working hours of all participants are between 9AM and 9PM

Output:
Output the number of different slots(of T minutes) during which the meeting can be scheduled

Sample Output:

2

Explanation:

Here T=30 minute. There are 2 participants and the periods during which they are busy are as follows.

1st member -> 0900 2029 -> 9AM to 20:29PM

2nd member -> 0900 2029 -> 9AM to 20:29PM

The two available slots of 30 minutes are: 20:30 to 20:59 and 20:31 to 21:00

Code:

#include<iostream>
using namespace std;
int main(int argc,char **argv)
{
int n,i,j,k=0,T,v,m,slot=0;
cin>>T>>n;
int s[n],e[n],ts[n+2],te[n+2];
for(i=0;i<n;i++)
cin>>s[i]>>e[i];

for(i=0;i<n;i++)
{
v=s[i];
m=0;
for(j=i;j<n;j++)
{
if(s[i]==0)
break;
if(s[j]==v)
{
if(e[j]>m)
m=e[j];
if(j!=i)
s[j]=0;
}
}
if(s[i]!=0)
{
ts[k]=v;
te[k]=m;
k++;
}
}
if(ts[0]!=900)
{
for(i=k;i>0;i–)
{
ts[i]=ts[i-1];
te[i]=te[i-1];
}
ts[0]=te[0]=900;
k++;
}
ts[k]=2100;
te[k]=0;
k++;

for(i=0;i<k;i++)
for(j=i;j<k;j++)
if(ts[i]>ts[j])
{
int t=ts[i],t1=te[i];
ts[i]=ts[j];
te[i]=te[j];
ts[j]=t;
te[j]=t1;
}

for(i=0;i<k-1;i++)
{
int d=ts[i+1]-te[i];
if(d>60)
d=d-40;
if(d>=T)
slot+=(d-T)+1;
}

cout<<slot;
return 0;
}

Towers of Hanoi – C Solution

.

Code:

#include<stdio.h>
void tower(int n,char L,char R,char M)
{
if(n==1)
{
//Moves the last remaining disk to destination peg
printf(“\nMove disk 1 from %c to %c”,L,R);
return;
}
//Moves n-1 disks from L(left) to M(middle) peg using R(right) as auxillary
tower(n-1,L,M,R);
printf(“\nMove disk:%d from %c to %c”,n,L,R);
//Moves n-1 disks from M(middle) to R(right) peg using L(left) as auxillary
tower(n-1,M,R,L);
}
void main()
{
int n;
printf(“\nTOWERS OF HANOI”);
printf(“\n\nEnter number of disks”);
scanf(“%d”,&n);
if(n<1)
printf(“\nNo disks to move”);
else
tower(n,’L’,’R’,’C’); // Move n disks from L to R using C as auxillary
}

Spiral Pattern – C++ solution

 

Code:

#include<iostream>
using namespace std;
int main()
{
int n;
cin>>n;
int i,j,k=1;

int l=0,r=1,d=0,u=0;
int a[n][n];

for(i=0;i<n;i++)
for(j=0;j<n;j++)
a[i][j]=0;
i=0;
j=-1;
while(k<=(n*n))
{
if(r==1)
{
j++;
if(j==n || a[i][j]!=0)
{
r=0;
d=1;
j–;
}
else
{
a[i][j]=k;
k++;
}
}
else if(d==1)
{
i++;
if(i==n || a[i][j]!=0)
{
i–;
d=0;
l=1;
}
else
{
a[i][j]=k;
k++;
}
}
else if(l==1)
{
j–;
if(j<0 || a[i][j]!=0)
{
l=0;
j++;
u=1;
}
else
{
a[i][j]=k;
k++;
}

}
else if(u==1)
{
i–;
if(i<0 || a[i][j]!=0)
{
i++;
u=0;
r=1;
}
else
{
a[i][j]=k;
k++;
}
}
}

for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
cout<<a[i][j]<<“\t”;
cout<<“\n”;
}
return 0;

}

Input:

5

Output:

1	2	3	4	5	
16	17	18	19	6	
15	24	25	20	7	
14	23	22	21	8	
13	12	11	10	9

Input:

4

Output:

1	2	3	4	
12	13	14	5	
11	16	15	6	
10	9	8	7	

All Combinations of given string – C++ Solution

 

Code:

#include<iostream>
#include<string.h>
using namespace std;
int total=0;
string swap(string s,int i,int j)
{
char t=s[i];
s[i]=s[j];
s[j]=t;

return s;
}

void combine(string s,int n,int i)
{
if(i==n)
{
for(int ti=0;ti<n;ti++)
cout<<s[ti];
cout<<“\n”;
total++;
}
else
{
for(int j=i;j<n;j++)
{
s=swap(s,i,j);
combine(s,n,i+1);
s=swap(s,i,j);
}
}
}

int main()
{
string s;
cin>>s;
int n=s.length();
string st=s+s;
int i,j,f,t,si,ft=1;
string su;
for(i=0;i<n;i++)
{
cout<<s[i]<<“\n”;
total++;
}
t=n;
for(int k=2;k<n;k++)
{
ft=1;
t=t*(n-(k-1));
for(int q=2;q<=k;q++)
ft=ft*q;
int l=(t/ft)-s.length();
for(i=0;i<n;i++)
{
string su=st.substr(i,k);
combine(su,su.length(),0);
}
int p=2;
while(l>0)
{

for(i=0;i<n;i++)
{
string su=st.substr(i,1);
su=su+st.substr(i+p,k-1);
combine(su,su.length(),0);
l–;
if(l==0)
break;
}
p++;
}}
combine(s,n,0);

cout<<“total: “<<total;
return 0;
}

 

Output:

abcde

a
b
c
d
e
ab
ba
bc
cb
cd
dc
de
ed
ea
ae
ac
ca
bd
db
ce
ec
da
ad
eb
be
abc
acb
bac
bca
cba
cab
bcd
bdc
cbd
cdb
dcb
dbc
cde
ced
dce
dec
edc
ecd
dea
dae
eda
ead
aed
ade
eab
eba
aeb
abe
bae
bea
acd
adc
cad
cda
dca
dac
bde
bed
dbe
deb
edb
ebd
cea
cae
eca
eac
aec
ace
dab
dba
adb
abd
bad
bda
ebc
ecb
bec
bce
cbe
ceb
abcd
abdc
acbd
acdb
adcb
adbc
bacd
badc
bcad
bcda
bdca
bdac
cbad
cbda
cabd
cadb
cdab
cdba
dbca
dbac
dcba
dcab
dacb
dabc
bcde
bced
bdce
bdec
bedc
becd
cbde
cbed
cdbe
cdeb
cedb
cebd
dcbe
dceb
dbce
dbec
debc
decb
ecdb
ecbd
edcb
edbc
ebdc
ebcd
cdea
cdae
ceda
cead
caed
cade
dcea
dcae
deca
deac
daec
dace
edca
edac
ecda
ecad
eacd
eadc
adec
adce
aedc
aecd
aced
acde
deab
deba
daeb
dabe
dbae
dbea
edab
edba
eadb
eabd
ebad
ebda
aedb
aebd
adeb
adbe
abde
abed
bead
beda
baed
bade
bdae
bdea
eabc
eacb
ebac
ebca
ecba
ecab
aebc
aecb
abec
abce
acbe
aceb
baec
bace
beac
beca
bcea
bcae
cabe
caeb
cbae
cbea
ceba
ceab
abcde
abced
abdce
abdec
abedc
abecd
acbde
acbed
acdbe
acdeb
acedb
acebd
adcbe
adceb
adbce
adbec
adebc
adecb
aecdb
aecbd
aedcb
aedbc
aebdc
aebcd
bacde
baced
badce
badec
baedc
baecd
bcade
bcaed
bcdae
bcdea
bceda
bcead
bdcae
bdcea
bdace
bdaec
bdeac
bdeca
becda
becad
bedca
bedac
beadc
beacd
cbade
cbaed
cbdae
cbdea
cbeda
cbead
cabde
cabed
cadbe
cadeb
caedb
caebd
cdabe
cdaeb
cdbae
cdbea
cdeba
cdeab
ceadb
ceabd
cedab
cedba
cebda
cebad
dbcae
dbcea
dbace
dbaec
dbeac
dbeca
dcbae
dcbea
dcabe
dcaeb
dceab
dceba
dacbe
daceb
dabce
dabec
daebc
daecb
decab
decba
deacb
deabc
debac
debca
ebcda
ebcad
ebdca
ebdac
ebadc
ebacd
ecbda
ecbad
ecdba
ecdab
ecadb
ecabd
edcba
edcab
edbca
edbac
edabc
edacb
eacdb
eacbd
eadcb
eadbc
eabdc
eabcd
total: 325

Longest Increasing Subsequence – C++ Solution

 

Code:

#include<iostream>
using namespace std;

int main()
{
int N,p,i,j,k=0;
cin>>N;
int a[N];
int c[N*N];
int l[N*N][N];
for(i=0;i<N;i++)
cin>>a[i];
c[0]=0;
l[0][0]=a[0];

for(i=0;i<N;i++)
{

for(j=0;j<=k;j++)
{
if(a[i]>l[j][c[j]])
{
k++;
for(p=0;p<=c[j];p++)
l[k][p]=l[j][p];
l[k][p]=a[i];
c[k]=p;
}
else
{
if(a[i]<l[j][0] && l[k][c[k]]!=a[i])
{
k++;
l[k][0]=a[i];
c[k]=0;
}

}
}

}

int m=0;
for(i=0;i<=k;i++)
if(m<c[i])
m=c[i];

for(i=0;i<=k;i++)
{
if(m==c[i])
{
for(j=0;j<=c[i];j++)
cout<<l[i][j]<<“\t”;
cout<<“\n”;

}
}

return 0;
}

destocreativesolutions's avatarDESTOLOG

Here is something that you can see and have a good laugh!

This is not made by us but we found it extremely funny and felt like re-sharing it. The original post can be found here –

http://martinvalasek.com/blog/pictures-from-a-developers-life

When I show the boss that I have finally fixed this bug

When the project manager enters the office

Inline image 1

When I’m deploying code to production

When I try to fix a bug at 3 in the morning

When my regex returned exactly what I expected

When a friend of mine asks me to fix his website built with Joomla

When I’m told that the module on which I have worked all the week will never be used

When the code that I have not tested on dev works perfectly in production

When the sales people announce they have sold our product to the customer

When I apply a new CSS for the…

View original post 240 more words

Adding two numbers in linked list – C Solution

Code:

#include<stdio.h>
struct node
{
int data;
struct node *next;
};

void addbeg(struct node **h,int v)
{
struct node *t;
t=(struct node *)malloc(sizeof(struct node));
t->data=v;
t->next=(*h);
(*h)=t;
}

void addlast(struct node **h,int v)
{
struct node *t1,*t;
if((*h)==NULL)
{
t=(struct node *)malloc(sizeof(struct node));
t->data=v;
t->next=NULL;
*h=t;
}
else
{
t=(*h);
t1=(struct node *)malloc(sizeof(struct node));
while(t->next!=NULL)
t=t->next;
t1->data=v;
t1->next=NULL;
t->next=t1;
}
}

struct node* sum(struct node *t1,struct node *t2)
{
struct node *r;
r=malloc(sizeof(struct node));
r=NULL;
int n1=0,n2=0,s,t;
while(t1!=NULL)
{
n1=(10*n1)+(t1->data);
t1=t1->next;
}
while(t2!=NULL)
{
n2=(10*n2)+(t2->data);
t2=t2->next;
}
s=n1+n2;
while(s>0)
{
t=s%10;
s=s/10;
addbeg(&r,t);
}
return r;
}

void display(struct node *t)
{
printf(“\n”);
while(t->next!=NULL)
{
printf(“%d–>”,t->data);
t=t->next;
}
printf(“%d\n”,t->data);
}

void main()
{
struct node *h1;
struct node *h2;
struct node *res;
int n1,n2,t;
h1=(struct node*)malloc(sizeof(struct node));
h2=(struct node*)malloc(sizeof(struct node));
res=(struct node*)malloc(sizeof(struct node));
h1=NULL;
h2=NULL;
printf(“\nEnter numbers”);
scanf(“%d”,&n1);
scanf(“%d”,&n2);
while(n1>0)
{
t=n1%10;
n1=n1/10;
addbeg(&h1,t);
}
while(n2>0)
{
t=n2%10;
n2=n2/10;
addbeg(&h2,t);
}
display(h1);
display(h2);
res=sum(h1,h2);
display(res);
}

Output:

Enter the numbers
153
2899

1–>5–>3

2–>8–>9–>9

3–>0–>5–>2

Removing specified characters from string – Python

Code:

str="This is a test-for manipulation, of string!"
st='-,!' #characters to be removed from string
s=[]
i=0
for c in str:
     if c in st:
          j=str.find(c,i)
          s.append(str[i:j])
          i=j+1
s=filter(None,s) #removes empty element from list
s=''.join(s) #convert list to string
print s

Output:

This is a testfor manipulation of string

Bit sort in C

 

Description:

This sort is considered the most efficient if the range of integers to be sorted is known. For example: Sort the k integers which lie between 1 to 1000000. Below code is unblushingly copied from the book “programming pearls”.

Code:

#include<stdio.h>
#define mask 0x1F
#define shift 5
#define n 1000000
int a[n/32];
void set(int i)
{
a[i>>shift]|=(1<<(i&mask));
}
int test(int i)
{
return (a[i>>shift]&(1<<(i&mask)));
}

void main()
{
int i,d;
for(i=0;i<n/32;i++)
a[i]=0;
while(scanf(“%d”,&d)!=0) //enter any character to stop the iteration
set(d);

for(i=0;i<n;i++)
if(test(i))
printf(“\n%d”,i);
}

qsort in c

 

 

Code:

#include<stdio.h>
#include<stdlib.h>
int cmp(const void *a,const void *b)
{

int v1=*(const int*)a;
int v2=*(const int*)b;

/*the above two lines are optional. It is used to remove the warning.

warning: passing argument 4 of ‘qsort’ from incompatible pointer type
/usr/include/stdlib.h:710: note: expected ‘__compar_fn_t’ but argument is of type ‘int
(*)(const int *, const int *)’

If not used then replace the below statement with:

return *a-*b;

*/

return v1-v2;
}

void main()
{
int a[30];
int i=0,j;
while(scanf(“%d”,&a[i])!=0)  //scanf returns 0 on read error. So input any character instead of integer to stop the iteration.
i++;
qsort(a,i,sizeof(int),cmp);
for(j=0;j<i;j++)
printf(“\n%d”,a[j]);
}

Sort and remove duplicate strings – C++ solution

 

 

Description:

The elements in set are usually sorted(ascending) and unique. Note in case of strings BBB comes in-front of aaa. A( ASCII: 65 ) < a(ASCII: 97). NULL is used as input terminator here.

Code:

#include<iostream>
#include<set>
#include<string.h>
using namespace std;
int main(void)
{
set<string> s;
set<string>::iterator j;
string t;
while(cin>>t && t.compare(“NULL”))
s.insert(t);

for(j=s.begin();j!=s.end();++j)
cout<<*j<<“\n”;
return 0;
}

 Output:

jackie
tony
aaa
chinese
tamil
BBB
java
programming
NULL

BBB
aaa
chinese
jackie
java
programming
tamil
tony