ProgrammingHardTCS NQT · Accenture
How many pairs (i, j) with i < j in {1, 3, 5, 8, 10} have a difference of exactly 2 (that is, a[j] - a[i] = 2)? Compute with this program.
How many pairs (i, j) with i < j in {1, 3, 5, 8, 10} have a difference of exactly 2 (that is, a[j] - a[i] = 2)? Compute with this program.
#include <stdio.h>
int main(void) {
int a[] = {1, 3, 5, 8, 10}, c = 0;
for (int i = 0; i < 5; i++)
for (int j = i + 1; j < 5; j++)
if (a[j] - a[i] == 2) c++;
printf("%d", c);
}- A. 2
- B. 4
- C. 5
- D. 3
Correct answer: D. 3
The pairs with difference 2 are (1, 3), (3, 5) and (8, 10), so the count is 3.
More C Programming questions
- Starting at node 0, how many nodes does this linked walk visit before it reaches the sentinel -1? The next-index array is {3, 2, 0, -1}. c…
- What is the digital root of 987654 (the repeated digit sum until one digit remains)? The recursion computes it. What is printed? c…
- What is printed? c #include <stdio.h> int main(void) { unsigned x = 5; printf("%d", (x - 6) > 0); }
- How many inner iterations run in total? Compute with this program. c #include <stdio.h> int main(void) { int i = 0, c = 0; do { for (int j…
- What is the sizeof of this struct, assuming char has size 1, int has size 4 with 4-byte alignment, and a nested struct is laid out in…
- What is printed? c #include <stdio.h> long long sum(long long n, long long acc) { return n == 0 ? acc : sum(n - 1, acc + n); } int…