sahil.bathla1
BAN USER
#include <iostream>
using namespace std;
struct list
{
char *value;
list * next;
}*first=NULL;
void JoinList(int A[],int size)
{
char *tempString = new char(15);
int i=0;list *currentNode;
while(i<size)
{
list *temp=new list;
temp->value=new char(15);
strcpy(temp->value,"");
temp->next=NULL;
do
{
itoa(A[i],tempString,10); //convert array element to string
strcat(temp->value,tempString); //concatenate to current node
i++;
if(i>=size)
break;
}while(A[i]==(A[i-1]+1)); // till the next element is one greater than the current element
if(first==NULL)
{
first=temp;
currentNode=temp;
}
else
{
currentNode->next=temp;
currentNode=temp;
}
}
}
void display(list * node)
{
while(node!=NULL)
{cout<<node->value<<" ";
node=node->next;
}
}
int main()
{
int A[12]= {2,3,4,5,6,9,10,12,13};
JoinList(A,9);
display(first);
system("pause");
return 0;
}
Steps :-
1) convert current array element to string & add to list
2) read & concatenate further elements provided the next element = previous element + 1
3) repeat 1 & 2 to form the linked list till you run out of array elements
Just see the comments in Join List Function. You will understand
wat do you mean by containing one sequence??
- sahil.bathla1 November 15, 2012your logic would work only if the tree is a perfect binary tree. it will fail otherwise... suppose root has only right subtree with one children i.e - tree=> a c
answer should be
c - 2 0
a - 1 1
but as per your solution it would be
c - 1 0
a - 0 1
iterative code
list * alternateSwap(list * node)
{
list *previous_joint,*new_joint,*temp;
previous_joint=new list;
while (node!=NULL&&node->next!=NULL)
{
temp=node->next->next;
node->next->next=node; //swap step 1 //break & update next nodes link
if(node==list_head)
list_head=node->next;
node=node->next; //swap step 2 update node
new_joint=node->next; // saving joining point
if(node!=list_head)
{
previous_joint->next=node; //joining previous joint with new swapped node
previous_joint=new_joint;
}
else
previous_joint=new_joint; //saving first joining point
node=temp;
} //end of while
previous_joint->next=node;
return list_head;
}
list * alternateSwap(list * node)
{
if (node!=NULL&&node->next!=NULL)
{
list *temp= new list;
temp->next=node->next->next; //swap step 1 //save the next nodes link;
node->next->next=node; //swap step 2 //break & update next nodes link
node=node->next; //swap step 3 update node
node->next->next=alternateSwap(temp->next); //find next link of swapped node
delete temp;
}
return node;
}
example 1 -> 2-> 3
swap step 1 : save 2->next=3
swap step 2 : 2->next =1
swap step 3 : 2= node or head
step 4 : 2->next->next=alternateSwap(3) => 1->next=alternateSwap(3) = 3;
therefore final result = 2->1->3;
Using ashishanand's logic i made this code.Works fine :-
#include <iostream>
using namespace std;
/*
Harcoded Today's Given Date 27/08/2012
Day Thursday
*/
class date
{
int dd,mm,yyyy;
public:
date(int d,int m,int y)
{dd=d;mm=m;yyyy=y;}
void compute_day();
};
void date :: compute_day()
{
int count_year=0,count_days=0;
int year=yyyy,month=mm,d=dd;
cout<<"Date is "<<d<<"/"<<month<<"/"<<year<<"\n";
while(year != 2012)
{
if(year>2012) // if year is greater compute odd days
{
if(year%400==0||(year%4==0&&year%100!=0))
count_year+=2;
else
count_year++;
year-=1;
}
else // if year is less compute odd days
{
if(year%400==0||(year%4==0&&year%100!=0))
count_year-=2;
else
count_year--;
year+=1;
}
}
while(month != 8)
{
if(month>8)
{
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12) // if month is greater compute odd days
count_days+=3;
else if(month==2)
;
else
count_days+=2;
month--;
}
else
{
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
count_days-=3;
else if(month==2)
;
else
count_days-=2;
month++;
}
}
if(d>27) // if days are greater compute odd days
{ count_days+=(d-27);}
else
{ count_days-=(27-d);}
int gap=(count_days+count_year)%7;
if(gap<0) gap+=7; //mathematical modulo
if(gap==0)
cout<<"Thurday";
else if(gap==1)
cout<<"Friday";
else if(gap==2)
cout<<"Saturday";
else if(gap==3)
cout<<"Sunday";
else if(gap==4)
cout<<"Monday";
else if(gap==5)
cout<<"Tuesday";
else if(gap==6)
cout<<"Wednesday";
else ;
}
int main()
{
date toCheck(26,10,2016); // change it to any date you want to check
toCheck.compute_day();
system("pause");
return 0;
}
Well the code does this :-
Step 1 It keeps the track of the minimum element index all the time..
Step 2 Checks if the condition holds and keeps storing them in another array B till it holds
Step3 When condition fail the current B array is displayed and then step 1 and 2 are repeated starting from (minimum+1) index because all the elements before that index are irrelevant now
Step 4 if all the elements are traversed exit and display again
As you can see the logic is simple ... keep track of the minimum element. keep going till the condition holds and if it fails the minimum element and all the elements before it are irrelevant now because the condition would fail if we include them. so restart from minimum + 1 instead.
#include <iostream>
using namespace std;
void convert(int a)
{
if(a!=0)
{
int n=a%26;
if(n!=0)
{
convert(a/26);
cout<<(char)(96+n);
}
else
{convert((a/26)-1);
cout<<"z";
}
}
}
int main()
{
int a;
cout<<"Enter number";
cin>>a;
convert(a);
system("pause");
return 0;
}
exactly the solution i thought. Any way to improve upon it?
- sahil.bathla1 November 12, 2012i made a code to work it out .. complexity is surely less than O(n^2) . Please prove my code wrong because i haven't used either min heap or dequeue to solve it . :)
#include <iostream>
using namespace std;
void display(int B[],int n)
{
if(n>1)
{
for(int i=0;i<n;i++)
cout<<B[i]<<" ";
}
cout<<"\n";
}
int main()
{
int A[12]= {1,2,3,4,5,-5,1,2,3,4,5,-5};
int B[10];
int k,i=1;
int check=1;
int min=0,count=1,flag=0;
cout<<"Enter k\n";
cin>>k;
B[0]=A[0];
while(i<12) // 12 is harcoded .. take n instead
{
if(A[min]+A[i]>k) //to check if the condition satisfies
{
if(i>=check) flag=1; //minimum index to counter(to exclude any minimal subset)
if(A[i]<A[min]) //update min position
min=i;
B[count]=A[i];
count++; //total count in sub-array
i++;}
else
{ if(flag==1) //if displayable ( new elements added)
{display(B,count);flag=0;check=i;} // it should not be displayed till the index i is rediscovered again
{min=min+1;B[0]=A[min];i=min+1;count=1;}
}
}
if(flag==1)
display(B,count);
system("pause");
return 0;
}
if(onParachute)
goto label execute_code
execute_code :
static int count;
count++;
if(count<3) // on landing each rover visit the label therefore count =2
{
while(1)
{
go left;
no operation; //to make the rover slow
}
}
else if(count ==3) // one rover has reached others parachute
{
while(1)
go left;
}
else ; //count can never be 4
RepElkeRoll, Animator at A9
I am Elke from the USA. I am working as a technologist in Hill-Behan company. I perform and analyze the ...
Repnovathomes, abc at 8x8
I am a friendly and outgoing person who enjoys greeting people with a smile. I have more than eight years ...
RepDavidRNichols, Analyst at AMD
Skilled communicator and listener with a knack for remedying conflict, and keen organizational skills which allow for effective delivery of ...
Repmandybourne5463, Accountant at ABC TECH SUPPORT
MandyBourne, and I am a Computer typesetter and I love my work. Apart from this, today I am doing new ...
Replillianrperkins66, Android test engineer at ABC TECH SUPPORT
I'm Lillian Perkins, and I'm a studio camera operator. A studio camera operator Main work includes operating cranes ...
Reppennyruddie, Cloud Support Associate at Accenture
Training Coordinator focuses on transaction quality, efficiency, and facility management. develops effective training for the programme “ mantra for attract girl ...
RepJamesFain, Area Sales Manager at ABC TECH SUPPORT
Phil has nearly 30 years of experience as a fisheries research scientist and directs the CFS Northwest science team. He ...
RepPaulMahone, Android test engineer at Booking.com
Hey, my name is Jazzy and I am a Journalist as a profession.I started my journey from last 4 ...
RepDaisyRoss, Video game designer at A9
Seeking lead game programmer and position to utilize knowledge and skills to advance portfolio and potential for increased responsibility. Explore ...
Reprudystake, Game Programmer at BrowserStack
I am Rudy , a physical therapist with extensive knowledge of sports injuries and therapy . Strong background in athletic coaching and ...
Repmorganswitzer8475, Android test engineer at ABC TECH SUPPORT
Hey, I'm Morgan switzer and i am a sociologist. I also like to read some interesting books like get ...
RepGizzyKale, Graphics Programmer at Arista Networks
Je suis Gizzy, un commis aux archives avec d'excellentes compétences interpersonnelles et de communication. Expert en nourriture , boisson et ...
Repbrendaalfaro847, Computer Scientist at ABC TECH SUPPORT
Brenda Alfaro, and I am a Skills training coordinator and I love my work. Apart from this, today I am ...
RepMiaMiller, abc at A9
Experienced software engineer with a passion for developing innovative programs that expedite the efficiency and effectiveness of organizational success. Well-versed ...
Reptabithalaws245, Computer Scientist at Absolute Softech Ltd
I am a Quality auditor Friendly technicians with over six years experience solving customer technical issues quickly and effectively. and ...
Repleliakelsch6389, Associate at 247quickbookshelp
Hello, I am a News Anchor. And I have completed all my studies from Portland. And today I am a ...
Repameliahill344, abc at Detail Oriented
Professionally licensed Architect with a Master’s degree in Architecture and more than 4 years experience designing commercial buildings, offices ...
RepNoraMiller, abc at A9
Confident and dedicated photographer with experience in both professional and freelance photography.. Prioritizes communication on the job to avoid errors ...
can you give the test case for which it failed
- sahil.bathla1 November 16, 2012