Bird
Raised Fist0
Cprogramming~5 mins

Multiple input and output in C - Time & Space Complexity

Choose your learning style10 modes available
Time Complexity: Multiple input and output
O(n + m)
Understanding Time Complexity

When a program takes several inputs and produces several outputs, we want to know how the time it takes changes as the inputs grow.

We ask: How does the work increase when we have more inputs to handle?

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


#include <stdio.h>

void process(int a[], int n, int b[], int m) {
    for (int i = 0; i < n; i++) {
        printf("%d ", a[i]);
    }
    for (int j = 0; j < m; j++) {
        printf("%d ", b[j]);
    }
}

int main() {
    int arr1[] = {1, 2, 3};
    int arr2[] = {4, 5};
    process(arr1, 3, arr2, 2);
    return 0;
}
    

This code prints all elements from two separate arrays, each with its own size.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: Two separate loops printing elements of each array.
  • How many times: First loop runs n times, second loop runs m times.
How Execution Grows With Input

As the sizes of the two arrays grow, the total work grows by adding the work for each array.

Input Size (n, m)Approx. Operations
10, 5About 15 operations
100, 50About 150 operations
1000, 500About 1500 operations

Pattern observation: The total work grows roughly by adding the sizes of both inputs.

Final Time Complexity

Time Complexity: O(n + m)

This means the time grows in a straight line with the sum of both input sizes.

Common Mistake

[X] Wrong: "The time complexity is O(n * m) because there are two inputs."

[OK] Correct: The loops run one after another, not inside each other, so their times add, not multiply.

Interview Connect

Understanding how multiple inputs affect time helps you explain your code clearly and shows you can think about real problems with several parts.

Self-Check

"What if the two loops were nested instead of separate? How would the time complexity change?"

Practice

(1/5)
1. What does the following C code do?
int a, b;
scanf("%d %d", &a, &b);
printf("%d %d", a, b);
easy
A. Prints two integers without reading input.
B. Reads one integer and prints it twice.
C. Reads two integers from input and prints them in the same order.
D. Reads two integers but prints only the first one.

Solution

  1. Step 1: Understand scanf with multiple inputs

    The scanf function reads two integers from the user input and stores them in variables a and b.
  2. Step 2: Understand printf with multiple outputs

    The printf function prints the values of a and b in the same order they were read.
  3. Final Answer:

    Reads two integers from input and prints them in the same order. -> Option C
  4. Quick Check:

    scanf and printf with two %d = same two integers [OK]
Hint: Match each %d with a variable and & in scanf [OK]
Common Mistakes:
  • Forgetting & before variables in scanf
  • Mixing order of variables in printf
  • Using wrong format specifiers
2. Which of the following is the correct syntax to read three integers a, b, c in C?
easy
A. scanf("%d %d %d", &a, &b, &c);
B. scanf("%d %d %d", a, b, c);
C. scanf("%d, %d, %d", &a, &b, &c);
D. scanf("%d %d %d", *a, *b, *c);

Solution

  1. Step 1: Check scanf format string and arguments

    The format string should have three %d separated by spaces, and variables must be passed by address using &.
  2. Step 2: Identify correct option

    scanf("%d %d %d", &a, &b, &c); uses correct format string and & before variables, so it is correct.
  3. Final Answer:

    scanf("%d %d %d", &a, &b, &c); -> Option A
  4. Quick Check:

    Use & with variables in scanf [OK]
Hint: Always use & before variables in scanf [OK]
Common Mistakes:
  • Omitting & before variables
  • Using commas inside format string incorrectly
  • Using pointers incorrectly with *
3. What is the output of this C program?
int x, y;
scanf("%d %d", &x, &y);
printf("%d %d %d", x, y, x + y);

Input:
3 7
medium
A. 10 10 20
B. 3 7 10
C. 7 3 10
D. 3 7 37

Solution

  1. Step 1: Read input values

    scanf reads 3 into x and 7 into y.
  2. Step 2: Calculate and print output

    printf prints x (3), y (7), and x + y (3 + 7 = 10).
  3. Final Answer:

    3 7 10 -> Option B
  4. Quick Check:

    3 + 7 = 10 [OK]
Hint: Add variables inside printf for combined output [OK]
Common Mistakes:
  • Mixing order of variables in printf
  • Confusing concatenation with addition
  • Not using & in scanf
4. Find the error in this code snippet:
int a, b;
scanf("%d %d", a, b);
printf("%d %d", a, b);
medium
A. Missing semicolon after scanf.
B. Wrong format specifier in scanf.
C. printf format specifiers do not match variables.
D. Missing & before variables in scanf.

Solution

  1. Step 1: Check scanf arguments

    scanf requires addresses of variables, so &a and &b are needed.
  2. Step 2: Identify error

    Code passes variables a and b directly, causing undefined behavior or crash.
  3. Final Answer:

    Missing & before variables in scanf. -> Option D
  4. Quick Check:

    scanf needs & before variables [OK]
Hint: Always use & before variables in scanf [OK]
Common Mistakes:
  • Forgetting & in scanf
  • Assuming variables auto-pass by address
  • Confusing scanf and printf syntax
5. Write a C program snippet that reads two integers and prints their sum and product separated by a space. Which code correctly does this?
hard
A. int a, b;
scanf("%d %d", &a, &b);
printf("%d %d", a + b, a * b);
B. int a, b;
scanf("%d %d", a, b);
printf("%d %d", a + b, a * b);
C. int a, b;
scanf("%d %d", &a, &b);
printf("%d %d", a - b, a / b);
D. int a, b;
scanf("%d %d", &a, &b);
printf("%d %d", a * b, a + b);

Solution

  1. Step 1: Check input reading correctness

    scanf must use &a and &b to read two integers correctly.
  2. Step 2: Check output matches sum and product

    printf should print sum (a + b) first, then product (a * b) separated by space.
  3. Final Answer:

    int a, b; scanf("%d %d", &a, &b); printf("%d %d", a + b, a * b); -> Option A
  4. Quick Check:

    scanf with & and printf sum then product [OK]
Hint: Print sum first, product second with correct scanf [OK]
Common Mistakes:
  • Forgetting & in scanf
  • Swapping sum and product order
  • Using wrong operators in printf
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard