Friday 19 February 2016

Number in a position

You are given a sequence of integers. The length of the sequence is unknown but the sequence ends with a -1. You are given another number pos . Your task is to output the integer in position pos. If pos > length of the sequence output -1.

You are supposed to implement two functions :
Node loadNum(): Function to load the numbers onto the linked list      Return a pointer to the head of the list
void releaseMem(Node head): Function to release the memory after every iteration



Note: pos is indexed from 1.

Input
T
a11 a12 .. -1
Pos 1


aT1  aT2 … -1
Pos T

Output
N1
N2
..
..
NT

Constraints
1 <= T <= 30
1 <= aij <= 100
There is no restriction on the length of each list.

Example
Input
1
4 3 2 1 -1
2

Output
3

Explanation
3 is the number in the second position.

Solution -:



in Java-

import java.io.*;

public class Position {

public static void main(String ...a) {
     Scanner in = new Scanner(System.in);
      List<Integer> list = new ArrayList<Integer>();
      int i = in.nextInt();
       
     while(i != -1) {
     list.add(i);
       i = in.nextInt();
       }
        i = in.nextInt();
       if(list.size() >i) {
           System.out.prinltn(list.get(in.nextInt))
      } else{
         System.out.prinltn(i);
}
}

5 comments:

  1. #include
    #include

    struct node{
    int data;
    struct node* next;
    };

    typedef struct node* Node;

    /* Function to load the numbers onto the linked list
    Return a pointer to the head of the list */
    Node loadNum();

    /* Function to print the number in position pos
    head is guaranteed to be non-NULL */
    /* Function to print the number in position pos
    head is guaranteed to be non-NULL */
    void printNum(Node head,int pos)
    {
    int i = 1;
    Node temp = head;
    while( i != pos ){
    temp = temp->next;
    if( temp == NULL){
    printf("-1");
    return;
    }
    i++;
    }
    printf("%d",temp->data);
    }


    /* Function to release the memory after every iteration */
    void releaseMem(Node head);

    int main()
    {
    int i,T;
    int pos;
    Node head;
    scanf("%d",&T);
    for (i = 0; i < T; i++){

    head = loadNum();
    scanf("%d",&pos);
    printNum(head, pos);
    if(i
    printf("\n"); // Will add a new line for after all output
    // except for last.
    releaseMem(head);
    }
    return 0;
    }
    /* Function to load the numbers onto the linked list
    Return a pointer to the head of the list */
    Node loadNum()
    {
    Node temp=NULL,head=NULL;
    int n;
    scanf("%d",&n);
    if(n!=-1)
    {
    temp=(Node)malloc(sizeof(struct node));
    head=temp;
    }
    while(n!=-1)
    {
    temp->data=n;
    temp->next=(Node)malloc(sizeof(struct node));
    temp=temp->next;
    scanf("%d",&n);
    }
    temp=NULL;
    return head;
    }

    /* Function to release the memory after every iteration */
    void releaseMem(Node head)
    {
    Node temp;
    while(head!=NULL)
    {
    temp=head;
    head=head->next;
    free(temp);
    }
    }
















    input/Output should be like this:
    Input:
    2
    9 8 5 2 ‐1
    4
    5 3 1 9 10 ‐1
    9

    output:
    2
    ‐1

    Program provides correct output except for few test cases which is showing "time limit exceeded" for given input:-
    input:
    3
    5 4 1 3 5 ‐1
    5
    2 3 6 7 ‐1
    4
    6 3 4 3 8 ‐1
    2
    Where am I going wrong?

    ReplyDelete
    Replies
    1. Node loadNum(){
      Node temp=NULL,head=NULL;
      int n;
      scanf("%d",&n);
      if(n!=-1)
      {
      temp=malloc(100000);
      head=temp;
      }
      while(n!=-1)
      {
      temp->data=n;
      temp->next=malloc(100000);
      temp=temp->next;
      scanf("%d",&n);
      }
      temp=NULL;
      return head;
      }

      Delete
    2. use calloc instead of malloc.u are not providing contiguous memory to that input.

      Node loadNum()
      {
      Node temp=NULL,head=NULL;
      int n;
      scanf("%d",&n);
      if(n!=-1)
      {
      temp=(Node)(calloc(n,sizeof(struct node)));
      head=temp;
      }
      while(n!=-1)
      {
      temp->data=n;
      temp->next=(Node)(calloc(n,sizeof(struct node)));
      temp=temp->next;
      scanf("%d",&n);
      }
      temp=NULL;
      return head;
      }

      Delete
  2. #include
    #include

    struct node{
    int data;
    struct node* next;
    };

    typedef struct node* Node;

    /* Function to load the numbers onto the linked list
    Return a pointer to the head of the list */
    Node loadNum();

    /* Function to print the number in position pos
    head is guaranteed to be non-NULL */
    /* Function to print the number in position pos
    head is guaranteed to be non-NULL */
    void printNum(Node head,int pos)
    {
    int i = 1;
    Node temp = head;
    while( i != pos ){
    temp = temp->next;
    if( temp == NULL){
    printf("-1");
    return;
    }
    i++;
    }
    printf("%d",temp->data);
    }


    /* Function to release the memory after every iteration */
    void releaseMem(Node head);

    int main()
    {
    int i,T;
    int pos;
    Node head;
    scanf("%d",&T);
    for (i = 0; i < T; i++){

    head = loadNum();
    scanf("%d",&pos);
    printNum(head, pos);
    if(i<T-1)
    printf("\n"); // Will add a new line for after all output
    // except for last.
    releaseMem(head);
    }
    return 0;
    }

    ReplyDelete