ProgrammingHardTCS NQT · Infosys
For a = {2, 4, 2, 2, 6, 4}, how many index pairs i < j satisfy a[i] == a[j]?
- A. 4
- B. 3
- C. 5
- D. 6
Correct answer: A. 4
The value 2 occurs three times, giving 3 pairs, and 4 occurs twice, giving 1 pair, for a total of 4. Counting each value's c(c-1)/2 pairs after sorting runs in O(N log N), while the nested loop is too slow for N = 2*10^5.
More C Programming questions
- uint32t fact(uint32t n) { return n <= 1 ? 1 : n fact(n - 1); } What does fact(13) return?
- uint8t acc = 0; uint8t a[3] = {250, 3, 7}; for each element, acc += a[i]; What is acc after all three additions?
- int a[] = {3, 5, 6, 9}; int pre[5]; pre[0] = 0, and pre[i] = pre[i-1] ^ a[i-1] for i = 1..4. What is pre[4] ^ pre[1]?
- void f(int a) { sizet n = sizeof a / sizeof a[0]; ... } is called as f(arr) where int arr[4]. What is true about n?
- unsigned int n = 0u; int i = 0; if (i < n - 1) printf("loop"); What is printed?
- Is the C standard guaranteed to remove stack frames of tail-recursive calls, so that deep tail recursion cannot overflow the stack?