Code For Alls..!

get the code!!

Showing posts with label cpp. Show all posts
Showing posts with label cpp. Show all posts

Monday 18 September 2017

                                                                          Three Strings
Three Strings


Given N string values, the program must print 3 string values as the output as described in the Example Input/Output section.
Input format:
The first line will contain N denoting the number of string values.
Next N lines will contain the N string values.
Output format:
Three lines containing string values as described in the Example Input/Output section.
Example Input/Output 1:
Input:
3
JOHN
JOHNY
JANARDHAN
Output:
JJOJAN
OHHARD
NNYHAN
Example Input/Output 2:
Input:
4
JOHN
JOHNY
JANARDHAN
MIKESPENCER
Output:
JJOJANMIKE
OHHARDSPE
NNYHANNCER
C++ Program:
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
string s,s1="",s2="",s3="";
int n;
cin>>n;
for(int i=0;i<n;i++)
{
    cin>>s;
    s1+=s.substr(0,i+1);
    s2+=s.substr(i+1,s.length()-2*(i+1));
    s3+=s.substr(s.length()-(i+1));
}
cout<<s1<<"\n"<<s2<<"\n"<<s3;
}

Sunday 17 September 2017

                                                Minimum Distance Between Alphabets (Id-3109)
Minimum Distance Between Alphabets (Id-3109)


Given a string S and two alphabets C1 and C2 present in S, find the minimum distance D  between C1 and C2 in S.
Input Format:
The first line will contain S.
The second line will contain C1 and C2 separated by a space.
Output Format:
The first line will contain D.
Boundary Conditions:
2 <= Length of S <= 100
Example Input/Output 1:
Input:
spaceship
c s
Output:
1
c++ Program:
#include <iostream>

using namespace std;

int main(int argc, char** argv)
{
string s;
cin>>s;
char a,b;
cin>>a;
cin>>b;
int m=s.length(),flag=0,d=0;
for(int i=0;i<s.length();i++)
{
    if(s[i]==a||s[i]==b)
    {
        if(flag==1&&d!=0)
        {
        if(d<m)
        m=d;
        d=0;
        }
        else
        flag=1;
    }
    if(flag==1)
    d++;
}
cout<<m-1;
}


Sunday 10 September 2017

                                               Isolate Interlaced Strings (Id-3077) 



Two string values S1 and S2 are interlaced and passed as a single input string S. Given L1 which is the length of S1, print S1 and S2 as the output.
Input Format:
The first line contains S.
The second line contains L1 and L2 separated by a space.
Output Format:
The first line contains S1.
The second line contains S2.
Boundary Conditions:
4 <= LENGTH(S) <= 100
1 <= LENGTH(S1) <= 99
1 <= LENGTH(S2) <= 99
Example Input/Output 1:
Input:
LBARZIYSK
4
Output:
LAZY
BRISK

c++ program:

#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
string s,f="";
cin>>s;
int n,i;
cin>>n;
for(i=0;i<s.length();i=i+2)
{
    if(f.length()<n)
    {
    f+=s[i];
    s[i]='0';
    }
    if(s.length()-i==n-i/2)
    break;
}
if(i<s.length())
while(i<s.length())
{f+=s[++i];n
s[i]='0';
}
cout<<f<<"\n";
 i=0;
 while(i<s.length())
 {
     if(s[i]!='0')
 cout<<s[i];
 i++;
 }

}

Thursday 7 September 2017

                                                                String Zig Zag Pattern

Given a string S and an integer N as the input, the program must split the string into groups whose size is N and print them as the output in separate lines in a zig zag manner. If the last group size is less than N then the remaining letters must be filled with asterisk as shown in the Example Input/Output.
Input Format:
The first line contains S.
The second line contains N.
Output Format:
LENGTH(S)/N + LENGTH(S)%N lines containing the desired pattern.
Boundary Conditions:
4 <= LENGTH(S) <= 500
2 <= N <= LENGTH(S)
Example Input/Output 1:
Input:
ENVIRONMENT
3
Output:
ENV
ORI
NME
*TN
Example Input/Output 2:
Input:
ENVIRONMENT
4
Output:
ENVI
MNOR
ENT*
Example Input/Output 3:
Input:
EVERYDAY
2
Output:
EV
RE
YD
YA
C++program:
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
string s;
int n,c=0,l;
cin>>s;
cin>>n;
l=s.length();
while(l%n!=0)
{
s.append("*");
l=s.length();
}
int i=0,t=0;
while(i<l)
    {
    cout<<s[i];
    c++;
    if(c==n)
    {
    c=0;
    cout<<"\n";
    i=i+n;
    t++;
    }
    else
    {
    if(t%2==0)
    i++;
    else
    i--;
    }
}
}

Sunday 3 September 2017


You’re given the pointer to the head node of a linked list. Change the next pointers of the nodes so that their order is reversed. The head pointer given may be null meaning that the initial list is empty.
Input Format
You have to complete the Node* Reverse(Node* head) method which takes one argument - the head of the linked list. You should NOT read any input from stdin/console.
Output Format
Change the next pointers of the nodes that their order is reversed and return the head of the reversed linked list. Do NOT print anything to stdout/console.
Sample Input
NULL
2 --> 3 --> NULL
Sample Output
NULL
3 --> 2 --> NULL
Explanation
1. Empty list remains empty
2. List is reversed from 2,3 to 3,2
c++ program:
/*
  Reverse a linked list and return pointer to the head
  The input list will have at least one element  
  Node is defined as 
  struct Node
  {
     int data;
     struct Node *next;
  }
Reverse(Node  head)
        prev=NULL       //Previous Pointer
        cur=head        //Current Node
        nxt=*cur.next   //Next Pointer
        while cur is not NULL
            nxt=(*cur).next
            (*cur).next=prev
            prev=cur
            cur=nxt
        head=prev
        return head
*/
Node* Reverse(Node *head)
{
 struct Node *prev=NULL,*cur,*nxt;
    cur=head;
    nxt=cur->next;
    while(cur!=NULL)
        {
        nxt=cur->next;
    cur->next=prev;
    prev=cur;
    cur=nxt;
    }
    head=prev;
    return(head);
    // Complete this method
}




You are given the pointer to the head node of a linked list and you need to print all its elements in reverse order from tail to head, one element per line. The head pointer may be null meaning that the list is empty - in that case, do not print anything!
Input Format
You have to complete the void ReversePrint(Node* head) method which takes one argument - the head of the linked list. You should NOT read any input from stdin/console.
Output Format
Print the elements of the linked list in reverse order to stdout/console (using printf or cout) , one per line.
Sample Input
1 --> 2 --> NULL
2 --> 1 --> 4 --> 5 --> NULL
Sample Output
2
1
5
4
1
2
Explanation
1. First list is printed from tail to head hence 2,1
2. Similarly second list is also printed from tail to head.

c++ program:
/*
  Print elements of a linked list in reverse order as standard output
  head pointer could be NULL as well for empty list
  Node is defined as 
  struct Node
  {
     int data;
     struct Node *next;
  }
*/
void ReversePrint(Node *head)
{
  if(head!=NULL)
      {
      ReversePrint(head->next);
  cout<<head->data<<"\n";
  }
    // This is a "method-only" submission. 
  // You only need to complete this method. 
}




You’re given the pointer to the head node of a linked list and the position of a node to delete. Delete the node at the given position and return the head node. A position of 0 indicates head, a position of 1 indicates one node away from the head and so on. The list may become empty after you delete the node.
Input Format
You have to complete the Node* Delete(Node* head, int position) method which takes two arguments - the head of the linked list and the position of the node to delete. You should NOT read any input from stdin/console. position will always be at least 0 and less than the number of the elements in the list.
Output Format
Delete the node at the given position and return the head of the updated linked list. Do NOT print anything to stdout/console.
Sample Input
1 --> 2 --> 3 --> NULL, position = 0
1 --> NULL , position = 0
Sample Output
2 --> 3 --> NULL
NULL
Explanation
1. 0th position is removed, 1 is deleted from the list.
2. Again 0th position is deleted and we are left with empty list.

C++program:
/*
  Delete Node at a given position in a linked list 
  Node is defined as 
  struct Node
  {
     int data;
     struct Node *next;
  }
*/
Node* Delete(Node *head, int position)
{
  struct Node *temp;
   int i=0;
    if(position==0)
    {
        head=head->next;
    return(head);
    }
    else
       { temp=head;
    while(i<position-1)
    {
        i=i+1;
        temp=temp->next;
    }
   temp->next=temp->next->next;
    return(head);
    // Complete this method
}
}


You’re given the pointer to the head node of a linked list, an integer to add to the list and the position at which the integer must be inserted. Create a new node with the given integer, insert this node at the desired position and return the head node. A position of 0 indicates head, a position of 1 indicates one node away from the head and so on. The head pointer given may be null meaning that the initial list is empty.
Input Format
You have to complete the Node* Insert(Node* head, int data, int position) method which takes three arguments - the head of the linked list, the integer to insert and the position at which the integer must be inserted. You should NOT read any input from stdin/console. position will always be between 0 and the number of the elements in the list (inclusive).
Output Format
Insert the new node at the desired position and return the head of the updated linked list. Do NOT print anything to stdout/console.
Sample Input
NULL, data = 3, position = 0
3 --> NULL, data = 4, position = 0
Sample Output
3 --> NULL
4 --> 3 --> NULL
Explanation
1. we have an empty list and position 0. 3 becomes head.
2. 4 is added to position 0, hence 4 becomes head.
Note
For the purpose of evaluation the list has been initialised with a node with data=2. Ignore it, this is done to avoid printing empty lists while comparing output.

c++ program:
/*
  Insert Node at a given position in a linked list 
  head can be NULL 
  First element in the linked list is at position 0
  Node is defined as 
  struct Node
  {
     int data;
     struct Node *next;
  }
*/
Node* InsertNth(Node *head, int data, int position)
{
  struct Node *newnode,*temp;
   int i=0;
    newnode=new Node;
    newnode->next=NULL;
    newnode->data=data;
    if(position==0)
       { 
        newnode->next=head;
    head=newnode;
        return(head);
       }
    else
        {
       temp=head;
        while(i<position-1)
            {
            i=i+1;
            temp=temp->next;
        }
        newnode->next=temp->next;
        temp->next=newnode;
        return(head);
    }
    // Complete this method only
  // Do not write main function. 
}



You’re given the pointer to the head node of a linked list and an integer to add to the list. Create a new node with the given integer, insert this node at the head of the linked list and return the new head node. The head pointer given may be null meaning that the initial list is empty.
Input Format
You have to complete the Node* Insert(Node* head, int data) method which takes two arguments - the head of the linked list and the integer to insert. You should NOT read any input from stdin/console.
Output Format
Insert the new node at the head and return the head of the updated linked list. Do NOT print anything to stdout/console.
Sample Input
NULL , data = 1
1 --> NULL , data = 2
Sample Output
1 --> NULL
2 --> 1 --> NULL

Explanation
1. We have an empty list, on inserting 1, 1 becomes new head.
2. We have a list with 1 as head, on inserting 2, 2 becomes the new head.

c++ code:

/*
  Insert Node at the begining of a linked list
  Initially head pointer argument could be NULL for empty list
  Node is defined as 
  struct Node
  {
     int data;
     struct Node *next;
  }
return back the pointer to the head of the linked list in the below method.
*/
Node* Insert(Node *head,int data)
{
  struct Node *newnode;
    newnode=new Node;
    newnode->data=data;
    newnode->next=NULL;
    newnode->next=head;
    head=newnode;
    return(head);
    // Complete this method
}



Four strings all having the same length L are passed as the input to the program. The four strings must be printed in a L*L square matrix shape as shown in the example input/output.
The string which is on the top will always be the first string in the input. Other three strings can occur in a random order in the input. The sequence of the string can be identified by the fact that the last letter of a string will be the first letter of another string (and you can safely assume the last letter will not occur more than once).
Input Format:
The first line contains the string which represents the top of the square matrix.
The next three lines will contain the remaining the three string values which can represent the right, left and bottom side of the squares, but not necessarily in the same order.
Output Format:
The L*L square matrix with these four strings as it's sides as described in the Example Input/Output.
Boundary Conditions:
3 <=  L <= 100
Example Input/Output 1:
Input:
TIGER
YACHT
RANGE
EVERY
Output:
TIGER
H***A
C***N
A***G
YREVE
Example Input/Output 2:
Input:
MAN
DOT
NOD
TIM
Output:
MAN
I*O
TOD
c++ code:
#include <iostream>
#include<string>
using namespace std;
int main(int argc, char** argv) {
string a[4];
int i,j,l,k;
for(i=0;i<4;i++)
cin>>a[i];
l=a[0].length();
string e,t;
for(i=1;i<4;i++)
if(a[0][0]==a[i][l-1])
t=a[i];
for(i=1;i<4;i++)
if(t[0]==a[i][l-1])
e=a[i];
for(i=1;i<4;i++)
if(a[i]!=e&&a[i]!=t)
break;
cout<<a[0]<<"\n";
for(j=0;j<l-2;j++)
{
cout<<t[l-2-j];
for(k=0;k<l-2;k++)
cout<<"*";
cout<<a[i][j+1];
cout<<"\n";
}
for(i=l-1;i>=0;i--)
cout<<e[i];
}

Saturday 2 September 2017


You are given the pointer to the head node of a linked list and an integer to add to the list. Create a new node with the given integer. Insert this node at the tail of the linked list and return the head node of the linked list formed after inserting this new node. The given head pointer may be null, meaning that the initial list is empty.
Input Format
You have to complete the Node* Insert(Node* head, int data) method. It takes two arguments: the head of the linked list and the integer to insert. You should not read any input from the stdin/console.
Output Format
Insert the new node at the tail and just return the head of the updated linked list. Do not print anything to stdout/console.
Sample Input
NULL, data =
 --> NULL, data = 
Sample Output
2 -->NULL
2 --> 3 --> NULL
Explanation
1. We have an empty list, and we insert .
2. We start with a  in the tail. When  is inserted,  then becomes the tail.


C++ program:
/*
  Insert Node at the end of a linked list 
  head pointer input could be NULL as well for empty list
  Node is defined as 
  struct Node
  0
  {
     int data;
     struct Node *next;
  }
*/
Node* Insert(Node *head,int data)
{
  struct Node *newnode,*temp;
    newnode=new Node;
    newnode->next=NULL;
    newnode->data=data;
   temp=head;
    if(temp!=NULL)
{
        while(temp->next!=NULL)
            temp=temp->next;
            temp->next=newnode; 
        return(head);
    }    // Complete this method
else{
    head=newnode;
    return(head);
}
}

If you're new to linked lists, this is a great exercise for learning about them. Given a pointer to the head node of a linked list, print its elements in order, one element per line. If the head pointer is null (indicating the list is empty), don’t print anything.
Input Format
The void Print(Node* head) method takes the head node of a linked list as a parameter. Each struct Node has a data field (which stores integer data) and a next field (which points to the next element in the list).
Note: Do not read any input from stdin/console. Each test case calls the Print method individually and passes it the head of a list.
Output Format
Print the integer data for each element of the linked list to stdout/console (e.g.: using printfcout, etc.). There should be one element per line.
Sample Input
This example uses the following two linked lists:
NULL 
1->2->3->NULL
 and  are the two head nodes passed as arguments to Print(Node* head).
Note: In linked list diagrams, -> describes a pointer to the next node in the list.
Sample Output
1
2
3
Explanation
Test Case 0: NULL. An empty list is passed to the method, so nothing is printed.
Test Case 1: 1->2->3->NULL. This is a non-empty list so we loop through each element, printing each element's data field on its own line.


c++ program:

/*
  Print elements of a linked list on console 
  head pointer input could be NULL as well for empty list
  Node is defined as 
  struct Node
  {
     int data;
     struct Node *next;
  }
*/
void Print(Node *head)
{
  
    if(head!=NULL)
        {
        cout<<head->data<<"\n";
        Print(head->next);
    }
    // This is a "method-only" submission. 
  // You only need to complete this method. 
}

Saturday 22 July 2017

Given a time in -hour AM/PM format, convert it to military (-hour) time.
Note: Midnight is on a -hour clock, and on a -hour clock. Noon is on a -hour clock, and on a -hour clock.
Input Format
A single string containing a time in -hour clock format (i.e.: or ), where and .
Output Format
Convert and print the given time in -hour format, where .
Sample Input
07:05:45PM
Sample Output
19:05:45
 
C++ code: 

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

int main(){
      string s;
    cin >> s;

    int n = s.length();
    int hh, mm, ss;
    hh = (s[0] - '0') * 10 + (s[1] - '0');
    mm = (s[3] - '0') * 10 + (s[4] - '0');
    ss = (s[6] - '0') * 10 + (s[7] - '0');

    if (hh < 12 && s[8] == 'P') hh += 12;
    if (hh == 12 && s[8] == 'A') hh = 0;

    printf("%02d:%02d:%02d\n", hh, mm, ss);

    return 0;
}