InfyTQ Coding Questions and Answers 2023 (FAQ): Previous Q/A, Expected Q/A, & More

InfyTQ Coding Questions and Answers 2023 | InfyTQ Previous Year Coding Questions | InfyTQ Coding Questions with Answers Java | InfyTQ Python Coding Questions with Answers | InfyTQ Coding Round Questions

InfyTQ Coding Questions and Answers 2023 | InfyTQ Coding Questions 2023: The coding test is of medium complexity, which implies that although it is not impossible to pass, it is also not particularly easy. You would surely have an edge in the next InfyTQ coding test if you practiced some of the code Questions from last year’s test. Let’s examine a few C, C++, Java, and Python, Test patterns, InfyTQ Coding Questions Python, code issues, and their remedies. Prior recruiting campaigns included a heavy emphasis on arrays and other basic programming concepts in their coding tests. With enough preparation, one may easily pass the first round and even subsequent rounds. To answer the coding tasks, you must pick a language that you are familiar with, such as C, C++, Java, or even Python, and use your programming skills. To get more private jobs, click here

InfyTQ Coding Round Pattern 2023

To pass any exam, applicants must have the following two things: A solid preparation strategy and the latest InfyTQ Coding syllabus. To achieve this, it is essential to know the syllabus in-depth and create a smart strategy for exam preparation. For better clarity on the upcoming InfyTQ Coding exams, applicants should refer to the Exam pattern & Syllabus

Topics Total Questions
Number of Questions 03-05
Time Limit Variable
Difficulty level High

Tech Mahindra Coding Questions with Answers

InfyTQ Coding Questions Python

Questions: Andy wants to go on a vacation to de-stress himself. Therefore he decides to take a trip to an island. It is given that he has as many consecutive days as possible to rest, but he can only make one trip to the island. Suppose that the days are numbered from 1 to N. Andy has M obligations in his schedule, which he has already undertaken and which correspond to some specific days. This means that ith obligation is scheduled for day Di. Andy is willing to cancel at most k of his obligations in order to take more holidays.

Your task is to find out the maximum days of vacation Andy can take by canceling at most K of his obligations.

Input Format

  • The first line contains an integer N, denoting the total number of days
  • The next line contains an integer M denoting the total number of obligations.
  • The next line contains an integer K denoting the largest number of obligations he could cancel
  • Each line i of the M subsequent lines (where 0<=i<=M) contains an integer describing Di.

Constraints

  • 1<=N<=10^6
  • 1<=M<=2*10^6
  • 1<=K<=2*10^6
  • 1<=D[i]<=10^6

Sample Input 1:

10
5
2
6
9
3
2
7

Sample Output 1 :

5

Explanation:

Here he could cancel his 3rd and 4th obligation which makes vacation length 5.

Sample input 2:

7
2
0
3
4

Sample Output 2:

3

Explanation:

Here he could not cancel any obligation since K=0, so the vacation length is 3.

EXAMPLE PROGRAM

PYTHON PROGRAM

n = int(input())
m = int(input())
k = int(input())
arr = [0] * n
for i in range(m):
   arr[i] = int(input())
ans = 0
arr.sort()
if k > 0:
   for i in range(k + 1, m + 3, 1):
       ans = max(ans, arr[i] - arr[i - k - 1] - 1)
else:
   j = 0
   while arr[j] == 0:
       j = j + 1
   count = 0
   for i in range(1, n + 1, 1):
       count += 1
       if j < n and (i == arr[j]):
           count = 0

           j += 1
       ans = max(count, ans)
print(ans)

Q) Khaled has an array A of N elements. It is guaranteed that N is even. He wants to choose at most N/2 elements from array A. It is not necessary to choose consecutive elements.  Khaled is interested in XOR of all the elements he chooses. Here, XOR denotes the bitwise XOR operation.

For example:

  • If A=[2,4,6,8], then khaled can choose the subset [2,4,8] to achieve XOR=(2 XOR 4 XOR 8)=14.

Khaled wants to maximize the XOR of all the elements he chooses. Your task is to help khaled to find the max XOR of a subset that he can achieve by choosing at most N/2 elements?

   Input format:

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

   Constraints 

  • 1<=N<=120
  • 1<=A[i]<=10^6

   Sample Input 1

2
1
2
   Sample Output 1
2

Explanation:

N=2,  A=[1,2] khaled can choose the subset[2]. The xor of the elements in the subset is 2. And the number of elements in the subset is 1 which is less than N/2.

Sample Input 2
4
1
2
4
7

Sample Output 2

7

Explanation:

N=4,  A=[1,2,4,7] Khaled can choose the subset [7]. The xor of the elements in the subset is 7, and the number of elements in the subset is 1 which is less than N/2.

EXAMPLE PROGRAMS

PYTHON PROGRAM

from itertools import combinations


def fun(arr, N):
   sub = []
   max_xor = max(arr)
   for i in range(1, N // 2):
       comb = combinations(arr, i + 1)
       for i in comb:
           sub.append(list(i))
   for a in sub:
       z = 0
       for b in a:
           z = z ^ b
       if z > max_xor:
           max_xor = z
   return max_xor


N = int(input("Enter Length : "))
arr = []
for i in range(N):
   arr.append(int(input(f"Enter {i+1} Element : ")))
if N < 3:
   print("Output :", max(arr))
else:
   print("Output :", fun(arr, N))

Q) Write a program to calculate and return the sum of absolute difference between the adjacent number in an array of positive integers from the position entered by the user.

You are expected to write code in the findTotalSum function only which receive three positional arguments:

1st: number of elements in the array
2nd: Array
3rd: position from where the sum is to be calculated

Example

Input
input 1 : 7
input 2 : 11 22 12 24 13 26 14
input 3 : 5

Output
25

Explanation

The first parameter 7 is the size of the array. Next is an array of integers and input 5 is the position from where you have to calculate the Total Sum. The output  is 25 as per calculation below.
| 26-13 | = 13
| 14-26 | =  12
Total Sum = 13 + 12 = 25

Here is the code:

def findTotalSum(n,numbers,pos):
    total = 0
    for i in range(pos-1,n-1):
        total+= abs(numbers[i]-numbers[i+1])
    return total
n = int(input())
numbers = list(map(int, input().split()))
pos = int(input())
print(findTotalSum(n,numbers,pos))

Q) Write a program to find the difference between the elements at odd index and even index.

Note : You are expected to write code in the findDifference function only which receive the first parameter as the numbers of items in the array and second parameter as the array itself. You are not required to take the input from the console.

Example

Finding the maximum difference between adjacent items of a list of 5 numbers

Input
input 1 : 7
input 2 : 10 20 30 40 50 60 70

Output
40

Explanation
The first parameter 7 is the size of the array. Sum of element at even index of array is 10 + 30 + 50 + 70 = 160 and sum of elements at odd index of array is 20 + 40 + 60 = 120. The difference between both is 40

Here is the code:

def findDifference(n,values):
    total = 0
    for i in range(n):
        if i%2 == 0:
            total+=values[i]
        else:
            total-=values[i]
    return total
n = int(input())
values = list(map(int, input().split()))
print(findDifference(n,values))

InfyTQ Coding Questions in Java

Questions: While playing an RPG game, you were assigned to complete one of the hardest quests in this game. There are monsters you’ll need to defeat in this quest. Each monster is described with two integer numbers – poweri and bonusi. To defeat this monster, you’ll need at least poweri experience points. If you try fighting this monster without having enough experience points, you lose immediately. You will also gain bonusi experience points if you defeat this monster. You can defeat monsters in any order.

The quest turned out to be very hard – you try to defeat the monsters but keep losing repeatedly. Your friend told you that this quest is impossible to complete. Knowing that, you’re interested, what is the maximum possible number of monsters you can defeat?

Input:

  • The first line contains an integer, n, denoting the number of monsters. The next line contains an integer, e, denoting your initial experience.
  • Each line i of the n subsequent lines (where 0 ≤ i < n) contains an integer, poweri, which represents power of the corresponding monster.
  • Each line i of the n subsequent lines (where 0 ≤ i < n) contains an integer, bonusi, which represents bonus for defeating the corresponding monster.

Output

  • 2

Example Programs:

JAVA PROGRAM

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

class Main
{
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int exp = s.nextInt();

        int monst[] = new int[n];
        int bonus[] = new int[n];

        for (int i = 0; i < n; i++) {
            monst[i] = s.nextInt();
        }
        for (int i = 0; i < n; i++) {
            bonus[i] = s.nextInt();
        }

        class Monster {
            private final int power, bonus;
            public Monster(int power, int bonus){
                this.power = power;
                this.bonus = bonus;
            }
        }

        Monster[] monsters = new Monster[n];

        for(int i = 0; i < n; i++)
            monsters[i] = new Monster(monst[i], bonus[i]);

        Arrays.sort(monsters, Comparator.comparingInt(m -> m.power));

        int count = 0;

        for(Monster m: monsters){
            if(exp < m.power) break;
            exp += m.bonus;
            ++count;
        }
        System.out.println(count);
    }
}

Questions: One of the first lessons IT students learn is the representation of natural numbers in the binary number system (base 2) This system uses only two digits, 0 and 1. In everyday life we use for convenience the decimal system (base 10) which uses ten digits, from 0 to 9. In general, we could use any numbering system.

Computer scientists often use systems based on 8 or 16. The numbering system based on K uses K digits with a value from 0 to K-1. Suppose a natural number M is given, written in the decimal system To convert it to the corresponding writing in the system based on K, we successively divide M by K until we reach a quotient that is less than K

The representation of M in the system based on K is formed by the final quotient (as first digit) and is followed by the remainder of the previous divisionsFor example :

  •  If M=122 and K=8, 122 in base 10= 172 in base 8 This means that the number
  • In decimal system = 172 in octal system.
  • 172 in base 8 = 1*8^2 + 7*8 + 2 = 122

You made the following observation in applying the above rule of converting natural numbers to another numbering system

  •  In some cases in the new representation all the digits of the number are the same. For example 63 in base 10= 333 in base 4

Given a number M in its decimal representation, your task is find the minimum base B such that in the representation of M at base B all digits are the same.

Input Format

  • The first line contains an integer, M, denoting the number given

Constraints

  • 1 <= M = 10^12

Sample Input 1 :

41

Sample Output 1 :

40

Explanation :

Here 41 in base 40. will be 11 so it has all digits the same, and there is no smaller base satisfying the requirements

Sample Input 2 :

34430

Sample Output 2 :

312

Explanation :

Here 34430 in base 312 will have all digits the same and there is no smaller base satisfying the requirements

Example Programs:

JAVA PROGRAM

import java.util.*;
class Main
{
  public static boolean convertBase (int m, int base)
  {

    int rem = m % base;
      m = m / base;

    while (m >= base && (m % base == rem))
      m = m / base;

    if (m == rem)
      return true;

      return false;
  }

  public static void main (String[]args)
  {
    Scanner sc = new Scanner (System.in);
    int m = sc.nextInt ();
    int base = 2;

    while (convertBase (m, base) != true)
      base++;

    System.out.println (base);

  }

}

Q) You have been given a string S of length N. The given string is a binary string that consists of only 0’s and ‘1’s. Ugliness of a string is defined as the decimal number that this binary string represents.

 Example:

  • “101” represents 5.
  • “0000” represents 0.
  • “01010” represents 10.

There are two types of operations that can be performed on the given string.

  • Swap any two characters by paying a cost of A coins.
  • Flip any character by paying a cost of B coins
  • flipping a character means converting a ‘1’to a ‘0’or converting a ‘0’ to a ‘1’.

Initially, you have been given coins equal to the value defined in CASH. Your task is to minimize the ugliness of the string by performing the above mentioned operations on it. Since the answer can be very large, return the answer modulo 10^9+7.

Note:

  • You can perform an operation only if you have enough number of coins to perform it.
  • After every operation the number of coins get deducted by the cost for that operation.

Input Format

  • The first line contains an integer, N, denoting the number of character in the string
  • The next line contains a string, S, denoting the the binary string
  • The next line contains an integer, CASH, denoting the total number of coins present initially
  • Next will contains an integer, A, denoting the cost to swap two characters.
  • Then the next line contains an integer, B, denoting the cost to flip a character.

Constraints

  • 1 <= N <= 10^5
  • 1< len(S)<= 10^5
  • 1<=CASH <=10^5
  • 1<=A<=10^5
  • 1<=B<=10^5

Sample Input 1 :

4
1111
7
1
2

  Sample Output 1 :

1

  Explanation:

3 flips can be used to create “0001” which represents 1.

  Sample Input 2:

6
111011
7
1
3

  Sample Output 2:

7

  Explanation:

First swap 0 with the most significant 1, then use flip twice first on index one and then on index two “111011”=>”0111111″=>”001111″=>”000111″ the value represented is 7.

  Sample Input 3:

6
111011
7
3
2

  Sample Output 3:

3

 Explanation:

Flip the 3 most significant characters to get “000011” : the value represented by this string is 3.N

EXAMPLE PROGRAMS

JAVA PROGRAM

import java.util.*;
class Main
{
  static String str;
  static int cash, n, a, b;
  static void swapf ()
  {
    char s[] = str.toCharArray ();
    int i = 0;
    for (int a = 0; a < s.length; a++)
      if (s[a] == '1')
	{
	  i = a;
	  break;
	}
    int j = s.length - 1;
    while (j > i)
      {
	if (cash < a)
	  break;
	if (s[j] == '0')
	  {
	    if (s[i] == '0')
	      i++;
	    else
	      {
		char temp = s[i];
		s[i] = s[j];
		s[j] = temp;
		cash -= a;
		j--;
	      }
	  }
	else
	  j--;
      }
    str = new String (s);
  }
  static void flipf ()
  {
    char s[] = str.toCharArray ();
    int i = 0;

    for (int a = 0; a < s.length; a++)
      if (s[a] == '1')
	{
	  i = a;
	  break;
	}
    while (cash >= b)
      {
	if (i == s.length)
	  break;
	if (s[i] == '1')
	  {
	    s[i] = '0';
	    i++;
	    cash -= b;
	  }
      }
    str = new String (s);
  }

  public static void main (String[]args)
  {
    Scanner sc = new Scanner (System.in);
    n = sc.nextInt ();
    str = sc.next ();
    cash = sc.nextInt ();
    a = sc.nextInt ();
    b = sc.nextInt ();

    if (a < b)
      {
	swapf ();
	flipf ();
      }
    else
      {
	flipf ();
	swapf ();
      }
    System.out.println (Integer.parseInt (str, 2));
  }
}

InfyTQ Coding Questions in C

Q) Here are some Tech Mahindra Coding Questions from 2020 which are solved in C: Build a program for calculating and returning the sums of absolute differences between adjacent numbers in arrays of positive integers. This must be calculated from the position determined by the current user.

In the case of this coding problem, you use three positional arguments through a findTotalSum function. The three inputs you would require are the number of elements inside the array, the elements in the array and the position from where this function will take place.

For example, suppose the total number of elements is 5 and these are the elements:

1 2 3 6 4

Then, if we decide to start from the 3rd position or enter 3 as input, the function will occur from ‘3’, the 3rd number in the array.

Hence, the sum would be a total of (6-3)+(4-6)= 5

Here is the code:

#include <stdio.h>
#include <stdlib.h>
int findTotalSum(int n, int arr[], int start)
{
    int difference, sum=0;
    for(int i=start-1; i<n-1; i++)
    {
        difference = abs(arr[i]-arr[i+1]);
        sum = sum + difference;
    }
    return sum;
}
int main()
{
    int n;
    int start;
    scanf("%d",&n);
    int array[n];
    for(int i=0; i<n; i++)
    {
        scanf("%d",&array[i]);
    }
    scanf("%d",&start);
    int result = findTotalSum(n, array, start);
    printf("\n%d",result);
    return 0;
}

Q) Build a program that will allow you to find out how many clothing pieces in total of a certain length can be extracted from a particular number of cloth pieces. We can take the required length for each clothing piece as 10 feet.

In this case, we must first decide upon the length unit as feet and determine the inputs we need. For this function, we will need two inputs, first the number of pieces ( in the array) and the size of each piece in feet inside the array.

A cloth merchant has some pieces of cloth of different lengths. He has an order of curtains of length 12 feet. He has to find how many curtains can be made from these pieces. Length of pieces of cloth is recorded in feet.

For example, suppose the total number of elements is 3 and these are the elements:

0 10 40

Then, the first input is 3 followed by the second input of 0, 10 and 40.

Hence, the sum would be a total of 0 + (10/10) + (40/10) = 5

Thus, there could be 5 pieces of clothing extracted from these 3 pieces of cloth of variable sizes.

Here is the code:

#include <stdio.h>
int findTotalPieces(int n, int arr[])
{
    int feet, total = 0;
    for(int i=0; i<n; i++)
    {
        feet = arr[i] / 10;
        total = total + feet;    
    }
    return total;
}
int main()
{
    int n;
    scanf("%d",&n);
    int array[n];
    for(int i=0; i<n; i++)
    {
        scanf("%d",&array[i]);
    }
    int result = findTotalPieces(n, array);
    printf("%d",result);
    return 0;
}

Q) A Cloth merchant has some pieces of cloth of different lengths. He has an order of curtains of length of 12 feet. He has to find how many curtains can  be made from these pieces. Length of pieces of cloth is recorded in feet.

Note : You are expected to write code in the findTotalCurtains function only which receive the first parameter as the number of items in the array and second parameter as the array itself. You are not required to take the input from the console.

Example

Finding the total curtains from a list of 5 cloth pieces.

Input
input 1 : 5
input 2 : 3 42 60 6 14

Output
9

Explanation
The first parameter 5 is the size of the array. Next is an array of measurements in feet. The total number of curtains is 5 which is calculated as under

3 -> 0
42 -> 3
60 -> 5
6 -> 0
14 -> 1
total = 9

Here is the code:

#include <stdio.h>
int findTotalCurtains(int n, int arr[])
{
    int feet, total = 0;
    for(int i=0; i<n; i++)
    {
        feet = arr[i] / 12;
        total = total + feet;    
    }
    return total;
}
int main()
{
    int n;
    scanf("%d",&n);
    int array[n];
    for(int i=0; i<n; i++)
    {
        scanf("%d",&array[i]);
    }
    int result = findTotalCurtains(n, array);
    printf("%d",result);
    return 0;
}

Q) Write a program to find the difference between the elements at odd index and even index.

Note : You are expected to write code in the findDifference function only which receive the first parameter as the numbers of items in the array and second parameter as the array itself. You are not required to take the input from the console.

Example

Finding the maximum difference between adjacent items of a list of 5 numbers

Input
input 1 : 7
input 2 : 10 20 30 40 50 60 70

Output
40

Explanation
The first parameter 7 is the size of the array. Sum of element at even index of array is 10 + 30 + 50 + 70 = 160 and sum of elements at odd index of array is 20 + 40 + 60 = 120. The difference between both is 40

Here is the code:

#include <stdio.h>
int findDifference(int n, int arr[])
{
    int odd=0, even=0;
    for(int i=0; i<n; i++)
    {
        if(i%2==0)
        {
            even = even + arr[i];
        }
        else
        {
            odd = odd + arr[i];
        }  
    }
    return even-odd;
}
int main()
{
    int n;
    scanf("%d",&n);
    int array[n];
    for(int i=0; i<n; i++)
    {
        scanf("%d",&array[i]);
    }
    int result = findDifference(n, array);
    printf("%d",result);
    return 0;
}

Q) Write a program to calculate and return the sum of absolute difference between the adjacent number in an array of positive integers from the position entered by the user.

Note : You are expected to write code in the findTotalSum function only which receive three positional arguments:

1st : number of elements in the array
2nd : array
3rd : position from where the sum is to be calculated

Example

Input
input 1 : 7
input 2 : 11 22 12 24 13 26 14
input 3 : 5

Output
25

Explanation

The first parameter 7 is the size of the array. Next is an array of integers and input 5 is the position from where you have to calculate the Total Sum. The output  is 25 as per calculation below.
| 26-13 | = 13
| 14-26 | =  12
Total Sum = 13 + 12 = 25

Here is the code:

#include <stdio.h>
#include <stdlib.h>
int findTotalSum(int n, int arr[], int start)
{
    int difference, sum=0;
    for(int i=start-1; i<n-1; i++)
    {
        difference = abs(arr[i]-arr[i+1]);
        sum = sum + difference;
    }
    return sum;
}
int main()
{
    int n;
    int start;
    scanf("%d",&n);
    int array[n];
    for(int i=0; i<n; i++)
    {
        scanf("%d",&array[i]);
    }
    scanf("%d",&start);
    int result = findTotalSum(n, array, start);
    printf("\n%d",result);
    return 0;
}

InfyTQ Practice Coding Questions and Solutions

Question: Biggest Meatball

Problem Statement – Bhojon is a restaurant company and has started a new wing in a city. They have every type of cook except the meatball artist. They had fired their last cook because the sale of meatballs in their restaurant is really great, and they can’t afford to make meatballs again and again every time their stock gets empty. They have arranged a hiring program, where you can apply with their meatball.
They will add the meatball in their seekh (a queue) and everytime they cut the meatball they take it and cut it on the day’s quantity and then re-add the meatball in the seekh. You are the hiring manager there and you are going to say who is gonna be hired.

Day’s quantity means, on that very day the company sells only that kg of meatballs to every packet.

If someone has less than a day’s quantity, it will be counted as a sell.

Function Description:

  • Complete the function with the following parameters:

Parameters:

Name Type Description
N Integer How many people are participating in the hiring process.
D Integer Day’s quantity, how many grams of meatball is being sold    to every packet.
Array[ ] Integer array Array of integers, the weight of meatballs everyone came with.

Return:

  • The ith person whose meat is served at last.

Constraints:

  • 1 <= N <= 10^3
  • 1 <= D <= 10^3
  • 1 <= Array[i] <= 10^3

Input Format:

  • First line contains N.
  • Second line contains D.
  • After that N lines contain The ith person’s meatball weight.

Output Format: The 1 based index of the man whose meatball is served at the last.

Sample Input 1:

4

2

[7 8 9 3]

Sample Output 1:

3

Explanation:

The seekh or meatball queue has [7 8 9 3] this distribution. At the first serving they will cut 2 kgs of meatball from the first meatball and add it to the last of the seekh, so after 1st time it is:

[8 9 3 5]

Then, it is: [9 3 5 6],  [3 5 6 7], [5 6 7 1], [6 7 1 3], [7 1 3 4], [1 3 4 5], [3 4 5], [4 5 1], [5 1 2], [1 2 3], [2 3], [3], [1], [0]

So the last served meatball belongs to the 3rd person.

C++ PROGRAM

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,x,a,m=0,maxPos=0;
    cin>>n;
    vector v(n);
    cin>>x;
    
    for(int i=0;i<n;i++){ cin>>v[i];
    }
    
    for(int i=0;i<n;i++){
        v[i]=(v[i]-1)/x;
        
    }
 
    for(int i=0;i<n;i++){ if(v[i]>=m){
            m=v[i];
            maxPos=i;
        }
    }
    
    cout<<maxPos+1;
}

JAVA PROGRAM

import java.util.*;
class Main
{ 
  public static void main(String args[])
  {
    Scanner sc = new Scanner (System.in);
    
    int n=sc.nextInt();
    int v[]= new int[n];
    int x=sc.nextInt();
   
    for(int i=0; i<n; i++)
       v[i]=sc.nextInt();
    
    for(int i=0;i<n;i++){
        v[i]=(v[i]-1)/x;
        
    }
    
    int m=0, maxPos=0;
    
    for(int i=0;i<n;i++){ if(v[i]>=m){
            m=v[i];
            maxPos=i;
        }
    }
    
    System.out.println(maxPos+1);
  }
 }

Question: Self Sufficient

Problem Statement: Abhijeet is one of those students who tries to get his own money by part time jobs in various places to fill up the expenses for buying books. He is not placed in one place, so what he does, he tries to allocate how much the book he needs will cost, and then work to earn that much money only. He works and then buys the book respectively. Sometimes he gets more money than he needs so the money is saved for the next book. Sometimes he doesn’t. In that time, if he has stored money from previous books, he can afford it, otherwise he needs money from his parents.

Now His parents go to work and he can’t contact them amid a day. You are his friend, and you have to find how much money minimum he can borrow from his parents so that he can buy all the books.

He can Buy the book in any order.

Function Description:

Complete the function with the following parameters:

Name Type Description
N Integer How many Books he has to buy that day.
EarnArray[ ] Integer array Array of his earnings for the ith book
CostArray[ ] Integer array Array of the actual cost of the ith book.

Constraints:

  • 1 <= N <= 10^3
  • 1 <= EarnArray[i] <= 10^3
  • 1 <=  CostArray[i] <= 10^3

Input Format:

  • First line contains N.
  • Second N lines contain The ith earning for the ith book.
  • After that N lines contain The cost of the ith book.

Output Format: The minimum money he needs to cover the total expense.

Sample Input 1:

3

[3 4 2]

[5 3 4]

Sample Output 1:

3

Explanation:

At first he buys the 2nd book, which costs 3 rupees, so he saves 1 rupee. Then he buys the 1st book, that takes 2 rupees more. So he spends his stored 1 rupee and hence he needs 1 rupee more. Then he buys the last book.

PYTHON PROGRAM:

n=int(input())
L1=[0] *n
L2=[0]*n
for i in range(n):
    L2[i]=int(input())
for i in range(n):
    L1[i]=int(input())
    
for i in range(n):
    L2[i]=L2[i]-L1[i];

L2.sort()
su=0
ans=0
for i in range(n):
    su=su+L2[i]
    if su<0:
        ans = ans + abs(su)
        su=0
        
print(ans)

Question: Parallel Columbus

Problem Statement: Nobel Prize-winning Austrian-Irish physicist Erwin Schrödinger developed a machine and brought as many Christopher Columbus from as many parallel universes he could. Actually he was quite amused by the fact that Columbus tried to find India and got America. He planned to dig it further.

Though totally for research purposes, he made a grid of size n X m, and planted some people of America in a position (x,y) [in 1 based indexing of the grid], and then planted you with some of your friends in the (n,m) position of the grid. Now he gathered all the Columbus in 1,1 positions and started a race.

Given the values for n, m, x, y, you have to tell how many different Columbus(s) together will explore you as India for the first time.

Remember, the Columbus who will reach to the people of America, will be thinking that as India and hence wont come further.

Function Description:

Complete the markgame function in the editor below. It has the following parameter(s):

Parameters:

Name Type Description
n Integer The number of rows in the grid.
m Integer The number of columns in the grid.
x Integer The American cell’s Row.
y Integer The American cell’s Column.

Constraints:

  • 1 <= n <= 10^2
  • 1 <= m <= 10^2
  • 1 <= x <= n
  • 1 <= y <= m

Input Format:

  • The first line contains an integer, n, denoting the number of rows in the grid.
  • The next line contains an integer m, denoting the number of columns in the grid.
  • The next line contains an integer, x, denoting the American cell’s row.
  • The next line contains an integer, y, denoting the American cell’s column.

Sample Cases

Sample Input 1

2

2

2

1

Sample Output 1

1

Explanation

The only way possible is (1,1) ->(2,1) -> (2,2), so the answer is 1.

PYTHON PROGRAM

import math
n=int(input())-1
m=int(input())-1
x=int(input())-1
y=int(input())-1

ans=math.factorial(n+m)
ans=ans//(math.factorial(n))
ans=ans//(math.factorial(m))

ans1=math.factorial(x+y)
ans1=ans1//(math.factorial(x))
ans1=ans1//(math.factorial(y))

x1=n-x
y1=m-y

ans2=math.factorial(x1+y1)
ans2=ans2//(math.factorial(x1))
ans2=ans2//(math.factorial(y1))
print(ans-(ans1*ans2))

***BEST OFF LUCK***

We hope our article is informative. To stay ahead of the ever-increasing competition, you are strongly encouraged to download the previous year’s papers and start practicing. By solving these papers you will increase your speed and accuracy. For more information check Naukrimessenger.com website for exam patterns, syllabi, Results, cut-off marks, answer keys, best books, and more to help you crack your exam preparation. You can also take advantage of amazing Job offers to improve your preparation volume by joining in Telegram Channel page!!!