recursion
GATE CSE & IT · Complexity Theory · 1990-2026
Study anchor
Cormen et al. — Introduction to Algorithms (CLRS)
Algorithms, data structures, graph algorithms, complexity
Practice action
Start latest PYQPYQs in this concept
All concepts →Consider the following code snippet in C language that computes the number of nodes in a non-empty singly linked list pointed to by the pointer variable head. struct node{ int elt;...
Consider the recursive functions represented by the following code segment: int bar(int n){ if (n == 1) return 0; else return 1 + bar(n/2); } int foo(int n){ if (n == 1) return 1;...
#include int foo(int S[],int size) { if(size == 0) return 0; if(size == 1) return 1; if(S[0] != S[1]) return 1+foo(S+1,size-1); return foo(S+1,size-1); } int main() { int A[]={0,1,...
#include <stdio.h> int foo(int S[ ], int size) { if(size == 0) return 0; if(size == 1) return 1; if(S[0]!=S[1]) return 1 + foo(S + 1, size - 1); return foo(S + 1, size - 1); } int...
Consider the following C program: #include void fX(); int main() { fX(); return 0;} void fX() { char a; if((a=getchar()) != '\n') fX(); if (a != '\n') putchar (a); } Assume that th...
Consider the following C program: #include <stdio.h> void fX(); int main() { fX(); return 0;} void fX() { char a; if ((a = getchar()) != '\n') fX(); if (a != '\n') putchar(a);} Ass...
Consider the following program: int main ( ) { f1( ); f2(2); f3( ); return (0); } int f1( ) { return (1); } int f2 (int X) { f3( ); if (X==1) return f1 ( ); else return (X*f2(X$$-$...
Consider the following ANSI C program #include <stdio.h> int foo(int x, int y, int q) { if ((x <= 0 && (y <= 0)) return q; if (x <= 0) return foo(x, y - q, q); if (y <= 0) return f...
What is the worst-case number of arithmetic operations performed by recursive binary search on a sorted array of size n?
Consider the following ANSI C function: int SomeFunction (int x, int y) { if ( (x == 1) I I (y == 1)) return 1; if (x == y) return x; if (x > y) return SomeFunction(x - y, y); if (...
Consider the following C functions. int fun1 (int n) { static int i = 0; if (n > 0) { ++i; fun1(n-1); } return (i); } ___________________ int fun2 (int n) { static int i = 0; if (n...
Consider the following C function. void convert(int n){ if(n < 0) printf("%d",n); else { convert(n/2); printf("%d",n%2); } } Which one of the following will happen when the functio...
Consider the following C program: #include < stdio.h > int counter = 0; int calc (int a, int b) { int c; counter++; if (b==3) return (a*a*a); else { c = calc(a, b/3); return (c*c*c...
Consider the following function written in the C programming language. void foo(char *a){ if ( *a && *a != ' '){ foo(a+1); putchar(*a); } } The output of the above function on inpu...
Consider the following C function. int fun ( int n ) { int x = 1, k ; if ( n == 1) return x ; for( k = 1 ; k < n ; ++ k ) x = x + fun( k ) * fun( n - k ) ; return x ; } The return...
Suppose you are provided with the following function declaration in the C programming language. int partition(int a[], int n); The function treats the first element of a[ ] as a pi...
Consider the pseudocode given below. The function Dosomething () takes as argument a pointer to the root of an arbitrary tree represented by the leftMostChild-rightSibling represen...
Which of the following statements are CORRECT? 1) Static allocation of all data areas by a compiler makes it impossible to implement recursion. 2) Automatic garbage collection is e...
Consider the C function given below. int f(int j) { static int i = 50; int k; if (i == j) { printf("something"); k = f(i); return 0; } else return 0; } Which one of the following i...
Consider the following function double f (double x) { if ( abs (x * x – 3) < 0. 01) return x; else return f (x / 2 + 1.5/x); } Give a value q (to 2 decimals) such that f(q) will re...
What is the return value of f (p, p), if the value of p is initialized to 5 before the call? Note that the first parameter is passed by reference, whereas the second parameter is p...
The height of a tree is defined as the number of edges on the longest path in the tree. The function shown in the pseudocode below is invoked as height (root) to compute the height...
The recurrence relation capturing the optional execution time of the Towers of Hanoi problem with $$n$$ discs is
Consider the following recursive C function that takes two arguments: unsigned int foo (unsigned int n, unsigned int r) { if (n > 0) return((n % r) + foo(n/r, r)); else return 0; }...
Consider the following recursive C function that takes two arguments: unsigned int foo (unsigned int n, unsigned int r) { if (n > 0) return((n % r) + foo(n/r, r)); else return 0; }...
Which of the following are true? I. A programming language which does not permit global variables of any kind and has no nesting of procedures/functions, but permits recursion can...
Which of the following are true? I. A programming language which does not permit global variables of any kind and has no nesting of procedures/functions, but permits recursion can...
Choose the correct option to fill ? 1 and ? 2 so that the program below prints an input string in reverse order. Assume that the input string is terminated by a newline character....
Consider the following C functions: int f1(int n){ if(n == 0 || n == 1){ return n; } return (2 * f1(n - 1) + 3 * f1(n - 2)); } int f2(int n){ int i; int X[N], Y[N], Z[N]; X[0] = Y[...
Consider the following C functions: int f1(int n){ if(n == 0 || n == 1){ return n; } return (2 * f1(n - 1) + 3 * f1(n - 2)); } int f2(int n){ int i; int X[N], Y[N], Z[N]; X[0] = Y[...
In the following C function, let n $$ \ge $$ m. int gcd(n,m) { if (n % m == 0) return m; n = n % m; return gcd(m,n); } How many recursive calls are made by this function?
Consider the following C program segment where CellNode represents a node in a binary tree: struct CellNode { struct CellNOde *leftChild; int element; struct CellNode *rightChild;...
Suppose you are given an implementation of a queue of integers. The operations that can be performed on the queue are: i. isEmpty (Q) — returns true if the queue is empty, false ot...
Consider the following C function: int f(int n) { static int r = 0; if(n <= 0) return 1; if(n>3) { r = n; return f(n-2)+2; } return f(n-1)+r; } What is the value of f(5)?
The following function computes the value of m C n correctly for all legal values m and n (m≥1,n≥0 and m>n) int func(int m, int n) { if (E) return 1; else return(func(m -1, n) + fu...
A function f defined on stacks of integers satisfies the following properties. f(∅) = 0 and f (push (S, i)) = max (f(S), 0) + i for all stacks S and integers i. If a stack S contai...
Consider the following C program: void foo (int n, int sum) { int k = 0, j = 0; if(n==0) return; k=n%10; j = n /10; sum = sum + k; foo(j, sum); printf("%d",k); } int main () { int...
Consider the following C - function: double foo(int n){ int i; double sum; if(n == 0) return 1.0; sum = 0.0; for (i = 0; i < n; i++){ sum += foo(i); } return sum; } Suppose we modi...
Consider the following C - function: double foo(int n){ int i; double sum; if(n == 0) return 1.0; sum = 0.0; for (i = 0; i < n; i++){ sum += foo(i); } return sum; } The space compl...
The time complexity of the following C function is (assume n > 0) int recursive(int n){ if(n == 1){ return (1); } return (recursive(n - 1) + recursive(n - 1)); }
Consider the following C program segment struct CellNode{ struct CellNode *leftChild; int element; struct CellNode *rightChild; }; int Dosomething (struct CellNode *ptr) { int valu...
Consider the following C function: int f(int n) { static int i = 1; if(n>=5) return n; n = n+1; i++; return f(n); } The value returned by f(1) is
Consider the function f defined below. struct item { int data; struct item * next; }; int f(struct item *p) { return ((p == NULL) || (p ->next == NULL) || ((p->data <= p -> next ->...
Consider the following C program: void abc(char *s) { if(s[0]=='\0') return; abc(s+1); abc(s+1); printf("%c",s[0]); } main() { abc("123"); } (a) What will be the output of the prog...
Consider the following C function definition int Trial (int a, int b, int c) { if ((a > = b) && (c < b)) return b; else if (a > = b) return Trial (a,c,b); else return Trial (b,a,c)...
What value would the following function return for the input x = 95? function fun (x:integer):integer; Begin If x > 100 then fun : x – 10 Else fun : fun(fun (x + 11)) End;
What value would the following function return for the input x = 95? Function fun (x:integer):integer; Begin If x > 100 then fun : x – 10 Else fun : fun(fun (x + 11)) End;
Indicate the following statement true or false : A programming language not supporting either recursion or pointer type does not need the support of dynamic memory allocation.
Match the followings: Group-I (a) Pointer data type (b) Activation Record (c) Repeat -Until (d) Coercion Group-II (p) Type Conversion (q) Dynamic Data Structure (r) Recursion (s) N...