The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
+26 votes

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 -> data) &&

For a given linked list $p$, the function $f$ returns $1$ if and only if

  1. the list is empty or has exactly one element

  2. the elements in the list are sorted in non-decreasing order of data value

  3. the elements in the list are sorted in non-increasing order of data value

  4. not all elements in the list have the same data value

asked in DS by Veteran (52k points)
edited by | 3.1k views
I still dont understand why is a not correct it is an or condition if (p==NULL||(p->next==NULL)||((p->data<=p->next->data)&&f(p->next))) all 4 coditions are in or even if one is true  it can return 1 plz explain

$A$ if and only if $B$ means both $A\rightarrow B$ as well as $B\rightarrow A$ hold.

So is the function returning 1 only when the list is empty or has exactly one element? NO

because it also returns 1 for  ((p->data <= p ->next -> data) && f(p->next)) which is nothing but C ! So C is the most appropriate choice.

C means it sat that its a decreasing order it possible??
Some one please give better answer

2 Answers

+28 votes
Best answer

It returns $1$ if and only if the linked list is sorted in non-decreasing order- (B) option.

It returns $1$ if the list is empty or has just $1$ element also, but if and only if makes (A) option false.

answered by Boss (14.4k points)
edited by
why not A as it also satisfies ?
Due to if and only if
olrite, got it

Can we also say that Option A is a part of Option B? Like A is included in B..


@Mizuki yes.

If and only if makes option A incorrect, but it also makes option B incorrect.

I think actual answer is : the list is empty OR has exactly one element OR the elements in the list are sorted in non-decreasing order of data value.

But there is no such option. Can someone explain this?
+17 votes
How to approach this:-

Emphasize on Word "if and only if"

Read all options carefully ..eliminate the wrong ones.

A. May Correct but due to if and only if Check remaining options.

C. False . Example:->5,5,4 it wont return 1.(still optionA may correct).

D. False. Example:->> 8,2,10 it wont return 1(still option A may correct).

B.Example:->>  1,1,2  it returns 1

So here Option A becomes wrong Now So option  B is correct.

(Check another large list also 1,2,2,3,3 still returns1 So finally Option B is Ans.)
answered by Boss (23.4k points)
The cause "if and only if" makes both option (a) and (b) wrong. How to handle this ?
@arjun sir

please expalin 4 the option through example .I m not getting this

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
49,535 questions
54,122 answers
71,039 users