CODEBHAVYA • DATA STRUCTURES

🔍 Searching

Learn important searching techniques in C from basic linear search to interview-oriented advanced searching methods.

📖 Introduction to Searching

Searching is the process of finding whether a particular value exists in a collection of data and, if it exists, determining its position.

The choice of searching algorithm depends mainly on whether the data is sorted, how uniformly the values are distributed, and whether the data structure supports random access.

Important: Always check the data requirement before selecting a searching algorithm. Binary Search, for example, requires sorted data.

📊 Searching Algorithm Comparison

Compare the major searching algorithms at a glance.

Algorithm Data Requirement Best Average Worst Space
Linear SearchSorted or UnsortedO(1)O(n)O(n)O(1)
Binary SearchSortedO(1)O(log n)O(log n)O(1)*
Recursive Binary SearchSortedO(1)O(log n)O(log n)O(log n)
Rotated Binary SearchRotated Sorted ArrayO(1)O(log n)O(log n)O(1)
Jump SearchSortedO(1)O(√n)O(√n)O(1)
Interpolation SearchSorted & Uniformly DistributedO(1)O(log log n)O(n)O(1)
Fibonacci SearchSortedO(1)O(log n)O(log n)O(1)
Skip List SearchSkip ListO(1)O(log n)O(n)O(n)

* Iterative Binary Search uses O(1) auxiliary space; recursive implementation uses O(log n) stack space.

Quick Interview Rule: Unsorted data → Linear Search | Sorted data → Binary Search | Rotated sorted array → Rotated Binary Search | Sorted uniformly distributed data → Interpolation Search | Ordered linked structure → Skip List.

📝 Important Interview Points

Key facts to remember before solving searching problems.

  • Binary Search requires sorted data.
  • Use low + (high - low) / 2 to calculate the midpoint safely.
  • Linear Search works on unsorted data.
  • Binary Search reduces the search space by approximately half each step.
  • Recursive Binary Search has O(log n) auxiliary stack space.
  • Rotated Binary Search identifies a sorted half at every step.
  • Interpolation Search is excellent for uniformly distributed sorted values.
  • Jump Search uses √n-sized blocks.
  • Fibonacci Search uses Fibonacci numbers to partition the search area.
  • Skip Lists use multiple levels of links to speed up ordered searches.
  • Always ask whether duplicate values are possible.
  • For duplicate arrays, interviewers may ask for first occurrence, last occurrence, or count of occurrences.

💡 Interview Tips

  1. First identify whether the input is sorted.
  2. Look for words such as "minimum", "maximum", "first", "last", or "closest"; they often indicate a Binary Search variation.
  3. Always define the search range clearly.
  4. Check boundary cases: empty array, one element, target at beginning, target at end, and target absent.
  5. For rotated arrays, determine which half is sorted before deciding where to move.
  6. Explain why the complexity is logarithmic instead of simply stating O(log n).

❓ Common Interview Questions

Think about each question first. If you are unsure, open the answer and learn the short interview-ready explanation.

1. Why must Binary Search use sorted data?

Binary Search decides whether to continue in the left half or right half by comparing the target with the middle element. That decision is valid only when the values are ordered.

Interview answer: Binary Search requires sorted data because the ordering allows every comparison to safely eliminate half of the remaining search space.
2. Why is Binary Search O(log n)?

After each comparison, Binary Search keeps only about half of the current elements. So the search sizes become n → n/2 → n/4 → ... → 1.

Interview answer: Binary Search is O(log n) because each comparison halves the remaining search range.
3. What is the difference between iterative and recursive Binary Search?

The iterative version repeatedly updates low and high inside a loop. The recursive version calls itself with a smaller range. Both take O(log n) search time, but recursion additionally uses call-stack space.

Interview answer: Iterative Binary Search uses a loop and O(1) auxiliary space; recursive Binary Search uses recursive calls and O(log n) stack space.
4. How do you avoid integer overflow while calculating mid?

Instead of mid = (low + high) / 2, use mid = low + (high - low) / 2. This avoids directly adding two potentially large indices.

Interview answer: Use low + (high - low) / 2 so low + high does not overflow.
5. How do you search a rotated sorted array?

Find mid. At least one side of a rotated sorted array is normally sorted. Identify that sorted half, check whether the target lies inside it, and discard the other half.

Interview answer: At every step, identify the sorted half and use the target range to decide which half to keep.
6. When is Interpolation Search better than Binary Search?

Interpolation Search is most effective when the array is sorted and values are distributed approximately uniformly. Its estimated position can then land very close to the target.

Interview answer: It can outperform Binary Search on large, sorted, uniformly distributed numeric data.
7. Why can Interpolation Search become O(n)?

If values are distributed very unevenly, the interpolation formula may repeatedly estimate poor positions and reduce the search range only a little at a time.

Interview answer: On non-uniform data, poor position estimates can make Interpolation Search degrade to linear-time behavior.
8. What is the block size used in Jump Search?

The usual block size is approximately √n. Jumping by about √n balances the number of jumps with the number of elements that may need to be scanned inside the final block.

Interview answer: Jump Search normally uses a block size of √n, giving O(√n) search time.
9. What is a Skip List?

A Skip List is a sorted linked structure with multiple levels of forward pointers. Higher levels skip over several nodes so the search can move quickly, then drop to lower levels near the target.

Interview answer: A Skip List is a probabilistic ordered structure that adds multiple forward-link levels to speed up searching.
10. How do duplicate values affect Rotated Binary Search?

With distinct values, it is normally easy to identify which half is sorted. With many duplicates, values at low, mid, and high may be equal, so the algorithm may not know which side can safely be discarded.

Interview answer: Duplicates can make the sorted half ambiguous and may degrade Rotated Binary Search from O(log n) to O(n).

🎯 20 Searching Practice Problems

Try each problem yourself first. Use 💻 Solve It Yourself to write and test your C program. Use Hint only when needed, and Show Program if you want to study the complete solution.

📈 Searching Practice Progress
Solved0 / 20
Completed with Solution0
Total Score0 / 2000
Completion0%
A problem counts as Solved when all tests pass without opening the full solution. Problems completed after viewing the solution are tracked separately.
1. Find an element using Linear Search

Given an array and a target, print its index or -1.

Input: n, n integers, target

Output: index or -1

Scan from index 0 to n-1 and stop when the target is found.
💻 Solve It Yourself — Linear Search

C Code Editor

Sample Input

Program Output

Run your program to see the output.

Test Cases

No tests checked yet.
Best Score0 / 100
Attempts0
StatusNot Solved
Write your C program and test it. You can do it! 💪
Scoring: up to 100 marks without help; up to 90 after viewing a hint. If the full solution is opened, the problem can still be marked Completed, but it is no longer counted as a competitive score.
#include <stdio.h>

int main() {
    int n, t, pos = -1;

    scanf("%d", &n);

    int a[n];

    for (int i = 0; i < n; i++)
        scanf("%d", &a[i]);

    scanf("%d", &t);

    for (int i = 0; i < n; i++) {
        if (a[i] == t) {
            pos = i;
            break;
        }
    }

    printf("%d\n", pos);
    return 0;
}

Sample Input

5
4 8 2 9 6
9

Sample Output

3
2. Count occurrences of a target

Count how many times a value occurs in an array.

Input: n, array, target

Output: count

Increment a counter every time a[i] equals target.
💻 Solve It Yourself — Count occurrences of a target

C Code Editor

Sample Input

Program Output

Run your program to see the output.

Test Cases

No tests checked yet.
Best Score0 / 100
Attempts0
StatusNot Solved
Write your C program and test it. You can do it! 💪
Scoring: up to 100 marks without help; up to 90 after viewing a hint. If the full solution is opened, the problem can still be marked Completed, but it is no longer counted as a competitive score.
#include <stdio.h>

int main() {
    int n, t, c = 0;

    scanf("%d", &n);

    int a[n];

    for (int i = 0; i < n; i++)
        scanf("%d", &a[i]);

    scanf("%d", &t);

    for (int i = 0; i < n; i++)
        if (a[i] == t)
            c++;

    printf("%d\n", c);
    return 0;
}

Sample Input

7
2 4 2 7 2 8 2
2

Sample Output

4
3. Find first occurrence

Find the first index of a target.

Input: n, array, target

Output: first index or -1

Scan from left to right and stop at the first match.
💻 Solve It Yourself — Find first occurrence

C Code Editor

Sample Input

Program Output

Run your program to see the output.

Test Cases

No tests checked yet.
Best Score0 / 100
Attempts0
StatusNot Solved
Write your C program and test it. You can do it! 💪
Scoring: up to 100 marks without help; up to 90 after viewing a hint. If the full solution is opened, the problem can still be marked Completed, but it is no longer counted as a competitive score.
#include <stdio.h>

int main() {
    int n, t, pos = -1;

    scanf("%d", &n);

    int a[n];

    for (int i = 0; i < n; i++)
        scanf("%d", &a[i]);

    scanf("%d", &t);

    for (int i = 0; i < n; i++) {
        if (a[i] == t) {
            pos = i;
            break;
        }
    }

    printf("%d\n", pos);
    return 0;
}

Sample Input

6
5 3 8 3 9 3
3

Sample Output

1
4. Find last occurrence

Find the last index of a target.

Input: n, array, target

Output: last index or -1

Do not stop at the first match. Update the position every time the target is found.
💻 Solve It Yourself — Find last occurrence

C Code Editor

Sample Input

Program Output

Run your program to see the output.

Test Cases

No tests checked yet.
Best Score0 / 100
Attempts0
StatusNot Solved
Write your C program and test it. You can do it! 💪
Scoring: up to 100 marks without help; up to 90 after viewing a hint. If the full solution is opened, the problem can still be marked Completed, but it is no longer counted as a competitive score.
#include <stdio.h>

int main() {
    int n, t, pos = -1;

    scanf("%d", &n);

    int a[n];

    for (int i = 0; i < n; i++)
        scanf("%d", &a[i]);

    scanf("%d", &t);

    for (int i = 0; i < n; i++)
        if (a[i] == t)
            pos = i;

    printf("%d\n", pos);
    return 0;
}

Sample Input

6
5 3 8 3 9 3
3

Sample Output

5
5. Binary Search — iterative

Search for a target in a sorted array using iteration.

Input: sorted array and target

Output: index or -1

Maintain low and high and compare the target with the middle element.
💻 Solve It Yourself — Binary Search — iterative

C Code Editor

Sample Input

Program Output

Run your program to see the output.

Test Cases

No tests checked yet.
Best Score0 / 100
Attempts0
StatusNot Solved
Write your C program and test it. You can do it! 💪
Scoring: up to 100 marks without help; up to 90 after viewing a hint. If the full solution is opened, the problem can still be marked Completed, but it is no longer counted as a competitive score.
#include <stdio.h>

int main() {
    int n, t, lo, hi, mid, pos = -1;

    scanf("%d", &n);

    int a[n];

    for (int i = 0; i < n; i++)
        scanf("%d", &a[i]);

    scanf("%d", &t);

    lo = 0;
    hi = n - 1;

    while (lo <= hi) {
        mid = lo + (hi - lo) / 2;

        if (a[mid] == t) {
            pos = mid;
            break;
        }

        if (a[mid] < t)
            lo = mid + 1;
        else
            hi = mid - 1;
    }

    printf("%d\n", pos);
    return 0;
}

Sample Input

5
10 20 30 40 50
40

Sample Output

3
6. Binary Search — recursive

Implement Binary Search using recursion.

Input: sorted array and target

Output: index or -1

Create a recursive function with low and high parameters.
💻 Solve It Yourself — Binary Search — recursive

C Code Editor

Sample Input

Program Output

Run your program to see the output.

Test Cases

No tests checked yet.
Best Score0 / 100
Attempts0
StatusNot Solved
Write your C program and test it. You can do it! 💪
Scoring: up to 100 marks without help; up to 90 after viewing a hint. If the full solution is opened, the problem can still be marked Completed, but it is no longer counted as a competitive score.
#include <stdio.h>

int bs(int a[], int l, int h, int t) {
    if (l > h)
        return -1;

    int m = l + (h - l) / 2;

    if (a[m] == t)
        return m;

    if (t < a[m])
        return bs(a, l, m - 1, t);

    return bs(a, m + 1, h, t);
}

int main() {
    int n, t;

    scanf("%d", &n);

    int a[n];

    for (int i = 0; i < n; i++)
        scanf("%d", &a[i]);

    scanf("%d", &t);

    printf("%d\n", bs(a, 0, n - 1, t));
    return 0;
}

Sample Input

6
2 5 8 11 14 19
14

Sample Output

4
7. First occurrence in sorted array

Find the first occurrence of a duplicate target using Binary Search.

Input: sorted array and target

Output: first index

When target is found, store mid and continue searching the left half.
💻 Solve It Yourself — First occurrence in sorted array

C Code Editor

Sample Input

Program Output

Run your program to see the output.

Test Cases

No tests checked yet.
Best Score0 / 100
Attempts0
StatusNot Solved
Write your C program and test it. You can do it! 💪
Scoring: up to 100 marks without help; up to 90 after viewing a hint. If the full solution is opened, the problem can still be marked Completed, but it is no longer counted as a competitive score.
#include <stdio.h>

int main() {
    int n, t, l = 0, h, pos = -1;

    scanf("%d", &n);

    int a[n];

    for (int i = 0; i < n; i++)
        scanf("%d", &a[i]);

    scanf("%d", &t);

    h = n - 1;

    while (l <= h) {
        int m = l + (h - l) / 2;

        if (a[m] == t) {
            pos = m;
            h = m - 1;
        } else if (a[m] < t) {
            l = m + 1;
        } else {
            h = m - 1;
        }
    }

    printf("%d\n", pos);
    return 0;
}

Sample Input

7
2 4 4 4 7 9 12
4

Sample Output

1
8. Last occurrence in sorted array

Find the last occurrence of a target using Binary Search.

Input: sorted array and target

Output: last index

When target is found, store mid and continue searching the right half.
💻 Solve It Yourself — Last occurrence in sorted array

C Code Editor

Sample Input

Program Output

Run your program to see the output.

Test Cases

No tests checked yet.
Best Score0 / 100
Attempts0
StatusNot Solved
Write your C program and test it. You can do it! 💪
Scoring: up to 100 marks without help; up to 90 after viewing a hint. If the full solution is opened, the problem can still be marked Completed, but it is no longer counted as a competitive score.
#include <stdio.h>

int main() {
    int n, t, l = 0, h, pos = -1;

    scanf("%d", &n);

    int a[n];

    for (int i = 0; i < n; i++)
        scanf("%d", &a[i]);

    scanf("%d", &t);

    h = n - 1;

    while (l <= h) {
        int m = l + (h - l) / 2;

        if (a[m] == t) {
            pos = m;
            l = m + 1;
        } else if (a[m] < t) {
            l = m + 1;
        } else {
            h = m - 1;
        }
    }

    printf("%d\n", pos);
    return 0;
}

Sample Input

7
2 4 4 4 7 9 12
4

Sample Output

3
9. Count occurrences in sorted array

Find how many times a target occurs.

Input: sorted array and target

Output: count

Find first and last occurrence; count = last - first + 1.
💻 Solve It Yourself — Count occurrences in sorted array

C Code Editor

Sample Input

Program Output

Run your program to see the output.

Test Cases

No tests checked yet.
Best Score0 / 100
Attempts0
StatusNot Solved
Write your C program and test it. You can do it! 💪
Scoring: up to 100 marks without help; up to 90 after viewing a hint. If the full solution is opened, the problem can still be marked Completed, but it is no longer counted as a competitive score.
#include <stdio.h>

int first(int a[], int n, int t) {
    int l = 0, h = n - 1, p = -1;

    while (l <= h) {
        int m = l + (h - l) / 2;

        if (a[m] == t) {
            p = m;
            h = m - 1;
        } else if (a[m] < t) {
            l = m + 1;
        } else {
            h = m - 1;
        }
    }

    return p;
}

int last(int a[], int n, int t) {
    int l = 0, h = n - 1, p = -1;

    while (l <= h) {
        int m = l + (h - l) / 2;

        if (a[m] == t) {
            p = m;
            l = m + 1;
        } else if (a[m] < t) {
            l = m + 1;
        } else {
            h = m - 1;
        }
    }

    return p;
}

int main() {
    int n, t;

    scanf("%d", &n);

    int a[n];

    for (int i = 0; i < n; i++)
        scanf("%d", &a[i]);

    scanf("%d", &t);

    int f = first(a, n, t), l = last(a, n, t);

    printf("%d\n", f == -1 ? 0 : l - f + 1);
    return 0;
}

Sample Input

8
1 2 2 2 3 4 4 7
2

Sample Output

3
10. Search in rotated sorted array

Search a target in a rotated sorted array.

Input: rotated sorted array and target

Output: index or -1

At every step, identify which half is sorted.
💻 Solve It Yourself — Search in rotated sorted array

C Code Editor

Sample Input

Program Output

Run your program to see the output.

Test Cases

No tests checked yet.
Best Score0 / 100
Attempts0
StatusNot Solved
Write your C program and test it. You can do it! 💪
Scoring: up to 100 marks without help; up to 90 after viewing a hint. If the full solution is opened, the problem can still be marked Completed, but it is no longer counted as a competitive score.
#include <stdio.h>

int main() {
    int n, t, l = 0, h, pos = -1;

    scanf("%d", &n);

    int a[n];

    for (int i = 0; i < n; i++)
        scanf("%d", &a[i]);

    scanf("%d", &t);

    h = n - 1;

    while (l <= h) {
        int m = l + (h - l) / 2;

        if (a[m] == t) {
            pos = m;
            break;
        }

        if (a[l] <= a[m]) {
            if (a[l] <= t && t < a[m])
                h = m - 1;
            else
                l = m + 1;
        } else {
            if (a[m] < t && t <= a[h])
                l = m + 1;
            else
                h = m - 1;
        }
    }

    printf("%d\n", pos);
    return 0;
}

Sample Input

7
8 9 10 2 3 5 6
5

Sample Output

5
11. Jump Search

Search a sorted array using Jump Search.

Input: sorted array and target

Output: index or -1

Jump √n positions until you cross the target, then linearly scan that block.
💻 Solve It Yourself — Jump Search

C Code Editor

Sample Input

Program Output

Run your program to see the output.

Test Cases

No tests checked yet.
Best Score0 / 100
Attempts0
StatusNot Solved
Write your C program and test it. You can do it! 💪
Scoring: up to 100 marks without help; up to 90 after viewing a hint. If the full solution is opened, the problem can still be marked Completed, but it is no longer counted as a competitive score.
#include <stdio.h>

int main() {
    int n, t, step = 1, prev = 0, pos = -1;

    scanf("%d", &n);

    int a[n];

    for (int i = 0; i < n; i++)
        scanf("%d", &a[i]);

    scanf("%d", &t);

    while (step * step < n)
        step++;

    int next = step;

    while (prev < n && a[(next < n ? next : n) - 1] < t) {
        prev = next;
        next += step;

        if (prev >= n)
            break;
    }

    while (prev < n && prev < next) {
        if (a[prev] == t) {
            pos = prev;
            break;
        }

        if (a[prev] > t)
            break;

        prev++;
    }

    printf("%d\n", pos);
    return 0;
}

Sample Input

9
1 4 7 10 13 16 19 22 25
19

Sample Output

6
12. Interpolation Search

Search uniformly distributed sorted data using interpolation.

Input: sorted array and target

Output: index or -1

Estimate the position using the values at low and high.
💻 Solve It Yourself — Interpolation Search

C Code Editor

Sample Input

Program Output

Run your program to see the output.

Test Cases

No tests checked yet.
Best Score0 / 100
Attempts0
StatusNot Solved
Write your C program and test it. You can do it! 💪
Scoring: up to 100 marks without help; up to 90 after viewing a hint. If the full solution is opened, the problem can still be marked Completed, but it is no longer counted as a competitive score.
#include <stdio.h>

int main() {
    int n, t, l = 0, h, pos = -1;

    scanf("%d", &n);

    int a[n];

    for (int i = 0; i < n; i++)
        scanf("%d", &a[i]);

    scanf("%d", &t);

    h = n - 1;

    while (l <= h && t >= a[l] && t <= a[h]) {
        if (a[l] == a[h]) {
            if (a[l] == t)
                pos = l;
            break;
        }

        int p = l + (int)((long long)(t - a[l]) * (h - l) / (a[h] - a[l]));

        if (a[p] == t) {
            pos = p;
            break;
        }

        if (a[p] < t)
            l = p + 1;
        else
            h = p - 1;
    }

    printf("%d\n", pos);
    return 0;
}

Sample Input

10
10 20 30 40 50 60 70 80 90 100
90

Sample Output

8
13. Fibonacci Search

Search a sorted array using Fibonacci partitions.

Input: sorted array and target

Output: index or -1

Generate Fibonacci numbers until the largest one is at least n, then use the Fibonacci offsets to probe.
💻 Solve It Yourself — Fibonacci Search

C Code Editor

Sample Input

Program Output

Run your program to see the output.

Test Cases

No tests checked yet.
Best Score0 / 100
Attempts0
StatusNot Solved
Write your C program and test it. You can do it! 💪
Scoring: up to 100 marks without help; up to 90 after viewing a hint. If the full solution is opened, the problem can still be marked Completed, but it is no longer counted as a competitive score.
#include <stdio.h>

int main() {
    int n, t;

    scanf("%d", &n);

    int a[n];

    for (int i = 0; i < n; i++)
        scanf("%d", &a[i]);

    scanf("%d", &t);

    int f2 = 0, f1 = 1, f = f1 + f2;

    while (f < n) {
        f2 = f1;
        f1 = f;
        f = f1 + f2;
    }

    int off = -1, pos = -1;

    while (f > 1) {
        int i = off + f2;

        if (i >= n)
            i = n - 1;

        if (a[i] < t) {
            f = f1;
            f1 = f2;
            f2 = f - f1;
            off = i;
        } else if (a[i] > t) {
            f = f2;
            f1 = f1 - f2;
            f2 = f - f1;
        } else {
            pos = i;
            break;
        }
    }

    if (pos == -1 && f1 && off + 1 < n && a[off + 1] == t)
        pos = off + 1;

    printf("%d\n", pos);
    return 0;
}

Sample Input

8
5 10 15 20 25 30 35 40
30

Sample Output

5
14. Lower Bound

Find the first index whose value is greater than or equal to the target.

Input: sorted array and target

Output: lower-bound index

If a[mid] is at least target, store mid and move left.
💻 Solve It Yourself — Lower Bound

C Code Editor

Sample Input

Program Output

Run your program to see the output.

Test Cases

No tests checked yet.
Best Score0 / 100
Attempts0
StatusNot Solved
Write your C program and test it. You can do it! 💪
Scoring: up to 100 marks without help; up to 90 after viewing a hint. If the full solution is opened, the problem can still be marked Completed, but it is no longer counted as a competitive score.
#include <stdio.h>

int main() {
    int n, t, l = 0, h, pos;

    scanf("%d", &n);

    int a[n];

    for (int i = 0; i < n; i++)
        scanf("%d", &a[i]);

    scanf("%d", &t);

    h = n;
    pos = n;

    while (l < h) {
        int m = l + (h - l) / 2;

        if (a[m] >= t) {
            pos = m;
            h = m;
        } else {
            l = m + 1;
        }
    }

    printf("%d\n", pos);
    return 0;
}

Sample Input

6
2 4 7 9 12 15
8

Sample Output

3
15. Upper Bound

Find the first index whose value is greater than the target.

Input: sorted array and target

Output: upper-bound index

If a[mid] is greater than target, store mid and move left; otherwise move right.
💻 Solve It Yourself — Upper Bound

C Code Editor

Sample Input

Program Output

Run your program to see the output.

Test Cases

No tests checked yet.
Best Score0 / 100
Attempts0
StatusNot Solved
Write your C program and test it. You can do it! 💪
Scoring: up to 100 marks without help; up to 90 after viewing a hint. If the full solution is opened, the problem can still be marked Completed, but it is no longer counted as a competitive score.
#include <stdio.h>

int main() {
    int n, t, l = 0, h, pos;

    scanf("%d", &n);

    int a[n];

    for (int i = 0; i < n; i++)
        scanf("%d", &a[i]);

    scanf("%d", &t);

    h = n;
    pos = n;

    while (l < h) {
        int m = l + (h - l) / 2;

        if (a[m] > t) {
            pos = m;
            h = m;
        } else {
            l = m + 1;
        }
    }

    printf("%d\n", pos);
    return 0;
}

Sample Input

6
2 4 7 9 12 15
9

Sample Output

4
16. Search insertion position

Find the position where a target should be inserted in sorted order.

Input: sorted array and target

Output: insertion index

The insertion position is the lower bound of the target.
💻 Solve It Yourself — Search insertion position

C Code Editor

Sample Input

Program Output

Run your program to see the output.

Test Cases

No tests checked yet.
Best Score0 / 100
Attempts0
StatusNot Solved
Write your C program and test it. You can do it! 💪
Scoring: up to 100 marks without help; up to 90 after viewing a hint. If the full solution is opened, the problem can still be marked Completed, but it is no longer counted as a competitive score.
#include <stdio.h>

int main() {
    int n, t, l = 0, h;

    scanf("%d", &n);

    int a[n];

    for (int i = 0; i < n; i++)
        scanf("%d", &a[i]);

    scanf("%d", &t);

    h = n;

    while (l < h) {
        int m = l + (h - l) / 2;

        if (a[m] < t)
            l = m + 1;
        else
            h = m;
    }

    printf("%d\n", l);
    return 0;
}

Sample Input

5
10 20 30 40 50
35

Sample Output

3
17. Find peak element

Find an index of an element that is not smaller than its neighbors.

Input: n and array

Output: peak index

Compare mid with mid+1. If a[mid] is smaller, move right; otherwise move left including mid.
💻 Solve It Yourself — Find peak element

C Code Editor

Sample Input

Program Output

Run your program to see the output.

Test Cases

No tests checked yet.
Best Score0 / 100
Attempts0
StatusNot Solved
Write your C program and test it. You can do it! 💪
Scoring: up to 100 marks without help; up to 90 after viewing a hint. If the full solution is opened, the problem can still be marked Completed, but it is no longer counted as a competitive score.
#include <stdio.h>

int main() {
    int n;

    scanf("%d", &n);

    int a[n];

    for (int i = 0; i < n; i++)
        scanf("%d", &a[i]);

    int l = 0, h = n - 1;

    while (l < h) {
        int m = l + (h - l) / 2;

        if (a[m] < a[m + 1])
            l = m + 1;
        else
            h = m;
    }

    printf("%d\n", l);
    return 0;
}

Sample Input

6
1 3 8 7 5 2

Sample Output

2
18. Integer square root using Binary Search

Find floor(√x) without using a square-root library.

Input: non-negative integer x

Output: floor square root

Binary-search the answer from 0 to x and keep the largest mid whose square is at most x.
💻 Solve It Yourself — Integer square root using Binary Search

C Code Editor

Sample Input

Program Output

Run your program to see the output.

Test Cases

No tests checked yet.
Best Score0 / 100
Attempts0
StatusNot Solved
Write your C program and test it. You can do it! 💪
Scoring: up to 100 marks without help; up to 90 after viewing a hint. If the full solution is opened, the problem can still be marked Completed, but it is no longer counted as a competitive score.
#include <stdio.h>

int main() {
    long long x;

    scanf("%lld", &x);

    long long l = 0, h = x, ans = 0;

    while (l <= h) {
        long long m = l + (h - l) / 2;

        if (m == 0 || m <= x / m) {
            ans = m;
            l = m + 1;
        } else {
            h = m - 1;
        }
    }

    printf("%lld\n", ans);
    return 0;
}

Sample Input

50

Sample Output

7
19. Search in a nearly sorted array

Each element may be at its correct position or one position away. Find the target.

Input: nearly sorted array and target

Output: index or -1

Check mid, mid-1, and mid+1 before moving the search range.
💻 Solve It Yourself — Search in a nearly sorted array

C Code Editor

Sample Input

Program Output

Run your program to see the output.

Test Cases

No tests checked yet.
Best Score0 / 100
Attempts0
StatusNot Solved
Write your C program and test it. You can do it! 💪
Scoring: up to 100 marks without help; up to 90 after viewing a hint. If the full solution is opened, the problem can still be marked Completed, but it is no longer counted as a competitive score.
#include <stdio.h>

int main() {
    int n, t;

    scanf("%d", &n);

    int a[n];

    for (int i = 0; i < n; i++)
        scanf("%d", &a[i]);

    scanf("%d", &t);

    int l = 0, h = n - 1, pos = -1;

    while (l <= h) {
        int m = l + (h - l) / 2;

        if (a[m] == t) {
            pos = m;
            break;
        }

        if (m - 1 >= l && a[m - 1] == t) {
            pos = m - 1;
            break;
        }

        if (m + 1 <= h && a[m + 1] == t) {
            pos = m + 1;
            break;
        }

        if (t < a[m])
            h = m - 2;
        else
            l = m + 2;
    }

    printf("%d\n", pos);
    return 0;
}

Sample Input

6
10 20 30 25 40 50
25

Sample Output

3
20. Find minimum in rotated sorted array

Find the minimum element in a rotated sorted array.

Input: rotated sorted array

Output: minimum value

Compare a[mid] with a[high]. If a[mid] is greater, the minimum lies to the right; otherwise it lies at mid or to the left.
💻 Solve It Yourself — Find minimum in rotated sorted array

C Code Editor

Sample Input

Program Output

Run your program to see the output.

Test Cases

No tests checked yet.
Best Score0 / 100
Attempts0
StatusNot Solved
Write your C program and test it. You can do it! 💪
Scoring: up to 100 marks without help; up to 90 after viewing a hint. If the full solution is opened, the problem can still be marked Completed, but it is no longer counted as a competitive score.
#include <stdio.h>

int main() {
    int n;

    scanf("%d", &n);

    int a[n];

    for (int i = 0; i < n; i++)
        scanf("%d", &a[i]);

    int l = 0, h = n - 1;

    while (l < h) {
        int m = l + (h - l) / 2;

        if (a[m] > a[h])
            l = m + 1;
        else
            h = m;
    }

    printf("%d\n", a[l]);
    return 0;
}

Sample Input

7
15 18 2 3 6 12 13

Sample Output

2
← Previous Topic: Data Structures Overview Next Topic: Sorting →