# Infosys Specialist Programmer Coding Questions

## InfyTQ Specialist Programmer

Infosys is hiring freshers for a new post of Specialist Programmer, where the salary package being offered is higher from the normal Infosys and Infytq. The difficulty level of this test is also comparatively higher than the other hiring test of Infosys.

In this article we have provided some Sample Practice Questions for Infosys Specialist Programmer Coding Profile, based on the pattern of previous year papers, make sure you study through all the questions in order to score better in the hiring test.

## More details about the Specialist Programmer Test

For the profile of Specialist Programmer (SP) roles the first stage of the selection process for these roles is the Infosys Online test.

• Infosys test will contain 3 questions in 3 hours:
• The test will have a sectional cut-off as well as the total cut-off.

#### Programming languages allowed to submit solutions for the above questions are:-

• C
• C++
• Java
• Python
• JavaScript

Each Question will have a different “Difficulty Level“.

1. Easy- that can be solved by applying the basic application of aptitude, algorithm, and data structures.
2. Medium- that can be solved by applying the basics of the Greedy algorithm.
3. Hard – Usually a question based on Dynamic Programming.
No. of questionsTiming
3 Coding Question3 Hours

## Question 1

Problem Statement :

You are given an array a of N Integers and asked to split the array a into k consecutive segments such that each element of a belongs to exactly one segment and the sum of the cost of all segments is minimum.

We define the cost of some segment t as the sum of distances between the first and last occurrence for each unique element in the segment t.

Your task is to find the minimum possible total sum of the cost of all segments.

Input Format

• The first line contains an integer, n, denoting the number of elements in a.
• The next line contains an integer, k, denoting the Number of required consecutive segments..
• Each line i of the n subsequent lines (where 0 <=i<n) contains an integer describing a[i].

Constraints :

• 1 <= n<= 35000
• 1 <=k <=min(n,100)
• 1 <=a[i] <=n
Sample InputSample OutputExplanation
1
1
1
0The only possible segment is [1] The cost is 1-1=0
7
2
1
6
6
4
6
6
6
3We can divide the array into [1,6,6,4] and [6,6,6] Cost of [1,6,6, 4] will be (1-1)+(3-2)+(4-4)=1 and cost of [6 ,6,6] will be 3-1=2. Total cost would be 1+2=3

## Question 2

Problem Statement : Wael wants to play Santa this year, so he prepared gifts for all the children of the neighborhood. He decided to pack the gifts in boxes and give each child a box. Let’s define the Value of the box as the number of distinct types of gifts inside this box.
Wael has N gifts, such that the type of each gift i is A[i]. Wael wants to pack exactly K boxes, and he has to put in each box a sub-array of consecutive gifts
Wael wants to maximize the total value of all boxes with gifts. Your task is to help him determine this maximum possible total value

Notes

• Each gift should be put in exactly one box, and each box should contain a sub-array of consecutive gifts.
• A box cannot be left empty

Input Format

• The first line contains an integer, N, denoting the number of elements in A
• The next line contains an integer, K, denoting the number of boxes.
• Each line i of the N subsequent lines (where 0≤i<N) contains an integer describing Ai

Constraints

• 1 <= N <= 35000
• 1 <= K <= Min (N,50)
• 1 <= A[i] <= N

Sample InputSample OutputExplanation
1
1
1
1Wael will put the only gift in a box so the total value will be 1.
4
1
1
2
2
1
2Wael has only one box he has to put all gifts in it, so that there are two types of gifts in the box, so the value is equal to 2
7
2
1
3
3
1
4
4
4
5It is optimal to put the first two gifts in the first box, and all the rest in the second There are two distinct types in the first box, and three in the second box then, so the total value is 5.

## Question 3

Problem Statement :
A subarray of array A is a segment of contiguous elements in array A.
Given an array A of N elements, you can apply the following operations as many times as you like:
– Choosing a subarray [L, R] and subtracting 1 from each element in this subarray. The cost of this operation is X.
– Choosing an index i such that A[i] is positive, and setting A[i] = 0. The cost of this operation in Y.

Your task is to make all the elements equal to 0 and find the minimum cost to do so.

Input Format

• The first line contains an integer, N., denoting the number of elements in A.
• The next line contains an integer, X, denoting the cost of the first operation.
• The next line contains an integer. Y, denoting the cost of the second operation
• Each line i of the N subsequent lines (where 1 <=i<= N) contains an Integer describing Ai.

Constraints

• 1<=N<=10^5
• 1<=X<=10
• 1<=Y<=10^4
• 1<=A[i]<=10^8

Sample Input 1

1
1
10
1

Sample Output 1

1

Explanation:

N=1 X=1 Y=10 A=[1]. The optimal solution is to perform one operation of the first type on the subarray [1,N].

Sample Input 2

3
1
1
1
1
1

Sample Output 2

1

Explanation:

N=3 X=1 Y=1 A=[1,1,1] The optimal solution is to perform one operation of the first type on the subarray[1,N];

## Question 4

Problem Statement :

Given an array A of N elements. You should choose a value B such that (B>=0), and then for each element in A set A[i]=A[i](+)B where is the bitwise XOR.
Print the minimum number of inversions in array A that you can achieve after choosing the value of B optimally and setting A[i] = A[i] (+) B. Since the answer might be large, print it modulo (10^9+7)

Input Format

• The first line contains an integer, N. denoting the number of elements in A
• Then the next line contains N elements, denoting the elements in A.

Input :

4
1 0 3 2

Output
1

## Question 5

Problem Statement :

Wael is well-known for how much he loves the bitwise XOR operation, while kaito is well known for how much he loves to sum numbers, so their friend Resli          decided to make up a problem that would enjoy both of them. Resil wrote down an array A of length N, an integer K and he defined a new function called  Xor-        sum as follows

• Xor-sum(x)=(x XOR A[1])+(x XOR A[2])+(x XOR A[3])+…………..+(x XOR A[N])

Can you find the integer x in the range [0,K] with the maximum Xor-sum (x) value?

Print only the value.

Input format

•  The first line contains integer N denoting the number of elements in A.
• The next line contains an integer, k, denoting the maximum value of x.
• Each line i of the N subsequent lines(where 0<=i<=N) contains an integer describing Ai.

Constraints

• 1<=N<=10^5
• 0<=K<=10^9
• 0<=A[i]<=10^9

Sample Input 1

1
0
989898

Sample Output 1

989898

Explanation:

Xor_sum(0)=(0^989898)=989898

Sample Input 2

3
7
1
6
3

Sample Output 2

14

Explanation

Xor_sum(4)=(4^1)+(4^6)+(4^3)=14.

Sample Input 3

4
9
7
4
0
3

Sample Output 3

46

Explanation:

Xor_sum(8)=(8^7)+(8^4) +(8^0)+(8^3)=46.