Infosys Specialist Programmer Coding Questions and Answers

Infosys Specialist Programmer Coding Questions and Answers | Infosys Specialist Programmer Important Coding Questions | Infosys SP Coding Question with Answer | Infosys Specialist Programmer Coding Questions PDF Download

Infosys Specialist Programmer Coding Questions and Answers: All of the fundamental algorithms, data structures, and aptitude concepts are included in the Infosys Specialist Programmer Coding Questions. We can assist you if you’re interested in working at Infosys but are unsure about how to get ready for the questions in the Infosys SP Coding Question and Answer and Infosys Specialist Programmer Test Coding Questions. One of the most important sections of the Infosys Specialist Programmer Online test is the coding round. In this article, we covered a variety of sample Infosys Specialist Programmer Coding Questions and Answers 2024. We will talk about Infosys Specialist Programmer Coding Questions PDF and Infosys SP Coding Question and Answers in the below section.

This page will gives you all there is to know about Infosys Specialist Programmer Coding Interview Questions PDF/ Infosys Specialist Programmer Coding Questions. More details like Infosys Specialist Programmer Coding Question with Answer PDF, Infosys SP Coding Questions and answers, Infosys Specialist Programmer Coding Questions, Cut Off, Salary, Hike, Increment, Annual Appraisal, etc. is available on this page.

Infosys Specialist Programmer Coding Questions

 Company Name Infosys Name of the Post Specialist Programmer Category Coding Questions and Answers Official website infosys.com

Infosys Specialist Programmer Coding Questions with Answers

Question 1:

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];

C++

```#include <iostream>
using namespace std;
int main ()
{
int arr[] = { 1, 1, 1 };

int X = 1;
int Y = 1;
int ans = 0;
int arrSize = sizeof (arr) / sizeof (arr[0]);

for (int i = 0; i < arrSize; i++)
{
arr[i]--;
}
ans = ans + X;

for (int i = 0; i < arrSize; i++)
{
if (arr[i] != 0)
{
arr[i] = 0;
ans = ans + Y;
}
}
cout << ans;
return 0;
}```

JAVA

```public class Main
{
public static void main (String[]args)
{

int[] arr = { 1, 1, 1 };

int X = 1;
int Y = 1;
int ans = 0;

for (int i = 0; i < arr.length; i++)
{

arr[i]--;

}
ans = ans + X;

for (int i = 0; i < arr.length; i++)
{
if (arr[i] != 0)
{
arr[i] = 0;
ans = ans + Y;
}
}
System.out.println (ans);
}
}
```
PHYTHON
```n = 1
x = 1
y = 10
arr = [1]
ans = 0
arrSize = len(arr)
for i in range(arrSize):
arr[i] = arr[i] - 1

ans = ans + x
for i in range(arrSize):
if arr[i] != 0:
arr[i] = 0
ans = ans + y
print(ans)
```
Question 2:

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.

C++

```#include<bits/stdc++.h>
using namespace std;
unordered_map < int, int >L;

int main ()
{

int n, k, m;
cin >> n >> k;
vector < int >v (n);

for (int i = 0; i < n; i++) { cin >> m;
v[i] = m;
int j = 0;
while (m)
{
L[j] += (m & 1);
m >>= 1;
j++;
}
}

int j = 0, K = k, ans = 0, ans2 = 0;
while (K)
{
j++;
K >>= 1;
}

for (int i = j; i > 0; i--)
{
if (L[i - 1] < n - L[i - 1])
ans != 1;
ans <<= 1; } ans >>= 1;
while (ans > k)
{
ans &= 0;
ans <<= 1;
k <<= 1;
}

for (auto i:v)
ans2 += ans ^ i;

cout << ans2;

}```

JAVA

```import java.util.*;
class Main
{
public static void main (String[]args)
{

Scanner sc = new Scanner (System.in);
int n = sc.nextInt ();
int k = sc.nextInt ();
int arr[] = new int[n];

for (int i = 0; i < n; i++)
arr[i] = sc.nextInt ();

int res = 0, max = Integer.MIN_VALUE;

for (int i = 0; i <= k; i++)
{
res = 0;
for (int j = 0; j < n; j++)
res = res + (i ^ arr[j]);
max = Math.max (res, max);
}
System.out.println (max);
}
}```

PHYTHON

```def Xor_sum(x, arr):
xorSum = sum(arr)

for i in range(1, x):
s = 0
for j in arr:
s += i ^ j
if s > xorSum:
xorSum = s
return xorSum

n = 4
x = 9
arr = [7, 4, 0, 3]
print(Xor_sum(x, arr))```

Question 3:

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

C++

```#include <bits/stdc++.h>
#define sz(x) ((int)(x).size())
#define inf mod

using namespace std;

const int maxn = (int) 5e6 + 100;
int n, t[2][maxn], id = 1;
int dp[2][30];
vector < int >g[maxn];

void add (int x, int pos)
{
int v = 0;
for (int i = 29; i >= 0; i--)
{
int bit = ((x >> i) & 1);
if (!t[bit][v])
t[bit][v] = id++;
v = t[bit][v];
g[v].push_back (pos);
}
}

void go (int v, int b = 29)
{
int l = t[0][v], r = t[1][v];
if (l)
go (l, b - 1);
if (r)
go (r, b - 1);
if (!l || !r)
return;
int res = 0;
int ptr = 0;
for (auto x:g[l])
{
while (ptr < sz (g[r]) && g[r][ptr] < x)
ptr++;
res += ptr;
}
dp[0][b] += res;
dp[1][b] += sz (g[l]) * 1ll * sz (g[r]) - res;
}

int main ()
{
cin >> n;
for (int i = 1; i <= n; i++)
{
int x;
cin >> x;
}
go (0);
int inv = 0;
int res = 0;
for (int i = 0; i <= 29; i++)
{
inv += min (dp[0][i], dp[1][i]);
if (dp[1][i] < dp[0][i])
res += (1 << i);
}
cout << inv;
}
```
Question 4:

Find the smallest and largest number in an Array

Sample Input

[3, 1, 56, 34, 12, 9, 98, 23, 4]

C++

#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> arr = {3, 1, 56, 34, 12, 9, 98, 23, 4};
int minVal = arr[0];
int maxVal = arr[0];
for(int i = 1; i < arr.size(); i++) {
if(arr[i] > maxVal) maxVal = arr[i];
if(arr[i] < minVal) minVal = arr[i];
}
cout << “Smallest Number: “ << minVal << endl;
cout << “Largest Number: “ << maxVal << endl;
return 0;
}

JAVA

public class Main {
public static void main(String[] args) {
int[] arr = {3, 1, 56, 34, 12, 9, 98, 23, 4};
int minVal = arr[0];
int maxVal = arr[0];
for(int i = 1; i < arr.length; i++) {
if(arr[i] > maxVal) maxVal = arr[i];
if(arr[i] < minVal) minVal = arr[i];
}
System.out.println(“Smallest Number: “ + minVal);
System.out.println(“Largest Number: “ + maxVal);
}
}

PHYTHON

```arr = [3, 1, 56, 34, 12, 9, 98, 23, 4]
minVal = min(arr)
maxVal = max(arr)

print(f"Smallest Number: {minVal}")
print(f"Largest Number: {maxVal}")```

Output

Smallest Number: 1
Largest Number: 98

Question 5:

How do you rotate a matrix by 90 degrees? Write a program.

Sample Input:

1 2 3

4 5 6

7 8 9

C++
#include <iostream>
#include <vector>
using namespace std;
void rotateMatrix(vector<vector<int>>& matrix) {
int n = matrix.size();
// Transpose the matrix
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
swap(matrix[i][j], matrix[j][i]);
}
}
// Reverse each row
for (int i = 0; i < n; ++i) {
int left = 0, right = n – 1;
while (left < right) {
swap(matrix[i][left], matrix[i][right]);
left++;
right–;
}
}
}
int main() {
// Input matrix
vector<vector<int>> matrix = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}};
// Print original matrix
cout << “Original Matrix:” << endl;
for (const auto& row : matrix) {
for (int val : row) {
cout << val << ” “;
}
cout << endl;
}
// Rotate matrix
rotateMatrix(matrix);
// Print rotated matrix
cout << “Rotated Matrix:” << endl;
for (const auto& row : matrix) {
for (int val : row) {
cout << val << ” “;
}
cout << endl;
}
return 0;
}

PHYTHON

def rotate_matrix(matrix):
n = len(matrix)
# Transpose the matrix
for i in range(n):
for j in range(i + 1, n):
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
# Reverse each row
for i in range(n):
matrix[i] = matrix[i][::-1]
# Input matrix
matrix = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
# Print original matrix
print(“Original Matrix:”)
for row in matrix:
print(row)
# Rotate matrix
rotate_matrix(matrix)
# Print rotated matrix
print(“Rotated Matrix:”)
for row in matrix:
print(row)

JAVA

import java.util.Arrays;
public class RotateMatrix {
public static void rotateMatrix(int[][] matrix) {
int n = matrix.length;
// Transpose the matrix
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
// Reverse each row
for (int i = 0; i < n; ++i) {
int left = 0, right = n – 1;
while (left < right) {
int temp = matrix[i][left];
matrix[i][left] = matrix[i][right];
matrix[i][right] = temp;
left++;
right–;
}
}
}

public static void main(String[] args) { // Input matrix

int[][] matrix = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}};
// Print original matrix
System.out.println(“Original Matrix:”);
for (int[] row : matrix) {
System.out.println(Arrays.toString(row));
}
// Rotate matrix
rotateMatrix(matrix);
// Print rotated matrix
System.out.println(“Rotated Matrix:”);
for (int[] row : matrix) {
System.out.println(Arrays.toString(row));
}
}

}

Output:

Rotated Matrix:

7 4 1

8 5 2

9 6 3

Keep watch dailyrecruitment.in site for more upcoming exams notifications.

Govt Jobs by Qualifications

Education & Vacancies Salary Apply Link
10th Pass Govt Jobs - 5,000 Vacancies Rs. 5,200 - 63,200 Apply Now
12th Pass Govt Jobs - 18,000+ Vacancies Rs. 5,200 - 92,300 Apply Now
ITI Pass Jobs - 3,500 Vacancies Rs. 5,200 - 35,000 Apply Now
Any Graduate Jobs - 19,100 Vacancies Rs. 5,200 - 92,300 Apply Now
Central Govt Jobs Rs. 5,200 - 17,000 Apply Now
Bank Jobs - 1,000 Vacancies Rs. 5,200 - 29,200 Apply Now
Diploma Jobs - 9,300 Vacancies Rs. 5,200 - 35,000 Apply Now
BTech/BE Jobs - 18,000 Vacancies Rs. 15,000 - 1,00,000 Apply Now
Data Entry Jobs - 1,300 Vacancies Rs. 5,200 - 29,200 Apply Now
Private Jobs Rs. 10,000 - 67,700 Apply Now