Infosys SP / DSE coding mock 2
One easy, one medium and one hard problem in 3 hours, like the reported pattern (20, 30 and 50 marks).
3 problems, 3 hours, marks 20 / 30 / 50.
Problem 1: Fewest exercises to get tired (Easy, 20 marks)
You start with E energy. Exercise i uses A[i] energy, and each exercise can be done at most twice. You are tired when your energy reaches 0 or less. Find the fewest exercises needed to get tired, or -1 if doing every exercise twice is not enough.
Input: First line: E. Second line: N. Third line: N values.
Output: The fewest exercises, or -1.
Constraints: 1 ≤ N ≤ 10^5; 1 ≤ E, A[i] ≤ 10^9.
Input:
6
2
1 2
Output:
4Problem 2: Longest holiday after cancelling (Medium, 30 marks)
Days are numbered 1 to N, and M of them have an appointment. You may cancel at most K appointments. Find the longest run of consecutive days without an appointment you can get.
Input: First line: N M K. Second line: the M appointment days.
Output: The longest run of free days.
Constraints: 1 ≤ N ≤ 10^9; 1 ≤ M ≤ 10^5; 0 ≤ K ≤ M; days are distinct.
Input:
10 3 1
3 6 8
Output:
5Problem 3: Best x for the largest XOR-sum (Hard, 50 marks)
Given an array A and a limit K, find the x between 0 and K that makes (x XOR A[1]) + (x XOR A[2]) + … + (x XOR A[N]) as large as possible, and print that largest sum.
Input: First line: N and K. Second line: N integers.
Output: The largest XOR-sum.
Constraints: 1 ≤ N ≤ 10^5; 0 ≤ K, A[i] ≤ 10^9.
Input:
3 7
1 2 3
Output:
18