Tech Mahindra Coding Questions 2024
About Tech Mahindra :
Tech Mahindra is an Indian multinational technology company that provides information technology (IT) services and business process outsourcing (BPO) solutions. Established in 1986, it is a part of the Mahindra Group, a prominent conglomerate in India. Tech Mahindra operates in various industries, including telecommunications, manufacturing, healthcare, finance, and more. The company specializes in offering services such as software development, system integration, network services, cloud computing, and digital transformation. Tech Mahindra has a global presence, with offices and delivery centers in numerous countries, serving clients across the world. It has gained recognition for its focus on innovation, customer-centric approach, and expertise in emerging technologies. [Tech Mahindra Coding Questions 2024]
Tech Mahindra Technical Round
- There will be 26 Technical Test Question with in the Tech Mahindra exam, two question will be the Coding Question and rest all question will be Technical MCQ along with there will be 72 Personality Test Questions are also present in the Tech Mahindra Technical Test.
- The difficulty level of question is medium to high where they will judge your knowledge of programming although overall time to solve the whole Tech Test Question is 90 minutes including 72 Personality Test Questions. so, you can give approximately 15-20 minutes to coding question in the exam. [Tech Mahindra Coding Questions 2024]
Technical Round
Technical Round | Total Questions | Total Time |
---|---|---|
Computer Programming | 12 | 15 mins |
Computer Science | 12 | 15 mins |
Automata Fix [Coding Questions] | 2 | 45 mins |
Personality Test | 72 | 15 mins |
Total Sections : 4 | 98 Questions | 90 mins |
Tech Mahindra Coding Questions 2024​
Question 1 : Count Non-repeated Character in String
A data compression software utilizes various steps to compress a string of data. One of the steps involves finding the count of characters that are not repeated in the string.
Write an algorithm for the software developer to find the count of characters that are not repeated in the string.
Input Format
The input consists of a string.
compString representing the string to be compressed.
Output
Print an integer representing the count of characters that are not repeated in the string. If no such character is found or the input string is empty then print 0.
Note
The input string compString is case sensitive. Uppercase characters and lowercase characters are counted as different. The input string compString consists of alphanumeric and special characters only.
Example
Input:Â Â alphaadida
Output:Â 4
Explanation :Â ** Non repeated characters are l, p ,h ,i **
[Tech Mahindra Coding Questions 2024]
Solution :
import java.util.HashMap;
import java.util.Map;
public class CompressionAlgorithm {
public static void main(String[] args) {
String compString = “alphaadida”;
int result = countNonRepeatedCharacters(compString);
System.out.println(result);
}
public static int countNonRepeatedCharacters(String compString) {
if (compString == null || compString.isEmpty()) {
return 0;
}
Map<Character, Integer> charFrequency = new HashMap<>();
// Count frequency of each character in the string
for (char c : compString.toCharArray()) {
charFrequency.put(c, charFrequency.getOrDefault(c, 0) + 1);
}
// Count characters with frequency 1
int count = 0;
for (Map.Entry<Character, Integer> entry : charFrequency.entrySet()) {
if (entry.getValue() == 1) {
count++;
}
}
return count;
}
}
#include <iostream>
#include <unordered_map>
int countNonRepeatedCharacters(const std::string& compString) {
if (compString.empty()) {
return 0;
}
std::unordered_map<char, int> charFrequency;
// Count frequency of each character in the string
for (char c : compString) {
charFrequency[c]++;
}
// Count characters with frequency 1
int count = 0;
for (const auto& entry : charFrequency) {
if (entry.second == 1) {
count++;
}
}
return count;
}
int main() {
std::string compString = “alphaadida”;
int result = countNonRepeatedCharacters(compString);
std::cout << result << std::endl;
return 0;
}
def count_non_repeated_characters(comp_string):
if not comp_string:
return 0
char_frequency = {}
# Count frequency of each character in the string
for char in comp_string:
char_frequency[char] = char_frequency.get(char, 0) + 1
# Count characters with frequency 1
count = 0
for char, frequency in char_frequency.items():
if frequency == 1:
count += 1
return count
# Example usage
comp_string = “alphaadida”
result = count_non_repeated_characters(comp_string)
print(result)
Question 2 : Difference Between the Count of Odd numbers and Even numbers
Write a program to return the difference between the count of odd numbers and even numbers.
Note : You are expected to write code in the countOddEvenDifference function only which will 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 input from the console.
Example
Finding the difference between the count of odd and even numbers from a list of 8Â number
Input1 :Â 8
Input2 :Â 10 20 30 40 55 66 77 83
Output: -2
Explanation :
The first paramter (8) is the szie of the array. Next is an array of integers. The calculation of difference between count sum of odd and even numbers is as follows:
3 (count of odd numbers) – 5 (count of even numbers) = -2  [Tech Mahindra Coding Questions 2024]
Solution :
public class CountOddEvenDifference {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 55, 66, 77, 83};
int size = 8;
int result = countOddEvenDifference(size, numbers);
System.out.println(result);
}
public static int countOddEvenDifference(int size, int[] arr) {
int oddCount = 0;
int evenCount = 0;
for (int i = 0; i < size; i++) {
if (arr[i] % 2 == 0) {
evenCount++;
} else {
oddCount++;
}
}
return oddCount – evenCount;
}
}
#include <iostream>
int count_odd_even_difference(int size, int arr[]) {
int odd_count = 0;
int even_count = 0;
for (int i = 0; i < size; i++) {
if (arr[i] % 2 == 0) {
even_count++;
} else {
odd_count++;
}
}
return odd_count – even_count;
}
int main() {
int size = 8;
int numbers[] = {10, 20, 30, 40, 55, 66, 77, 83};
int result = count_odd_even_difference(size, numbers);
std::cout << result << std::endl;
return 0;
}
def count_odd_even_difference(size, arr):
odd_count = 0
even_count = 0
for num in arr:
if num % 2 == 0:
even_count += 1
else:
odd_count += 1
return odd_count – even_count
# Example usage
size = 8
numbers = [10, 20, 30, 40, 55, 66, 77, 83]
result = count_odd_even_difference(size, numbers)
print(result)
Question 3 : Write a program to calculate and return the sum of absolute difference between the adjacent number in an array
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Â Â [Tech Mahindra Coding Questions 2024]
Solution :
import java.util.Scanner;
public class Main {
public static int findTotalSum(int n, int[] arr, int position) {
int totalSum = 0;
for (int i = position; i < n – 1; ++i) {
int diff = Math.abs(arr[i] – arr[i + 1]);
totalSum += diff;
}
return totalSum;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print(“Enter the number of elements in the array: “);
int n = scanner.nextInt();
int[] arr = new int[n];
System.out.print(“Enter the array elements separated by space: “);
for (int i = 0; i < n; ++i) {
arr[i] = scanner.nextInt();
}
System.out.print(“Enter the position from where to calculate the total sum: “);
int position = scanner.nextInt();
int result = findTotalSum(n, arr, position);
System.out.println(“Output: ” + result);
}
}
#include <iostream>
#include <cmath>
using namespace std;
int findTotalSum(int n, int arr[], int position) {
int total_sum = 0;
for (int i = position; i < n – 1; ++i) {
int diff = abs(arr[i] – arr[i + 1]);
total_sum += diff;
}
return total_sum;
}
int main() {
int n, position;
cout << “Enter the number of elements in the array: “;
cin >> n;
int arr[n];
cout << “Enter the array elements separated by space: “;
for (int i = 0; i < n; ++i) {
cin >> arr[i];
}
cout << “Enter the position from where to calculate the total sum: “;
cin >> position;
int result = findTotalSum(n, arr, position);
cout << “Output: ” << result << endl;
return 0;
}
def findTotalSum(n, arr, position):
total_sum = 0
for i in range(position, n-1):
diff = abs(arr[i] – arr[i+1])
total_sum += diff
return total_sum
# Example usage
if __name__ == “__main__”:
n = int(input(“Enter the number of elements in the array: “))
arr = list(map(int, input(“Enter the array elements separated by space: “).split()))
position = int(input(“Enter the position from where to calculate the total sum: “))
result = findTotalSum(n, arr, position)
print(“Output:”, result)
Question 4 : Write a program to find the difference between the elements at odd index and even index.
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 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 number
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Â [Tech Mahindra Coding Questions 2024]
Solution :
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}
System.out.println(findDifference(n, arr));
}
static int findDifference(int n, int[] arr) {
int evenSum = 0, oddSum = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
evenSum += arr[i];
} else {
oddSum += arr[i];
}
}
return Math.abs(evenSum – oddSum);
}
}
#include <iostream>
using namespace std;
int findDifference(int n, int arr[]) {
int evenSum = 0, oddSum = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
evenSum += arr[i];
} else {
oddSum += arr[i];
}
}
return abs(evenSum – oddSum);
}
int main() {
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
cout << findDifference(n, arr) << endl;
return 0;
}
def find_difference(n, arr):
even_sum = sum(arr[i] for i in range(0, n, 2))
odd_sum = sum(arr[i] for i in range(1, n, 2))
return abs(even_sum – odd_sum)
# Example usage
n = 7
arr = [10, 20, 30, 40, 50, 60, 70]
result = find_difference(n, arr)
print(result)
Question 5 : Write a program for Decimal to Binary Conversion
Solution :
import
java.io.*;
Â
 class
GFGÂ
{
   Â
// function to convert decimal to binary
   Â
static
void
decToBinary(
int
n)
   Â
{
       Â
// array to store binary number
       Â
int
[] binaryNum =
new
int
[
1000
];
 Â
        Â
// counter for binary array
       Â
int
i =
0
;
       Â
while
(n >
0
)Â
       Â
{
           Â
// storing remainder in binary array
           Â
binaryNum[i] = n %
2
;
           Â
n = n /
2
;
           Â
i++;
       Â
}
 Â
        Â
// printing binary array in reverse order
       Â
for
(
int
j = i -
1
; j >=
0
; j--)
           Â
System.out.print(binaryNum[j]);
   Â
}
    Â
    Â
// driver program
   Â
public
static
void
main (String[] args)Â
   Â
{
       Â
int
n =
17
;
         Â
System.out.println(
"Decimal - "
+ n);
       Â
System.out.print(
"Binary - "
);
         Â
decToBinary(n);
   Â
}
}
#include <iostream>
// Function to convert decimal to binary
std::string decimalToBinary(int decimalNumber) {
std::string binaryNumber = “”;
while (decimalNumber > 0) {
// Append the remainder to the front of the binary representation
binaryNumber = char(‘0’ + decimalNumber % 2) + binaryNumber;
// Divide the decimal number by 2 for the next iteration
decimalNumber /= 2;
}
return binaryNumber.empty() ? “0” : binaryNumber;
}
int main() {
// Example usage
int input1 = 17;
int input2 = 33;
std::string output1 = decimalToBinary(input1);
std::string output2 = decimalToBinary(input2);
// Output the results
std::cout << “Output1: ” << output1 << std::endl;
std::cout << “Output2: ” << output2 << std::endl;
return 0;
}
def decimal_to_binary(decimal_number):
binary_number = bin(decimal_number).replace(“0b”, “”)
return binary_number
# Example usage
input_1 = 17
input_2 = 33
output_1 = decimal_to_binary(input_1)
output_2 = decimal_to_binary(input_2)
print(“Output1:”, output_1)
print(“Output2:”, output_2)
Question 6 : Find a pair with maximum product in array of Integers
Solution :
public class HighestProductPair {
static class Pair {
int first, second;
Pair(int a, int b) {
first = a;
second = b;
}
}
// Function to find pair with highest product
static Pair findPairWithHighestProduct(int[] arr) {
int n = arr.length;
if (n < 2) {
System.out.println(“Array should have at least two elements”);
return null;
}
int maxPositive1 = Integer.MIN_VALUE, maxPositive2 = Integer.MIN_VALUE;
int minNegative1 = Integer.MAX_VALUE, minNegative2 = Integer.MAX_VALUE;
for (int i = 0; i < n; i++) {
if (arr[i] > maxPositive1) {
maxPositive2 = maxPositive1;
maxPositive1 = arr[i];
} else if (arr[i] > maxPositive2) {
maxPositive2 = arr[i];
}
if (arr[i] < 0 && Math.abs(arr[i]) < Math.abs(minNegative1)) {
minNegative2 = minNegative1;
minNegative1 = arr[i];
} else if (arr[i] < 0 && Math.abs(arr[i]) < Math.abs(minNegative2)) {
minNegative2 = arr[i];
}
}
// Check for the highest product
int product1 = maxPositive1 * maxPositive2;
int product2 = minNegative1 * minNegative2;
return (product1 > product2) ? new Pair(maxPositive1, maxPositive2) : new Pair(minNegative1, minNegative2);
}
public static void main(String[] args) {
int[] arr1 = {1, 4, 3, 6, 7, 0};
int[] arr2 = {-1, -3, -4, 2, 0, -5};
Pair result1 = findPairWithHighestProduct(arr1);
Pair result2 = findPairWithHighestProduct(arr2);
System.out.println(“Output1: {” + result1.first + “, ” + result1.second + “}”);
System.out.println(“Output2: {” + result2.first + “, ” + result2.second + “}”);
}
}
#include <iostream>
#include <climits>
using namespace std;
struct Pair {
int first, second;
Pair(int a, int b) {
first = a;
second = b;
}
};
// Function to find pair with highest product
Pair findPairWithHighestProduct(int arr[], int n) {
if (n < 2) {
cout << “Array should have at least two elements” << endl;
return Pair(0, 0);
}
int maxPositive1 = INT_MIN, maxPositive2 = INT_MIN;
int minNegative1 = INT_MAX, minNegative2 = INT_MAX;
for (int i = 0; i < n; i++) {
if (arr[i] > maxPositive1) {
maxPositive2 = maxPositive1;
maxPositive1 = arr[i];
} else if (arr[i] > maxPositive2) {
maxPositive2 = arr[i];
}
if (arr[i] < 0 && abs(arr[i]) < abs(minNegative1)) {
minNegative2 = minNegative1;
minNegative1 = arr[i];
} else if (arr[i] < 0 && abs(arr[i]) < abs(minNegative2)) {
minNegative2 = arr[i];
}
}
// Check for the highest product
int product1 = maxPositive1 * maxPositive2;
int product2 = minNegative1 * minNegative2;
return (product1 > product2) ? Pair(maxPositive1, maxPositive2) : Pair(minNegative1, minNegative2);
}
int main() {
int arr1[] = {1, 4, 3, 6, 7, 0};
int arr2[] = {-1, -3, -4, 2, 0, -5};
int n1 = sizeof(arr1) / sizeof(arr1[0]);
int n2 = sizeof(arr2) / sizeof(arr2[0]);
Pair result1 = findPairWithHighestProduct(arr1, n1);
Pair result2 = findPairWithHighestProduct(arr2, n2);
cout << “Output1: {” << result1.first << “, ” << result1.second << “}” << endl;
cout << “Output2: {” << result2.first << “, ” << result2.second << “}” << endl;
return 0;
}
def find_pair_with_highest_product(arr):
n = len(arr)
if n < 2:
print(“Array should have at least two elements”)
return None
max_positive1, max_positive2 = float(‘-inf’), float(‘-inf’)
min_negative1, min_negative2 = float(‘inf’), float(‘inf’)
for num in arr:
if num > max_positive1:
max_positive2 = max_positive1
max_positive1 = num
elif num > max_positive2:
max_positive2 = num
if num < 0 and abs(num) < abs(min_negative1):
min_negative2 = min_negative1
min_negative1 = num
elif num < 0 and abs(num) < abs(min_negative2):
min_negative2 = num
# Check for the highest product
product1 = max_positive1 * max_positive2
product2 = min_negative1 * min_negative2
return (max_positive1, max_positive2) if product1 > product2 else (min_negative1, min_negative2)
# Example usage
arr1 = [1, 4, 3, 6, 7, 0]
arr2 = [-1, -3, -4, 2, 0, -5]
result1 = find_pair_with_highest_product(arr1)
result2 = find_pair_with_highest_product(arr2)
print(“Output1:”, result1)
print(“Output2:”, result2)
Question 7 : Array Sub Array
You are given an array, You have to choose a contiguous subarray of length ‘k’, and find the minimum of that segment, return the maximum of those minimums.
Input:Â Â
1 → Length of segment x =1
5 → size of space n = 5
1 → space = [ 1,2,3,1,2]
2
3
1
2
Output:
3
Explanation :
The subarrays of size x = 1 are [1],[2],[3],[1], and [2],Because each subarray only contains 1 element, each value is minimal with respect to the subarray it is in. The maximum of these values is 3. Therefore, the answer is 3.  [Tech Mahindra Coding Questions 2024]
Solution :
import java.util.ArrayDeque;
import java.util.Deque;
public class MaximumOfMinimums {
// Function to find the maximum of minimums in contiguous subarrays of length ‘k’
static int maxOfMinInSubarrays(int[] arr, int n, int k) {
Deque<Integer> dq = new ArrayDeque<>();
int result = Integer.MIN_VALUE;
for (int i = 0; i < n; i++) {
// Remove elements outside the current window of size ‘k’
while (!dq.isEmpty() && dq.peek() < i – k + 1) {
dq.poll();
}
// Remove elements that are less than the current element from the back of the deque
while (!dq.isEmpty() && arr[dq.peekLast()] > arr[i]) {
dq.pollLast();
}
// Add the current element’s index to the deque
dq.offer(i);
// Check if the window size ‘k’ is reached, and update the result
if (i >= k – 1) {
result = Math.max(result, arr[dq.peek()]);
}
}
return result;
}
public static void main(String[] args) {
int[] arr = {1, 2, 3, 1, 2};
int k = 1;
int result = maxOfMinInSubarrays(arr, arr.length, k);
System.out.println(“Output: ” + result);
}
}
#include <iostream>
#include <deque>
using namespace std;
// Function to find the maximum of minimums in contiguous subarrays of length ‘k’
int maxOfMinInSubarrays(int arr[], int n, int k) {
deque<int> dq;
int result = INT_MIN;
for (int i = 0; i < n; i++) {
// Remove elements outside the current window of size ‘k’
while (!dq.empty() && dq.front() < i – k + 1) {
dq.pop_front();
}
// Remove elements that are less than the current element from the back of the deque
while (!dq.empty() && arr[dq.back()] > arr[i]) {
dq.pop_back();
}
// Add the current element’s index to the deque
dq.push_back(i);
// Check if the window size ‘k’ is reached, and update the result
if (i >= k – 1) {
result = max(result, arr[dq.front()]);
}
}
return result;
}
int main() {
int arr[] = {1, 2, 3, 1, 2};
int k = 1;
int result = maxOfMinInSubarrays(arr, sizeof(arr) / sizeof(arr[0]), k);
cout << “Output: ” << result << endl;
return 0;
}
from collections import deque
# Function to find the maximum of minimums in contiguous subarrays of length ‘k’
def max_of_min_in_subarrays(arr, n, k):
dq = deque()
result = float(‘-inf’)
for i in range(n):
# Remove elements outside the current window of size ‘k’
while dq and dq[0] < i – k + 1:
dq.popleft()
# Remove elements that are less than the current element from the back of the deque
while dq and arr[dq[-1]] > arr[i]:
dq.pop()
# Add the current element’s index to the deque
dq.append(i)
# Check if the window size ‘k’ is reached, and update the result
if i >= k – 1:
result = max(result, arr[dq[0]])
return result
# Example usage
arr = [1, 2, 3, 1, 2]
k = 1
result = max_of_min_in_subarrays(arr, len(arr), k)
print(“Output:”, result)
Question 8 : Find Total Feet
Given an array of integers representing measurements in inches, write a program to calculate the total of measurements in feet. Ignore the measurements that are less than a foot (eg. 10).
Note: You are expected to write code in the findTotalFeet function only which will receive the first parameter as the number of items in the array and the second parameter as the array itself. You are not required to take input from the console
Example:
Finding the total measurements in feet from a list of 5 numbers
Input:Â Â
5
18 11 27 12 14
Output:
5
Explanation :
The first parameter (5) is the size of the array. Next is an array of measurements in inches. The total number of feet is 5 which is calculated as shown below:
18->1
11->0
27->2
12->1
14->1
 [Tech Mahindra Coding Questions 2024]
Solution :
public class TotalMeasurements {
// Function to calculate the total measurements in feet
static int findTotalFeet(int n, int[] measurements) {
int totalFeet = 0;
for (int i = 0; i < n; i++) {
if (measurements[i] >= 12) {
totalFeet += measurements[i] / 12;
}
}
return totalFeet;
}
public static void main(String[] args) {
int[] measurements = {18, 11, 27, 12, 14};
int n = measurements.length;
int result = findTotalFeet(n, measurements);
System.out.println(“Output: ” + result);
}
}
#include <iostream>
using namespace std;
// Function to calculate the total measurements in feet
int findTotalFeet(int n, int measurements[]) {
int totalFeet = 0;
for (int i = 0; i < n; i++) {
if (measurements[i] >= 12) {
totalFeet += measurements[i] / 12;
}
}
return totalFeet;
}
int main() {
int measurements[] = {18, 11, 27, 12, 14};
int n = sizeof(measurements) / sizeof(measurements[0]);
int result = findTotalFeet(n, measurements);
cout << “Output: ” << result << endl;
return 0;
}
# Function to calculate the total measurements in feet
def findTotalFeet(n, measurements):
totalFeet = 0
for measurement in measurements:
if measurement >= 12:
totalFeet += measurement // 12
return totalFeet
# Example usage
measurements = [18, 11, 27, 12, 14]
n = len(measurements)
result = findTotalFeet(n, measurements)
print(“Output:”, result)
Question 9 : Calculate Total Tax
Write a program to calculate the total bill tax amount for a list of billing amounts passed as an array of long integers.
Up to the amount of 1000, there is no tax applicable, subsequently, a flat tax of 10% is applicable for the remaining amount as per the tax rate.
Note:Â
All calculations and results should be integer-based ignoring fractions
You are expected to write code int the calcTotalTax function only which will receive the first parameter as the number of items in the array and the second parameter is the array itself. You are not required to take input from the console.
Example:
Calculating total tax for a list of 5 billing amounts
Input:Â Â
5
1000 2000 3000 4000 5000
Output:
1000
Explanation :
The first parameter (5) is the size of the array. Next is an array of billing amounts For the first amount there will be 0 tax and for the next amount, it will be 10% of(2000-1000)=100 and so on.
The sum of all the tax amounts will be (0+100+200+300+400=1000)
[Tech Mahindra Coding Questions 2024]
Solution :
public class TotalTaxCalculator {
// Function to calculate the total bill tax amount
static int calcTotalTax(int n, long[] billingAmounts) {
int totalTax = 0;
int threshold = 1000;
for (int i = 1; i < n; i++) {
long amount = billingAmounts[i] – billingAmounts[i – 1];
int tax = (int) (amount > threshold ? 0.1 * (amount – threshold) : 0);
totalTax += tax;
}
return totalTax;
}
public static void main(String[] args) {
long[] billingAmounts = {1000, 2000, 3000, 4000, 5000};
int n = billingAmounts.length;
int result = calcTotalTax(n, billingAmounts);
System.out.println(“Output: ” + result);
}
}
#include <iostream>
using namespace std;
// Function to calculate the total bill tax amount
int calcTotalTax(int n, long billingAmounts[]) {
int totalTax = 0;
int threshold = 1000;
for (int i = 1; i < n; i++) {
long amount = billingAmounts[i] – billingAmounts[i – 1];
int tax = (amount > threshold) ? 0.1 * (amount – threshold) : 0;
totalTax += tax;
}
return totalTax;
}
int main() {
long billingAmounts[] = {1000, 2000, 3000, 4000, 5000};
int n = sizeof(billingAmounts) / sizeof(billingAmounts[0]);
int result = calcTotalTax(n, billingAmounts);
cout << “Output: ” << result << endl;
return 0;
}
# Function to calculate the total bill tax amount
def calcTotalTax(n, billingAmounts):
totalTax = 0
threshold = 1000
for i in range(1, n):
amount = billingAmounts[i] – billingAmounts[i – 1]
tax = int(0.1 * (amount – threshold)) if amount > threshold else 0
totalTax += tax
return totalTax
# Example usage
billingAmounts = [1000, 2000, 3000, 4000, 5000]
n = len(billingAmounts)
result = calcTotalTax(n, billingAmounts)
print(“Output:”, result)