- Understanding Tech Mahindra Coding Questions
- Top 5 Coding Problem Statements for Tech Mahindra
- Tips for Acing Tech Mahindra Coding Test
- Conclusion
- Frequently Asked Questions (FAQs)
Tech Mahindra Coding Questions: Problem Statements & Tips for Freshers
Tech Mahindra’s recruitment process emphasises a candidate’s technical prowess and problem-solving skills. Among its various evaluation parameters, coding questions stand out as a decisive factor. Whether you're aiming for a software developer role or any tech-based position, acing the coding section is crucial.
This article provides a detailed overview of coding question patterns, preparation tips, and actionable insights to help you secure a spot in Tech Mahindra.
Understanding Tech Mahindra Coding Questions
Question Categories
Tech Mahindra’s coding questions cover a mix of theoretical and practical programming challenges. Here are the key categories:
- Data Structures: Arrays, Linked Lists, Trees, Graphs, and Stacks.
- Algorithms: Sorting, Searching, Dynamic Programming, and Recursion.
- Problem Solving: Logical puzzles requiring analytical and coding skills.
- Error Fixing: Debugging provided code snippets.
Sample Questions
To give you a better idea, here’s a table showcasing typical coding challenges:
|
Category |
Example Question |
Difficulty |
|
Arrays |
Find the missing number in an array of size n. |
Easy |
|
Strings |
Determine if a given string is a palindrome. |
Easy |
|
Dynamic Programming |
Solve the Knapsack problem using recursion and memorisation. |
Moderate |
|
Sorting Algorithms |
Write and explain the MergeSort algorithm. |
Advanced |
|
Binary Trees |
Create a function to check if two binary trees are identical. |
Moderate |
Are you looking for Tech Mahindra coding questions? Click here to access coding practice sessions from moderate to challenging levels.
Top 5 Coding Problem Statements for Tech Mahindra
Problem Statement 1
You are given an array of votes each student has got in the student election in your university. You have to find the subsequence of votes of the students who can form a majority to run the student association. A majority is formed when the students have strictly received more than half of the total votes polled.
If there are multiple solutions, return the subsequence with minimum size and if there still exist multiple solutions, return the subsequence with the maximum total sum of all its elements. A subsequence of an array can be obtained by erasing some (possibly zero) elements from the array.
Input Format
The first line contains the length of the password array.
The second line contains an N number, each denoting the votes scored by the particular student.
Output Format
You have to print the list of votes scored by a student that would form the majority in non-increasing order.
Constraints
1<= Size of vote array<= 4*10^4
0<=Value at any index is<=100
Solution C++
#include<bits/stdc++.h>
using namespace std;
vectormajority_find(vector&nums) {
vectorres, freq(101, 0);
//we will create an result and a frequency vector to store the result
//and frequency to find out the largest element.
int sumres=0, sumleft=accumulate(begin(nums), end(nums), 0);
//STL function that would calculate the total sum.
for(auto n: nums)
freq[n]++;//store the frequency of each unique element.
for(int n=100; n>=1; n--){
while(freq[n]-->0&&sumres<=sumleft/*break the loop when result sum crosses left sum*/ )
{
sumres+=n;//calculating result sum.
sumleft-=n;//deducting the ans from total sum
res.push_back(n);
}
}
return res;//returning the result
}
int main()
{
int n,temp;
cin>>n;
vectornums(n);
for(int i=0;i<n;i++)
{
//taking input
cin>>temp;
nums[i]=temp;
}
//calling the function that would solve the problem
auto ans=majority_find(nums);
//printing the output
for(int i=0;i<ans.size();i++)
{
cout<<ans[i]<<" ";
}
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KCnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp2ZWN0b3I8aW50Pm1ham9yaXR5X2ZpbmQodmVjdG9yPGludD4mbnVtcykgewoKICAgIHZlY3RvcjxpbnQ+cmVzLCBmcmVxKDEwMSwgMCk7CgogICAgLy93ZSB3aWxsIGNyZWF0ZSBhbiByZXN1bHQgYW5kIGEgZnJlcXVlbmN5IHZlY3RvciB0byBzdG9yZSB0aGUgcmVzdWx0CgogICAgLy9hbmQgZnJlcXVlbmN5IHRvIGZpbmQgb3V0IHRoZSBsYXJnZXN0IGVsZW1lbnQuCgogICAgaW50IHN1bXJlcz0wLCBzdW1sZWZ0PWFjY3VtdWxhdGUoYmVnaW4obnVtcyksIGVuZChudW1zKSwgMCk7CgogICAgLy9TVEwgZnVuY3Rpb24gdGhhdCB3b3VsZCBjYWxjdWxhdGUgdGhlIHRvdGFsIHN1bS4KCgoKCiAgICBmb3IoYXV0byBuOiBudW1zKQoKICAgICAgICBmcmVxW25dKys7Ly9zdG9yZSB0aGUgZnJlcXVlbmN5IG9mIGVhY2ggdW5pcXVlIGVsZW1lbnQuCgoKCgogICAgZm9yKGludCBuPTEwMDsgbj49MTsgbi0tKXsKCiAgICAgICAgd2hpbGUoZnJlcVtuXS0tPjAmJnN1bXJlczw9c3VtbGVmdC8qYnJlYWsgdGhlIGxvb3Agd2hlbiByZXN1bHQgc3VtIGNyb3NzZXMgbGVmdCBzdW0qLyApCgogICAgICAgIHsKCiAgICAgICAgICAgIHN1bXJlcys9bjsvL2NhbGN1bGF0aW5nIHJlc3VsdCBzdW0uCgogICAgICAgICAgICBzdW1sZWZ0LT1uOy8vZGVkdWN0aW5nIHRoZSBhbnMgZnJvbSB0b3RhbCBzdW0KCiAgICAgICAgICAgIHJlcy5wdXNoX2JhY2sobik7CgogICAgICAgIH0KCiAgICB9CgogICAgcmV0dXJuIHJlczsvL3JldHVybmluZyB0aGUgcmVzdWx0Cgp9CgoKCgogICAgaW50IG1haW4oKQoKewoKICAgIGludCBuLHRlbXA7CgogICAgY2luPj5uOwoKICAgIHZlY3RvcjxpbnQ+bnVtcyhuKTsKCiAgICBmb3IoaW50IGk9MDtpPG47aSsrKQoKICAgIHsKCiAgICAgICAgLy90YWtpbmcgaW5wdXQKCiAgICAgICAgY2luPj50ZW1wOwoKICAgICAgICBudW1zW2ldPXRlbXA7CgogICAgfQoKICAgIC8vY2FsbGluZyB0aGUgZnVuY3Rpb24gdGhhdCB3b3VsZCBzb2x2ZSB0aGUgcHJvYmxlbQoKICAgIGF1dG8gYW5zPW1ham9yaXR5X2ZpbmQobnVtcyk7CgogICAgLy9wcmludGluZyB0aGUgb3V0cHV0CgogICAgZm9yKGludCBpPTA7aTxhbnMuc2l6ZSgpO2krKykKCiAgICB7CgogICAgICAgIGNvdXQ8PGFuc1tpXTw8IiAiOwoKICAgIH0KCiAgICByZXR1cm4gMDsKCn0K
Solution Java
import java.util.*;
public class Main {
public static List majorityFind(int[] nums) {
List res = new ArrayList<>();
int[] freq = new int[101]; // To store frequency of each number.
int sumRes = 0;
int sumLeft = Arrays.stream(nums).sum(); // Total sum of the array.
for (int num : nums) {
freq[num]++; // Count the frequency.
}
// Traverse from largest to smallest element.
for (int n = 100; n >= 1; n--) {
while (freq[n]-- > 0 && sumRes <= sumLeft) {
sumRes += n; // Add to result sum.
sumLeft -= n; // Deduct from remaining sum.
res.add(n); // Add number to result list.
}
}
return res;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] nums = new int[n];
// Input the array.
for (int i = 0; i < n; i++) {
nums[i] = sc.nextInt();
}
// Call the function and print the result.
List ans = majorityFind(nums);
for (int num : ans) {
System.out.print(num + " ");
}
}
}
aW1wb3J0IGphdmEudXRpbC4qOwoKcHVibGljIGNsYXNzIE1haW4gewoKICAgIHB1YmxpYyBzdGF0aWMgTGlzdDxJbnRlZ2VyPiBtYWpvcml0eUZpbmQoaW50W10gbnVtcykgewogICAgICAgIExpc3Q8SW50ZWdlcj4gcmVzID0gbmV3IEFycmF5TGlzdDw+KCk7CiAgICAgICAgaW50W10gZnJlcSA9IG5ldyBpbnRbMTAxXTsgLy8gVG8gc3RvcmUgZnJlcXVlbmN5IG9mIGVhY2ggbnVtYmVyLgogICAgICAgIGludCBzdW1SZXMgPSAwOwogICAgICAgIGludCBzdW1MZWZ0ID0gQXJyYXlzLnN0cmVhbShudW1zKS5zdW0oKTsgLy8gVG90YWwgc3VtIG9mIHRoZSBhcnJheS4KCiAgICAgICAgZm9yIChpbnQgbnVtIDogbnVtcykgewogICAgICAgICAgICBmcmVxW251bV0rKzsgLy8gQ291bnQgdGhlIGZyZXF1ZW5jeS4KICAgICAgICB9CgogICAgICAgIC8vIFRyYXZlcnNlIGZyb20gbGFyZ2VzdCB0byBzbWFsbGVzdCBlbGVtZW50LgogICAgICAgIGZvciAoaW50IG4gPSAxMDA7IG4gPj0gMTsgbi0tKSB7CiAgICAgICAgICAgIHdoaWxlIChmcmVxW25dLS0gPiAwICYmIHN1bVJlcyA8PSBzdW1MZWZ0KSB7CiAgICAgICAgICAgICAgICBzdW1SZXMgKz0gbjsgICAgLy8gQWRkIHRvIHJlc3VsdCBzdW0uCiAgICAgICAgICAgICAgICBzdW1MZWZ0IC09IG47ICAgLy8gRGVkdWN0IGZyb20gcmVtYWluaW5nIHN1bS4KICAgICAgICAgICAgICAgIHJlcy5hZGQobik7ICAgICAvLyBBZGQgbnVtYmVyIHRvIHJlc3VsdCBsaXN0LgogICAgICAgICAgICB9CiAgICAgICAgfQoKICAgICAgICByZXR1cm4gcmVzOwogICAgfQoKICAgIHB1YmxpYyBzdGF0aWMgdm9pZCBtYWluKFN0cmluZ1tdIGFyZ3MpIHsKICAgICAgICBTY2FubmVyIHNjID0gbmV3IFNjYW5uZXIoU3lzdGVtLmluKTsKICAgICAgICBpbnQgbiA9IHNjLm5leHRJbnQoKTsKICAgICAgICBpbnRbXSBudW1zID0gbmV3IGludFtuXTsKCiAgICAgICAgLy8gSW5wdXQgdGhlIGFycmF5LgogICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgaSsrKSB7CiAgICAgICAgICAgIG51bXNbaV0gPSBzYy5uZXh0SW50KCk7CiAgICAgICAgfQoKICAgICAgICAvLyBDYWxsIHRoZSBmdW5jdGlvbiBhbmQgcHJpbnQgdGhlIHJlc3VsdC4KICAgICAgICBMaXN0PEludGVnZXI+IGFucyA9IG1ham9yaXR5RmluZChudW1zKTsKICAgICAgICBmb3IgKGludCBudW0gOiBhbnMpIHsKICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludChudW0gKyAiICIpOwogICAgICAgIH0KICAgIH0KfQo=
Solution Python
def majority_find(nums):
res = []
freq = [0] * 101 # Frequency array for numbers 0 to 100.
sum_res = 0
sum_left = sum(nums) # Total sum of the array.
# Count the frequency of each number.
for num in nums:
freq[num] += 1
# Traverse from largest to smallest element.
for n in range(100, 0, -1):
while freq[n] > 0 and sum_res <= sum_left:
sum_res += n # Add to result sum.
sum_left -= n # Deduct from remaining sum.
res.append(n) # Add number to result list.
freq[n] -= 1 # Decrease the frequency.
return res
# Input
n = int(input())
nums = list(map(int, input().split()))
# Call the function and print the result.
ans = majority_find(nums)
print(*ans)
ZGVmIG1ham9yaXR5X2ZpbmQobnVtcyk6DQogICAgcmVzID0gW10NCiAgICBmcmVxID0gWzBdICogMTAxICAjIEZyZXF1ZW5jeSBhcnJheSBmb3IgbnVtYmVycyAwIHRvIDEwMC4NCiAgICANCiAgICBzdW1fcmVzID0gMA0KICAgIHN1bV9sZWZ0ID0gc3VtKG51bXMpICAjIFRvdGFsIHN1bSBvZiB0aGUgYXJyYXkuDQogICAgDQogICAgIyBDb3VudCB0aGUgZnJlcXVlbmN5IG9mIGVhY2ggbnVtYmVyLg0KICAgIGZvciBudW0gaW4gbnVtczoNCiAgICAgICAgZnJlcVtudW1dICs9IDENCiAgICANCiAgICAjIFRyYXZlcnNlIGZyb20gbGFyZ2VzdCB0byBzbWFsbGVzdCBlbGVtZW50Lg0KICAgIGZvciBuIGluIHJhbmdlKDEwMCwgMCwgLTEpOg0KICAgICAgICB3aGlsZSBmcmVxW25dID4gMCBhbmQgc3VtX3JlcyA8PSBzdW1fbGVmdDoNCiAgICAgICAgICAgIHN1bV9yZXMgKz0gbiAgIyBBZGQgdG8gcmVzdWx0IHN1bS4NCiAgICAgICAgICAgIHN1bV9sZWZ0IC09IG4gICMgRGVkdWN0IGZyb20gcmVtYWluaW5nIHN1bS4NCiAgICAgICAgICAgIHJlcy5hcHBlbmQobikgICMgQWRkIG51bWJlciB0byByZXN1bHQgbGlzdC4NCiAgICAgICAgICAgIGZyZXFbbl0gLT0gMSAgICMgRGVjcmVhc2UgdGhlIGZyZXF1ZW5jeS4NCg0KICAgIHJldHVybiByZXMNCg0KIyBJbnB1dA0KbiA9IGludChpbnB1dCgpKQ0KbnVtcyA9IGxpc3QobWFwKGludCwgaW5wdXQoKS5zcGxpdCgpKSkNCg0KIyBDYWxsIHRoZSBmdW5jdGlvbiBhbmQgcHJpbnQgdGhlIHJlc3VsdC4NCmFucyA9IG1ham9yaXR5X2ZpbmQobnVtcykNCnByaW50KCphbnMpDQo=
Problem Statement 2
You are given a series of queries on the form (A, B), where A represents the count of the number B. For example, the query (3, 5) means the number 5 appears 3 times.
Your task is to compute the absolute difference between the numbers that have the highest and lowest frequencies (appearing at least once). If there are multiple numbers with the same frequency, choose the smallest number for the lowest frequency and the largest number for the highest frequency.
Input Format
The first line contains a single integer denoting the number of queries.
The second line onwards contains two space-separated integers denoting the queries.
Output Format
Output the maximum possible absolute difference between the numbers with the highest and lowest frequencies. If there are only two numbers with the same occurrence, the output should be 0.
Constraints
1 <= Q <= 10^5.
1 <= A, B <= 10^5.
Solution C++
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template using oset =tree<T, null_type, less, rb_tree_tag,tree_order_statistics_node_update> ;
template using omset =tree<T, null_type, less_equal, rb_tree_tag,tree_order_statistics_node_update> ;
// ub --> lb, lb-->ub
// os.order_of_key(x) --> no. of elements strictly less than x, returns an integer
// os.find_by_order(x-1) --> returns an iterator to xth element, 1-based indexing.
using ll=long long;
using ld=long double;
#define gcd __gcd
#define double long double
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define fio ios_base::sync_with_stdio(false);cin.tie(nullptr)
#define ff first
#define ss second
#define pb push_back
#define eb emplace_back
#define deci(x,y) fixed<<setprecision(y)<>x; while(x--)
const int mod= 1e9+7;
int32_t main()
{
fio;
int q; cin>>q;
map<int,int>mp;
while(q--){
int a,b; cin>>a>>b;
mp[b]+=a;
}
int highest_freq=0; int lowest_freq=inf;
int reqnum_1=0; int reqnum_2=0;
for(auto it:mp){
if(highest_freq < it.second){
highest_freq = it.second;
reqnum_1 = it.first;
}
if(lowest_freq> it.second){
lowest_freq = it.second;
reqnum_2 = it.first;
}
}
cout<<abs(reqnum_1-reqnum_2)<<endl;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KI2luY2x1ZGUgPGV4dC9wYl9kcy9hc3NvY19jb250YWluZXIuaHBwPgojaW5jbHVkZSA8ZXh0L3BiX2RzL3RyZWVfcG9saWN5LmhwcD4KCnVzaW5nIG5hbWVzcGFjZSBfX2dudV9wYmRzOwp1c2luZyAJbmFtZXNwYWNlIHN0ZDsKdGVtcGxhdGU8Y2xhc3MgVD4gdXNpbmcgb3NldCA9dHJlZTxULCBudWxsX3R5cGUsIGxlc3M8VD4sIHJiX3RyZWVfdGFnLHRyZWVfb3JkZXJfc3RhdGlzdGljc19ub2RlX3VwZGF0ZT4gOwoKCnRlbXBsYXRlPGNsYXNzIFQ+IHVzaW5nIG9tc2V0ID10cmVlPFQsIG51bGxfdHlwZSwgbGVzc19lcXVhbDxUPiwgcmJfdHJlZV90YWcsdHJlZV9vcmRlcl9zdGF0aXN0aWNzX25vZGVfdXBkYXRlPiA7Ci8vIHViIC0tPiBsYiwgbGItLT51YgovLyAgb3Mub3JkZXJfb2Zfa2V5KHgpIC0tPiBuby4gb2YgZWxlbWVudHMgc3RyaWN0bHkgbGVzcyB0aGFuIHgsIHJldHVybnMgYW4gaW50ZWdlcgovLyBvcy5maW5kX2J5X29yZGVyKHgtMSkgLS0+IHJldHVybnMgYW4gaXRlcmF0b3IgdG8geHRoIGVsZW1lbnQsIDEtYmFzZWQgaW5kZXhpbmcuCgp1c2luZyAJbGw9bG9uZyBsb25nOwp1c2luZyAJbGQ9bG9uZyBkb3VibGU7CiNkZWZpbmUgZ2NkIF9fZ2NkCiNkZWZpbmUgZG91YmxlIGxvbmcgZG91YmxlCiNkZWZpbmUgc2V0Yml0cyh4KSAgICAgIF9fYnVpbHRpbl9wb3Bjb3VudGxsKHgpCiNkZWZpbmUgenJvYml0cyh4KSAgICAgCV9fYnVpbHRpbl9jdHpsbCh4KQojZGVmaW5lIGZpbyAJCQlpb3NfYmFzZTo6c3luY193aXRoX3N0ZGlvKGZhbHNlKTtjaW4udGllKG51bGxwdHIpCiNkZWZpbmUgZmYgCQkJCWZpcnN0CiNkZWZpbmUgc3MgCQkJCXNlY29uZAojZGVmaW5lIHBiIAkJCQlwdXNoX2JhY2sgCiNkZWZpbmUgZWIgCQkJCWVtcGxhY2VfYmFjawojZGVmaW5lIGRlY2koeCx5KSAJCWZpeGVkPDxzZXRwcmVjaXNpb24oeSk8PHgKI2RlZmluZSBhbGwoeCkgICAgIAkJeC5iZWdpbigpLHguZW5kKCkKI2RlZmluZSBtYXhlIAkJCSptYXhfZWxlbWVudAojZGVmaW5lIG1pbmUgCQkJKm1pbl9lbGVtZW50CiNkZWZpbmUgbGIgCQkJCWxvd2VyX2JvdW5kCiNkZWZpbmUgdWIgCQkJCXVwcGVyX2JvdW5kCiNkZWZpbmUgZW5kbCAJCQknXG4nCiNkZWZpbmUgaW5mbCAJCQkxZTE4CiNkZWZpbmUgaW5mIAkJCTJlOAojZGVmaW5lIE1BWCAJCQkxMDAwMDUKI2RlZmluZSBpbnQgCQkJbG9uZyBsb25nCiNkZWZpbmUgdyh4KSAgICAJCWxsIHg7IGNpbj4+eDsgd2hpbGUoeC0tKQpjb25zdCBpbnQgbW9kPSAxZTkrNzsKCgoKaW50MzJfdCBtYWluKCkKewoJZmlvOwoKCWludCBxOyBjaW4+PnE7CgltYXA8aW50LGludD5tcDsKCgl3aGlsZShxLS0pewoJCWludCBhLGI7IGNpbj4+YT4+YjsKCQltcFtiXSs9YTsKCX0KCglpbnQgaGlnaGVzdF9mcmVxPTA7IGludCBsb3dlc3RfZnJlcT1pbmY7CglpbnQgcmVxbnVtXzE9MDsgaW50IHJlcW51bV8yPTA7CgoJZm9yKGF1dG8gaXQ6bXApewoJCWlmKGhpZ2hlc3RfZnJlcSA8IGl0LnNlY29uZCl7CgkJCWhpZ2hlc3RfZnJlcSA9IGl0LnNlY29uZDsKCQkJcmVxbnVtXzEgPSBpdC5maXJzdDsKCQl9CgkJaWYobG93ZXN0X2ZyZXE+IGl0LnNlY29uZCl7CgkJCWxvd2VzdF9mcmVxID0gaXQuc2Vjb25kOwoJCQlyZXFudW1fMiA9IGl0LmZpcnN0OwoJCX0KCX0KCgljb3V0PDxhYnMocmVxbnVtXzEtcmVxbnVtXzIpPDxlbmRsOwoJCn0K
Solution Java
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int q = sc.nextInt();
Map<Integer, Integer> frequencyMap = new HashMap<>();
while (q-- > 0) {
int a = sc.nextInt();
int b = sc.nextInt();
frequencyMap.put(b, frequencyMap.getOrDefault(b, 0) + a);
}
int highestFreq = 0;
int lowestFreq = Integer.MAX_VALUE;
int numberWithHighestFreq = 0;
int numberWithLowestFreq = 0;
for (Map.Entry<Integer, Integer> entry : frequencyMap.entrySet()) {
int number = entry.getKey();
int freq = entry.getValue();
if (freq > highestFreq || (freq == highestFreq && number > numberWithHighestFreq)) {
highestFreq = freq;
numberWithHighestFreq = number;
}
if (freq < lowestFreq || (freq == lowestFreq && number < numberWithLowestFreq)) {
lowestFreq = freq;
numberWithLowestFreq = number;
}
}
System.out.println(Math.abs(numberWithHighestFreq - numberWithLowestFreq));
}
}
aW1wb3J0IGphdmEudXRpbC4qOwoKcHVibGljIGNsYXNzIE1haW4gewogICAgcHVibGljIHN0YXRpYyB2b2lkIG1haW4oU3RyaW5nW10gYXJncykgewogICAgICAgIFNjYW5uZXIgc2MgPSBuZXcgU2Nhbm5lcihTeXN0ZW0uaW4pOwogICAgICAgIAogICAgICAgIGludCBxID0gc2MubmV4dEludCgpOwogICAgICAgIE1hcDxJbnRlZ2VyLCBJbnRlZ2VyPiBmcmVxdWVuY3lNYXAgPSBuZXcgSGFzaE1hcDw+KCk7CiAgICAgICAgCiAgICAgICAgd2hpbGUgKHEtLSA+IDApIHsKICAgICAgICAgICAgaW50IGEgPSBzYy5uZXh0SW50KCk7CiAgICAgICAgICAgIGludCBiID0gc2MubmV4dEludCgpOwogICAgICAgICAgICBmcmVxdWVuY3lNYXAucHV0KGIsIGZyZXF1ZW5jeU1hcC5nZXRPckRlZmF1bHQoYiwgMCkgKyBhKTsKICAgICAgICB9CiAgICAgICAgCiAgICAgICAgaW50IGhpZ2hlc3RGcmVxID0gMDsKICAgICAgICBpbnQgbG93ZXN0RnJlcSA9IEludGVnZXIuTUFYX1ZBTFVFOwogICAgICAgIGludCBudW1iZXJXaXRoSGlnaGVzdEZyZXEgPSAwOwogICAgICAgIGludCBudW1iZXJXaXRoTG93ZXN0RnJlcSA9IDA7CiAgICAgICAgCiAgICAgICAgZm9yIChNYXAuRW50cnk8SW50ZWdlciwgSW50ZWdlcj4gZW50cnkgOiBmcmVxdWVuY3lNYXAuZW50cnlTZXQoKSkgewogICAgICAgICAgICBpbnQgbnVtYmVyID0gZW50cnkuZ2V0S2V5KCk7CiAgICAgICAgICAgIGludCBmcmVxID0gZW50cnkuZ2V0VmFsdWUoKTsKICAgICAgICAgICAgCiAgICAgICAgICAgIGlmIChmcmVxID4gaGlnaGVzdEZyZXEgfHwgKGZyZXEgPT0gaGlnaGVzdEZyZXEgJiYgbnVtYmVyID4gbnVtYmVyV2l0aEhpZ2hlc3RGcmVxKSkgewogICAgICAgICAgICAgICAgaGlnaGVzdEZyZXEgPSBmcmVxOwogICAgICAgICAgICAgICAgbnVtYmVyV2l0aEhpZ2hlc3RGcmVxID0gbnVtYmVyOwogICAgICAgICAgICB9CiAgICAgICAgICAgIAogICAgICAgICAgICBpZiAoZnJlcSA8IGxvd2VzdEZyZXEgfHwgKGZyZXEgPT0gbG93ZXN0RnJlcSAmJiBudW1iZXIgPCBudW1iZXJXaXRoTG93ZXN0RnJlcSkpIHsKICAgICAgICAgICAgICAgIGxvd2VzdEZyZXEgPSBmcmVxOwogICAgICAgICAgICAgICAgbnVtYmVyV2l0aExvd2VzdEZyZXEgPSBudW1iZXI7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgCiAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKE1hdGguYWJzKG51bWJlcldpdGhIaWdoZXN0RnJlcSAtIG51bWJlcldpdGhMb3dlc3RGcmVxKSk7CiAgICB9Cn0K
Solution Python
def main():
import sys
input = sys.stdin.read
data = input().split()
q = int(data[0])
frequency_map = {}
index = 1
for _ in range(q):
a = int(data[index])
b = int(data[index + 1])
if b in frequency_map:
frequency_map[b] += a
else:
frequency_map[b] = a
index += 2
highest_freq = 0
lowest_freq = float('inf')
number_with_highest_freq = 0
number_with_lowest_freq = 0
for number, freq in frequency_map.items():
if freq > highest_freq or (freq == highest_freq and number > number_with_highest_freq):
highest_freq = freq
number_with_highest_freq = number
if freq < lowest_freq or (freq == lowest_freq and number < number_with_lowest_freq):
lowest_freq = freq
number_with_lowest_freq = number
print(abs(number_with_highest_freq - number_with_lowest_freq))
if __name__ == "__main__":
main()
ZGVmIG1haW4oKToNCiAgICBpbXBvcnQgc3lzDQogICAgaW5wdXQgPSBzeXMuc3RkaW4ucmVhZA0KICAgIGRhdGEgPSBpbnB1dCgpLnNwbGl0KCkNCiAgICANCiAgICBxID0gaW50KGRhdGFbMF0pDQogICAgZnJlcXVlbmN5X21hcCA9IHt9DQogICAgDQogICAgaW5kZXggPSAxDQogICAgZm9yIF8gaW4gcmFuZ2UocSk6DQogICAgICAgIGEgPSBpbnQoZGF0YVtpbmRleF0pDQogICAgICAgIGIgPSBpbnQoZGF0YVtpbmRleCArIDFdKQ0KICAgICAgICBpZiBiIGluIGZyZXF1ZW5jeV9tYXA6DQogICAgICAgICAgICBmcmVxdWVuY3lfbWFwW2JdICs9IGENCiAgICAgICAgZWxzZToNCiAgICAgICAgICAgIGZyZXF1ZW5jeV9tYXBbYl0gPSBhDQogICAgICAgIGluZGV4ICs9IDINCiAgICANCiAgICBoaWdoZXN0X2ZyZXEgPSAwDQogICAgbG93ZXN0X2ZyZXEgPSBmbG9hdCgnaW5mJykNCiAgICBudW1iZXJfd2l0aF9oaWdoZXN0X2ZyZXEgPSAwDQogICAgbnVtYmVyX3dpdGhfbG93ZXN0X2ZyZXEgPSAwDQogICAgDQogICAgZm9yIG51bWJlciwgZnJlcSBpbiBmcmVxdWVuY3lfbWFwLml0ZW1zKCk6DQogICAgICAgIGlmIGZyZXEgPiBoaWdoZXN0X2ZyZXEgb3IgKGZyZXEgPT0gaGlnaGVzdF9mcmVxIGFuZCBudW1iZXIgPiBudW1iZXJfd2l0aF9oaWdoZXN0X2ZyZXEpOg0KICAgICAgICAgICAgaGlnaGVzdF9mcmVxID0gZnJlcQ0KICAgICAgICAgICAgbnVtYmVyX3dpdGhfaGlnaGVzdF9mcmVxID0gbnVtYmVyDQogICAgICAgIA0KICAgICAgICBpZiBmcmVxIDwgbG93ZXN0X2ZyZXEgb3IgKGZyZXEgPT0gbG93ZXN0X2ZyZXEgYW5kIG51bWJlciA8IG51bWJlcl93aXRoX2xvd2VzdF9mcmVxKToNCiAgICAgICAgICAgIGxvd2VzdF9mcmVxID0gZnJlcQ0KICAgICAgICAgICAgbnVtYmVyX3dpdGhfbG93ZXN0X2ZyZXEgPSBudW1iZXINCiAgICANCiAgICBwcmludChhYnMobnVtYmVyX3dpdGhfaGlnaGVzdF9mcmVxIC0gbnVtYmVyX3dpdGhfbG93ZXN0X2ZyZXEpKQ0KDQppZiBfX25hbWVfXyA9PSAiX19tYWluX18iOg0KICAgIG1haW4oKQ0K
Problem Statement 3
During the night of the street race, there are N cars on the street, each with a given speed. The police will be alerted if the total number of speed variation pairs exceeds a certain threshold, X.
A pair of cars (i, j) has a speed variation if the absolute difference in their speeds is at least K. The total speed variation count is the number of such pairs where the speed difference is at least K.
You need to determine whether the total speed variation count is greater than the given threshold, X. If it is, the police will be alerted; otherwise, they will not.
Input Format
The first line contains three space-separated integers: N, the number of cars; K, the minimum speed difference for variation; and X, the alert threshold.
The second line contains N space-separated integers representing the speeds of the cars.
Output Format
Print YES if the total speed variation count exceeds X; otherwise, print NO.
Constraints
1 <= N <= 2 * 10^5
1 <= K, X <= 10^8
1 <= S[i] <= 10^8
Solution C++
#include <bits/stdc++.h>
using namespace std;
void solve()
{
int n;
long long k, x;
cin >> n >> k >> x;
vector a(n);
for (auto &x : a) cin >> x;
sort(a.begin(), a.end()); // sort in non-decreasing order
long long cnt = 0; // store speed variations
int i = 0, j = 0;
while (i < n) {
if (j < i) j = i;
else if (j >= n) break; // all elements have been processed
if (a[j] - a[i] >= k) { // it is speed variation if difference is atleast k
i++;
cnt += (n - j);
}
else j++;
}
if (cnt >= x) cout << "YES\n"; // if speed variations count is atleast x the cops will be alerted
else cout << "NO\n";
}
int main()
{
#ifndef ONLINE_JUDGE
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
//freopen("Error.txt", "w", stderr);
#endif
ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
int t = 1;
//cin >> t;
while (t--) solve();
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp2b2lkIHNvbHZlKCkKewoJaW50IG47Cglsb25nIGxvbmcgaywgeDsKCWNpbiA+PiBuID4+IGsgPj4geDsKCgl2ZWN0b3I8bG9uZyBsb25nPiBhKG4pOwoJZm9yIChhdXRvICZ4IDogYSkgY2luID4+IHg7CgoJc29ydChhLmJlZ2luKCksIGEuZW5kKCkpOyAvLyBzb3J0IGluIG5vbi1kZWNyZWFzaW5nIG9yZGVyCglsb25nIGxvbmcgY250ID0gMDsgLy8gc3RvcmUgc3BlZWQgdmFyaWF0aW9ucwoKCWludCBpID0gMCwgaiA9IDA7Cgl3aGlsZSAoaSA8IG4pIHsKCQlpZiAoaiA8IGkpIGogPSBpOwoJCWVsc2UgaWYgKGogPj0gbikgYnJlYWs7IC8vIGFsbCBlbGVtZW50cyBoYXZlIGJlZW4gcHJvY2Vzc2VkCgoJCWlmIChhW2pdIC0gYVtpXSA+PSBrKSB7IC8vIGl0IGlzIHNwZWVkIHZhcmlhdGlvbiBpZiBkaWZmZXJlbmNlIGlzIGF0bGVhc3QgawoJCQlpKys7CgkJCWNudCArPSAobiAtIGopOwoJCX0KCQllbHNlIGorKzsKCX0KCglpZiAoY250ID49IHgpIGNvdXQgPDwgIllFU1xuIjsgLy8gaWYgc3BlZWQgdmFyaWF0aW9ucyBjb3VudCBpcyBhdGxlYXN0IHggdGhlIGNvcHMgd2lsbCBiZSBhbGVydGVkCgllbHNlIGNvdXQgPDwgIk5PXG4iOwp9CgppbnQgbWFpbigpCnsKI2lmbmRlZiBPTkxJTkVfSlVER0UKCS8vZnJlb3BlbigiaW5wdXQudHh0IiwgInIiLCBzdGRpbik7CgkvL2ZyZW9wZW4oIm91dHB1dC50eHQiLCAidyIsIHN0ZG91dCk7CgkvL2ZyZW9wZW4oIkVycm9yLnR4dCIsICJ3Iiwgc3RkZXJyKTsKI2VuZGlmCgoJaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbyhmYWxzZSksIGNpbi50aWUoTlVMTCksIGNvdXQudGllKE5VTEwpOwoJaW50IHQgPSAxOwoJLy9jaW4gPj4gdDsKCXdoaWxlICh0LS0pIHNvbHZlKCk7CgoJcmV0dXJuIDA7Cn0K
Solution Java
import java.util.*;
public class Main {
public static void solve() {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long k = sc.nextLong();
long x = sc.nextLong();
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = sc.nextLong();
}
Arrays.sort(a); // sort in non-decreasing order
long cnt = 0; // store speed variations
int i = 0, j = 0;
while (i < n) {
if (j < i) {
j = i;
} else if (j >= n) {
break; // all elements have been processed
}
if (a[j] - a[i] >= k) { // it is speed variation if difference is at least k
i++;
cnt += (n - j);
} else {
j++;
}
}
if (cnt >= x) {
System.out.println("YES"); // if speed variations count is at least x, the cops will be alerted
} else {
System.out.println("NO");
}
sc.close();
}
public static void main(String[] args) {
int t = 1;
// Scanner sc = new Scanner(System.in);
// t = sc.nextInt(); // if multiple test cases are needed
while (t > 0) {
solve();
t--;
}
}
}
aW1wb3J0IGphdmEudXRpbC4qOwoKcHVibGljIGNsYXNzIE1haW4gewoKICAgIHB1YmxpYyBzdGF0aWMgdm9pZCBzb2x2ZSgpIHsKICAgICAgICBTY2FubmVyIHNjID0gbmV3IFNjYW5uZXIoU3lzdGVtLmluKTsKCiAgICAgICAgaW50IG4gPSBzYy5uZXh0SW50KCk7CiAgICAgICAgbG9uZyBrID0gc2MubmV4dExvbmcoKTsKICAgICAgICBsb25nIHggPSBzYy5uZXh0TG9uZygpOwoKICAgICAgICBsb25nW10gYSA9IG5ldyBsb25nW25dOwogICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgaSsrKSB7CiAgICAgICAgICAgIGFbaV0gPSBzYy5uZXh0TG9uZygpOwogICAgICAgIH0KCiAgICAgICAgQXJyYXlzLnNvcnQoYSk7ICAvLyBzb3J0IGluIG5vbi1kZWNyZWFzaW5nIG9yZGVyCiAgICAgICAgbG9uZyBjbnQgPSAwOyAgLy8gc3RvcmUgc3BlZWQgdmFyaWF0aW9ucwoKICAgICAgICBpbnQgaSA9IDAsIGogPSAwOwogICAgICAgIHdoaWxlIChpIDwgbikgewogICAgICAgICAgICBpZiAoaiA8IGkpIHsKICAgICAgICAgICAgICAgIGogPSBpOwogICAgICAgICAgICB9IGVsc2UgaWYgKGogPj0gbikgewogICAgICAgICAgICAgICAgYnJlYWs7ICAvLyBhbGwgZWxlbWVudHMgaGF2ZSBiZWVuIHByb2Nlc3NlZAogICAgICAgICAgICB9CgogICAgICAgICAgICBpZiAoYVtqXSAtIGFbaV0gPj0gaykgeyAgLy8gaXQgaXMgc3BlZWQgdmFyaWF0aW9uIGlmIGRpZmZlcmVuY2UgaXMgYXQgbGVhc3QgawogICAgICAgICAgICAgICAgaSsrOwogICAgICAgICAgICAgICAgY250ICs9IChuIC0gaik7CiAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICBqKys7CiAgICAgICAgICAgIH0KICAgICAgICB9CgogICAgICAgIGlmIChjbnQgPj0geCkgewogICAgICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oIllFUyIpOyAgLy8gaWYgc3BlZWQgdmFyaWF0aW9ucyBjb3VudCBpcyBhdCBsZWFzdCB4LCB0aGUgY29wcyB3aWxsIGJlIGFsZXJ0ZWQKICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oIk5PIik7CiAgICAgICAgfQoKICAgICAgICBzYy5jbG9zZSgpOwogICAgfQoKICAgIHB1YmxpYyBzdGF0aWMgdm9pZCBtYWluKFN0cmluZ1tdIGFyZ3MpIHsKICAgICAgICBpbnQgdCA9IDE7CiAgICAgICAgLy8gU2Nhbm5lciBzYyA9IG5ldyBTY2FubmVyKFN5c3RlbS5pbik7CiAgICAgICAgLy8gdCA9IHNjLm5leHRJbnQoKTsgIC8vIGlmIG11bHRpcGxlIHRlc3QgY2FzZXMgYXJlIG5lZWRlZAogICAgICAgIHdoaWxlICh0ID4gMCkgewogICAgICAgICAgICBzb2x2ZSgpOwogICAgICAgICAgICB0LS07CiAgICAgICAgfQogICAgfQp9Cg==
Solution Python
def solve():
n, k, x = map(int, input().split())
a = list(map(int, input().split()))
a.sort() # sort in non-decreasing order
cnt = 0 # store speed variations
i, j = 0, 0
while i < n:
if j < i:
j = i
elif j >= n:
break # all elements have been processed
if a[j] - a[i] >= k: # it is speed variation if the difference is at least k
i += 1
cnt += (n - j)
else:
j += 1
if cnt >= x:
print("YES") # if speed variations count is at least x, the cops will be alerted
else:
print("NO")
if __name__ == "__main__":
t = 1
# t = int(input()) # if multiple test cases are needed
while t > 0:
solve()
t -= 1
ZGVmIHNvbHZlKCk6DQogICAgbiwgaywgeCA9IG1hcChpbnQsIGlucHV0KCkuc3BsaXQoKSkNCiAgICBhID0gbGlzdChtYXAoaW50LCBpbnB1dCgpLnNwbGl0KCkpKQ0KDQogICAgYS5zb3J0KCkgICMgc29ydCBpbiBub24tZGVjcmVhc2luZyBvcmRlcg0KICAgIGNudCA9IDAgICMgc3RvcmUgc3BlZWQgdmFyaWF0aW9ucw0KDQogICAgaSwgaiA9IDAsIDANCiAgICB3aGlsZSBpIDwgbjoNCiAgICAgICAgaWYgaiA8IGk6DQogICAgICAgICAgICBqID0gaQ0KICAgICAgICBlbGlmIGogPj0gbjoNCiAgICAgICAgICAgIGJyZWFrICAjIGFsbCBlbGVtZW50cyBoYXZlIGJlZW4gcHJvY2Vzc2VkDQoNCiAgICAgICAgaWYgYVtqXSAtIGFbaV0gPj0gazogICMgaXQgaXMgc3BlZWQgdmFyaWF0aW9uIGlmIHRoZSBkaWZmZXJlbmNlIGlzIGF0IGxlYXN0IGsNCiAgICAgICAgICAgIGkgKz0gMQ0KICAgICAgICAgICAgY250ICs9IChuIC0gaikNCiAgICAgICAgZWxzZToNCiAgICAgICAgICAgIGogKz0gMQ0KDQogICAgaWYgY250ID49IHg6DQogICAgICAgIHByaW50KCJZRVMiKSAgIyBpZiBzcGVlZCB2YXJpYXRpb25zIGNvdW50IGlzIGF0IGxlYXN0IHgsIHRoZSBjb3BzIHdpbGwgYmUgYWxlcnRlZA0KICAgIGVsc2U6DQogICAgICAgIHByaW50KCJOTyIpDQoNCmlmIF9fbmFtZV9fID09ICJfX21haW5fXyI6DQogICAgdCA9IDENCiAgICAjIHQgPSBpbnQoaW5wdXQoKSkgICMgaWYgbXVsdGlwbGUgdGVzdCBjYXNlcyBhcmUgbmVlZGVkDQogICAgd2hpbGUgdCA+IDA6DQogICAgICAgIHNvbHZlKCkNCiAgICAgICAgdCAtPSAxDQo=
Problem Statement 4
You are given a string made up of only lowercase English letters. Your task is to find the length of the longest substring that is "good." You can change the last character of the string to help make the substring as long as possible.
A substring is called "good" if the difference between consecutive characters is exactly 1, and the characters are in ascending order (e.g., "abc" is good, but "cba" is not).
Note: str contains only lowercase English alphabet
Input Format
The first line contains a single string of lowercase English letters denoting str.
Output Format
Print the length of the longest "good" substring
Constraints
1 <= |str| <= 10^5
Solution C++
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
ll get_ans(string s)
{
ll ans = INT_MIN , maxi = 1; // initializng the ans and maxi
for(ll i = 1; i < s.size(); i++)
{
if(s[i-1]+1 == s[i]) // checking if the characters are consecutive in ascending order
maxi++;
else
{
ans = max(ans,maxi); // updating the ans
maxi = 1;
}
}
return max(ans,maxi); // returning the length of maximum good string
}
ll solve(string str)
{
if(str.size()==1)
return 1;
ll n = str.size();
if(str[n-2]!='z')
{
str[n-1]=str[n-2]+1; // changing the last character if feasible
}
return get_ans(str);
}
int main()
{
string str;
cin>>str;
cout<<solve(str);
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNkZWZpbmUgbGwgbG9uZyBsb25nIGludApsbCBnZXRfYW5zKHN0cmluZyBzKSAKewogIGxsIGFucyA9IElOVF9NSU4gLCBtYXhpID0gMTsgLy8gaW5pdGlhbGl6bmcgdGhlIGFucyBhbmQgbWF4aQogIGZvcihsbCBpID0gMTsgaSA8IHMuc2l6ZSgpOyBpKyspCiAgewogICAgaWYoc1tpLTFdKzEgPT0gc1tpXSkgLy8gY2hlY2tpbmcgaWYgdGhlIGNoYXJhY3RlcnMgYXJlIGNvbnNlY3V0aXZlIGluIGFzY2VuZGluZyBvcmRlciAKICAgIG1heGkrKzsKICAgIGVsc2UKICAgIHsKICAgICAgYW5zID0gbWF4KGFucyxtYXhpKTsgLy8gdXBkYXRpbmcgdGhlIGFucwogICAgICBtYXhpID0gMTsKICAgIH0KICB9CiAgcmV0dXJuIG1heChhbnMsbWF4aSk7IC8vIHJldHVybmluZyB0aGUgbGVuZ3RoIG9mIG1heGltdW0gZ29vZCBzdHJpbmcKfQoKbGwgc29sdmUoc3RyaW5nIHN0cikKewogIGlmKHN0ci5zaXplKCk9PTEpCiAgcmV0dXJuIDE7CiAgCiAgbGwgbiA9IHN0ci5zaXplKCk7CiAgaWYoc3RyW24tMl0hPSd6JykKICB7CiAgICBzdHJbbi0xXT1zdHJbbi0yXSsxOyAvLyBjaGFuZ2luZyB0aGUgbGFzdCBjaGFyYWN0ZXIgaWYgZmVhc2libGUKICB9CiAgcmV0dXJuIGdldF9hbnMoc3RyKTsKfQppbnQgbWFpbigpIAp7CiAgc3RyaW5nIHN0cjsKICBjaW4+PnN0cjsKICAKICBjb3V0PDxzb2x2ZShzdHIpOwogIHJldHVybiAwOwp9Cg==
Solution Java
import java.util.Scanner;
public class Main {
static long getAns(String s) {
long ans = Long.MIN_VALUE;
long maxi = 1;
for (int i = 1; i < s.length(); i++) {
if (s.charAt(i - 1) + 1 == s.charAt(i)) { // checking if the characters are consecutive in ascending order
maxi++;
} else {
ans = Math.max(ans, maxi);
maxi = 1;
}
}
return Math.max(ans, maxi); // returning the length of maximum good string
}
static long solve(String str) {
if (str.length() == 1) {
return 1;
}
int n = str.length();
if (str.charAt(n - 2) != 'z') {
str = str.substring(0, n - 1) + (char)(str.charAt(n - 2) + 1); // changing the last character if feasible
}
return getAns(str);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str = scanner.next();
System.out.println(solve(str));
scanner.close();
}
}
aW1wb3J0IGphdmEudXRpbC5TY2FubmVyOwoKcHVibGljIGNsYXNzIE1haW4gewogICAgc3RhdGljIGxvbmcgZ2V0QW5zKFN0cmluZyBzKSB7CiAgICAgICAgbG9uZyBhbnMgPSBMb25nLk1JTl9WQUxVRTsKICAgICAgICBsb25nIG1heGkgPSAxOwogICAgICAgIGZvciAoaW50IGkgPSAxOyBpIDwgcy5sZW5ndGgoKTsgaSsrKSB7CiAgICAgICAgICAgIGlmIChzLmNoYXJBdChpIC0gMSkgKyAxID09IHMuY2hhckF0KGkpKSB7ICAvLyBjaGVja2luZyBpZiB0aGUgY2hhcmFjdGVycyBhcmUgY29uc2VjdXRpdmUgaW4gYXNjZW5kaW5nIG9yZGVyCiAgICAgICAgICAgICAgICBtYXhpKys7CiAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICBhbnMgPSBNYXRoLm1heChhbnMsIG1heGkpOwogICAgICAgICAgICAgICAgbWF4aSA9IDE7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgcmV0dXJuIE1hdGgubWF4KGFucywgbWF4aSk7ICAvLyByZXR1cm5pbmcgdGhlIGxlbmd0aCBvZiBtYXhpbXVtIGdvb2Qgc3RyaW5nCiAgICB9CgogICAgc3RhdGljIGxvbmcgc29sdmUoU3RyaW5nIHN0cikgewogICAgICAgIGlmIChzdHIubGVuZ3RoKCkgPT0gMSkgewogICAgICAgICAgICByZXR1cm4gMTsKICAgICAgICB9CiAgICAgICAgCiAgICAgICAgaW50IG4gPSBzdHIubGVuZ3RoKCk7CiAgICAgICAgaWYgKHN0ci5jaGFyQXQobiAtIDIpICE9ICd6JykgewogICAgICAgICAgICBzdHIgPSBzdHIuc3Vic3RyaW5nKDAsIG4gLSAxKSArIChjaGFyKShzdHIuY2hhckF0KG4gLSAyKSArIDEpOyAgLy8gY2hhbmdpbmcgdGhlIGxhc3QgY2hhcmFjdGVyIGlmIGZlYXNpYmxlCiAgICAgICAgfQogICAgICAgIHJldHVybiBnZXRBbnMoc3RyKTsKICAgIH0KCiAgICBwdWJsaWMgc3RhdGljIHZvaWQgbWFpbihTdHJpbmdbXSBhcmdzKSB7CiAgICAgICAgU2Nhbm5lciBzY2FubmVyID0gbmV3IFNjYW5uZXIoU3lzdGVtLmluKTsKICAgICAgICBTdHJpbmcgc3RyID0gc2Nhbm5lci5uZXh0KCk7CiAgICAgICAgCiAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKHNvbHZlKHN0cikpOwogICAgICAgIHNjYW5uZXIuY2xvc2UoKTsKICAgIH0KfQo=
Solution Python
def get_ans(s):
ans = float('-inf')
maxi = 1
for i in range(1, len(s)):
if ord(s[i-1]) + 1 == ord(s[i]): # checking if the characters are consecutive in ascending order
maxi += 1
else:
ans = max(ans, maxi)
maxi = 1
return max(ans, maxi) # returning the length of maximum good string
def solve(str):
if len(str) == 1:
return 1
n = len(str)
if str[n-2] != 'z':
str = str[:n-1] + chr(ord(str[n-2]) + 1) # changing the last character if feasible
return get_ans(str)
if __name__ == "__main__":
str = input()
print(solve(str))
ZGVmIGdldF9hbnMocyk6DQogICAgYW5zID0gZmxvYXQoJy1pbmYnKQ0KICAgIG1heGkgPSAxDQogICAgZm9yIGkgaW4gcmFuZ2UoMSwgbGVuKHMpKToNCiAgICAgICAgaWYgb3JkKHNbaS0xXSkgKyAxID09IG9yZChzW2ldKTogICMgY2hlY2tpbmcgaWYgdGhlIGNoYXJhY3RlcnMgYXJlIGNvbnNlY3V0aXZlIGluIGFzY2VuZGluZyBvcmRlcg0KICAgICAgICAgICAgbWF4aSArPSAxDQogICAgICAgIGVsc2U6DQogICAgICAgICAgICBhbnMgPSBtYXgoYW5zLCBtYXhpKQ0KICAgICAgICAgICAgbWF4aSA9IDENCiAgICByZXR1cm4gbWF4KGFucywgbWF4aSkgICMgcmV0dXJuaW5nIHRoZSBsZW5ndGggb2YgbWF4aW11bSBnb29kIHN0cmluZw0KDQpkZWYgc29sdmUoc3RyKToNCiAgICBpZiBsZW4oc3RyKSA9PSAxOg0KICAgICAgICByZXR1cm4gMQ0KICAgIA0KICAgIG4gPSBsZW4oc3RyKQ0KICAgIGlmIHN0cltuLTJdICE9ICd6JzoNCiAgICAgICAgc3RyID0gc3RyWzpuLTFdICsgY2hyKG9yZChzdHJbbi0yXSkgKyAxKSAgIyBjaGFuZ2luZyB0aGUgbGFzdCBjaGFyYWN0ZXIgaWYgZmVhc2libGUNCiAgICByZXR1cm4gZ2V0X2FucyhzdHIpDQoNCmlmIF9fbmFtZV9fID09ICJfX21haW5fXyI6DQogICAgc3RyID0gaW5wdXQoKQ0KICAgIHByaW50KHNvbHZlKHN0cikpDQo=
Problem Statement 5
Alice enjoys working with numbers and has discovered an interesting property about them. She realized that any number can be represented as a sequence of its digits. For example, the number 12 can be represented as the array digits of {1, 2}, and the number 1 can be represented as the array digits of {1}.
Alice defines a number as "good" if the absolute difference between every pair of consecutive digits in its array representation of digits is exactly 1.
For example, Number 1234 is a good number as it is in digit array {1, 2, 3, 4}. The difference between every consecutive pair is exactly 1.
Your task is to help Alice by finding all the "good" numbers from 0 to N, inclusive, where N is a given integer.
Note: All single-digit numbers are considered good, as there are no consecutive digits to compare.
Input Format
The first line of input contains a single integer N representing the upper limit of the range.
Output Format
Print all the "good" numbers from 0 to N, separated by spaces.
Constraints
1 <= N <= 10^5
Solution C++
#include <bits/stdc++.h>
using namespace std;
bool checkJumpingNumber(int num)
{
int prevDigit = num % 10;
num = num / 10;
while (num > 0)
{
int currentDigit = num % 10;
num = num / 10;
int diff = abs(currentDigit - prevDigit); // finding difference between current digit and previous digit.
if (diff != 1) // returning false as difference is not 1
{
return false;
}
prevDigit = currentDigit; // making prevDigit equal to currentDigit
}
return true; // returning true at the end
}
vector solve(int n)
{
vector jumpingNums;
for (int i = 0; i <= n; i++) // iterating over all the numbers from 0 to N.
{
if (checkJumpingNumber(i)) // adding number to the ans if it's satisfying the condition
{
jumpingNums.push_back(i);
}
}
return jumpingNums; // returning the ans
}
int main()
{
int n;
cin>>n;
vectorans=solve(n);
for(auto i : ans)
{
cout<<i<<" ";
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgpib29sIGNoZWNrSnVtcGluZ051bWJlcihpbnQgbnVtKQp7CiAgaW50IHByZXZEaWdpdCA9IG51bSAlIDEwOwogIG51bSA9IG51bSAvIDEwOwoKICB3aGlsZSAobnVtID4gMCkKICB7CiAgICAgIGludCBjdXJyZW50RGlnaXQgPSBudW0gJSAxMDsKICAgICAgbnVtID0gbnVtIC8gMTA7CgogICAgICBpbnQgZGlmZiA9IGFicyhjdXJyZW50RGlnaXQgLSBwcmV2RGlnaXQpOyAvLyBmaW5kaW5nIGRpZmZlcmVuY2UgYmV0d2VlbiBjdXJyZW50IGRpZ2l0IGFuZCBwcmV2aW91cyBkaWdpdC4KICAgICAgaWYgKGRpZmYgIT0gMSkgLy8gcmV0dXJuaW5nIGZhbHNlIGFzIGRpZmZlcmVuY2UgaXMgbm90IDEKICAgICAgewogICAgICAgICAgcmV0dXJuIGZhbHNlOwogICAgICB9CgogICAgICBwcmV2RGlnaXQgPSBjdXJyZW50RGlnaXQ7IC8vIG1ha2luZyBwcmV2RGlnaXQgZXF1YWwgdG8gY3VycmVudERpZ2l0CiAgfQogIHJldHVybiB0cnVlOyAvLyByZXR1cm5pbmcgdHJ1ZSBhdCB0aGUgZW5kCn0KCnZlY3RvcjxpbnQ+IHNvbHZlKGludCBuKQp7CiAgdmVjdG9yPGludD4ganVtcGluZ051bXM7CiAgZm9yIChpbnQgaSA9IDA7IGkgPD0gbjsgaSsrKSAvLyBpdGVyYXRpbmcgb3ZlciBhbGwgdGhlIG51bWJlcnMgZnJvbSAwIHRvIE4uCiAgewogICAgaWYgKGNoZWNrSnVtcGluZ051bWJlcihpKSkgLy8gYWRkaW5nIG51bWJlciB0byB0aGUgYW5zIGlmIGl0J3Mgc2F0aXNmeWluZyB0aGUgY29uZGl0aW9uCiAgICB7CiAgICAgICAganVtcGluZ051bXMucHVzaF9iYWNrKGkpOwogICAgfQogIH0KICByZXR1cm4ganVtcGluZ051bXM7IC8vIHJldHVybmluZyB0aGUgYW5zCn0KCmludCBtYWluKCkgCnsKICBpbnQgbjsKICBjaW4+Pm47CiAgdmVjdG9yPGludD5hbnM9c29sdmUobik7CiAgZm9yKGF1dG8gaSA6IGFucykKICB7CiAgICBjb3V0PDxpPDwiICI7CiAgfQogIHJldHVybiAwOwp9Cg==
Solution Java
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static boolean checkJumpingNumber(int num) {
int prevDigit = num % 10;
num = num / 10;
while (num > 0) {
int currentDigit = num % 10;
num = num / 10;
int diff = Math.abs(currentDigit - prevDigit); // finding difference between current digit and previous digit
if (diff != 1) { // returning false as difference is not 1
return false;
}
prevDigit = currentDigit; // making prevDigit equal to currentDigit
}
return true; // returning true at the end
}
public static List solve(int n) {
List jumpingNums = new ArrayList<>();
for (int i = 0; i <= n; i++) { // iterating over all the numbers from 0 to n
if (checkJumpingNumber(i)) { // adding number to the result if it satisfies the condition
jumpingNums.add(i);
}
}
return jumpingNums; // returning the result
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
List ans = solve(n);
for (int i : ans) {
System.out.print(i + " ");
}
sc.close();
}
}
aW1wb3J0IGphdmEudXRpbC5BcnJheUxpc3Q7CmltcG9ydCBqYXZhLnV0aWwuTGlzdDsKaW1wb3J0IGphdmEudXRpbC5TY2FubmVyOwoKcHVibGljIGNsYXNzIE1haW4gewoKICAgIHB1YmxpYyBzdGF0aWMgYm9vbGVhbiBjaGVja0p1bXBpbmdOdW1iZXIoaW50IG51bSkgewogICAgICAgIGludCBwcmV2RGlnaXQgPSBudW0gJSAxMDsKICAgICAgICBudW0gPSBudW0gLyAxMDsKCiAgICAgICAgd2hpbGUgKG51bSA+IDApIHsKICAgICAgICAgICAgaW50IGN1cnJlbnREaWdpdCA9IG51bSAlIDEwOwogICAgICAgICAgICBudW0gPSBudW0gLyAxMDsKCiAgICAgICAgICAgIGludCBkaWZmID0gTWF0aC5hYnMoY3VycmVudERpZ2l0IC0gcHJldkRpZ2l0KTsgIC8vIGZpbmRpbmcgZGlmZmVyZW5jZSBiZXR3ZWVuIGN1cnJlbnQgZGlnaXQgYW5kIHByZXZpb3VzIGRpZ2l0CiAgICAgICAgICAgIGlmIChkaWZmICE9IDEpIHsgIC8vIHJldHVybmluZyBmYWxzZSBhcyBkaWZmZXJlbmNlIGlzIG5vdCAxCiAgICAgICAgICAgICAgICByZXR1cm4gZmFsc2U7CiAgICAgICAgICAgIH0KCiAgICAgICAgICAgIHByZXZEaWdpdCA9IGN1cnJlbnREaWdpdDsgIC8vIG1ha2luZyBwcmV2RGlnaXQgZXF1YWwgdG8gY3VycmVudERpZ2l0CiAgICAgICAgfQogICAgICAgIHJldHVybiB0cnVlOyAgLy8gcmV0dXJuaW5nIHRydWUgYXQgdGhlIGVuZAogICAgfQoKICAgIHB1YmxpYyBzdGF0aWMgTGlzdDxJbnRlZ2VyPiBzb2x2ZShpbnQgbikgewogICAgICAgIExpc3Q8SW50ZWdlcj4ganVtcGluZ051bXMgPSBuZXcgQXJyYXlMaXN0PD4oKTsKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8PSBuOyBpKyspIHsgIC8vIGl0ZXJhdGluZyBvdmVyIGFsbCB0aGUgbnVtYmVycyBmcm9tIDAgdG8gbgogICAgICAgICAgICBpZiAoY2hlY2tKdW1waW5nTnVtYmVyKGkpKSB7ICAvLyBhZGRpbmcgbnVtYmVyIHRvIHRoZSByZXN1bHQgaWYgaXQgc2F0aXNmaWVzIHRoZSBjb25kaXRpb24KICAgICAgICAgICAgICAgIGp1bXBpbmdOdW1zLmFkZChpKTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICByZXR1cm4ganVtcGluZ051bXM7ICAvLyByZXR1cm5pbmcgdGhlIHJlc3VsdAogICAgfQoKICAgIHB1YmxpYyBzdGF0aWMgdm9pZCBtYWluKFN0cmluZ1tdIGFyZ3MpIHsKICAgICAgICBTY2FubmVyIHNjID0gbmV3IFNjYW5uZXIoU3lzdGVtLmluKTsKICAgICAgICBpbnQgbiA9IHNjLm5leHRJbnQoKTsKICAgICAgICBMaXN0PEludGVnZXI+IGFucyA9IHNvbHZlKG4pOwogICAgICAgIGZvciAoaW50IGkgOiBhbnMpIHsKICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludChpICsgIiAiKTsKICAgICAgICB9CiAgICAgICAgc2MuY2xvc2UoKTsKICAgIH0KfQo=
Solution Python
def check_jumping_number(num):
prev_digit = num % 10
num = num // 10
while num > 0:
current_digit = num % 10
num = num // 10
diff = abs(current_digit - prev_digit) # finding difference between current digit and previous digit
if diff != 1: # returning False as difference is not 1
return False
prev_digit = current_digit # making prev_digit equal to current_digit
return True # returning True at the end
def solve(n):
jumping_nums = []
for i in range(n + 1): # iterating over all the numbers from 0 to n
if check_jumping_number(i): # adding number to the result if it satisfies the condition
jumping_nums.append(i)
return jumping_nums # returning the result
if __name__ == "__main__":
n = int(input())
ans = solve(n)
print(" ".join(map(str, ans)))
ZGVmIGNoZWNrX2p1bXBpbmdfbnVtYmVyKG51bSk6DQogICAgcHJldl9kaWdpdCA9IG51bSAlIDEwDQogICAgbnVtID0gbnVtIC8vIDEwDQoNCiAgICB3aGlsZSBudW0gPiAwOg0KICAgICAgICBjdXJyZW50X2RpZ2l0ID0gbnVtICUgMTANCiAgICAgICAgbnVtID0gbnVtIC8vIDEwDQoNCiAgICAgICAgZGlmZiA9IGFicyhjdXJyZW50X2RpZ2l0IC0gcHJldl9kaWdpdCkgICMgZmluZGluZyBkaWZmZXJlbmNlIGJldHdlZW4gY3VycmVudCBkaWdpdCBhbmQgcHJldmlvdXMgZGlnaXQNCiAgICAgICAgaWYgZGlmZiAhPSAxOiAgIyByZXR1cm5pbmcgRmFsc2UgYXMgZGlmZmVyZW5jZSBpcyBub3QgMQ0KICAgICAgICAgICAgcmV0dXJuIEZhbHNlDQoNCiAgICAgICAgcHJldl9kaWdpdCA9IGN1cnJlbnRfZGlnaXQgICMgbWFraW5nIHByZXZfZGlnaXQgZXF1YWwgdG8gY3VycmVudF9kaWdpdA0KDQogICAgcmV0dXJuIFRydWUgICMgcmV0dXJuaW5nIFRydWUgYXQgdGhlIGVuZA0KDQoNCmRlZiBzb2x2ZShuKToNCiAgICBqdW1waW5nX251bXMgPSBbXQ0KICAgIGZvciBpIGluIHJhbmdlKG4gKyAxKTogICMgaXRlcmF0aW5nIG92ZXIgYWxsIHRoZSBudW1iZXJzIGZyb20gMCB0byBuDQogICAgICAgIGlmIGNoZWNrX2p1bXBpbmdfbnVtYmVyKGkpOiAgIyBhZGRpbmcgbnVtYmVyIHRvIHRoZSByZXN1bHQgaWYgaXQgc2F0aXNmaWVzIHRoZSBjb25kaXRpb24NCiAgICAgICAgICAgIGp1bXBpbmdfbnVtcy5hcHBlbmQoaSkNCiAgICByZXR1cm4ganVtcGluZ19udW1zICAjIHJldHVybmluZyB0aGUgcmVzdWx0DQoNCg0KaWYgX19uYW1lX18gPT0gIl9fbWFpbl9fIjoNCiAgICBuID0gaW50KGlucHV0KCkpDQogICAgYW5zID0gc29sdmUobikNCiAgICBwcmludCgiICIuam9pbihtYXAoc3RyLCBhbnMpKSkNCg==
Tips for Acing Tech Mahindra Coding Test
- Analyse the Syllabus
Begin by focusing on foundational programming concepts, including loops, conditions, and functions. Move on to advanced topics like dynamic programming and graph algorithms. - Practice Consistently
Regular coding practice on available online platforms and Tech Mahindra's official practice modules is key to improving speed and accuracy. - Solve Previous Year's Papers
Familiarising yourself with past Tech Mahindra coding questions offers insight into the difficulty level and frequently tested concepts. - Time Management
Simulate exam conditions while practising to learn effective time management and reduce stress during the actual test. - Focus on Debugging
Tech Mahindra often tests candidates’ debugging abilities. Practice identifying and fixing bugs in given code snippets to improve this skill.
Conclusion
Securing a position at Tech Mahindra demands strong coding skills and a strategic approach. Understanding the question patterns, practising consistently, and learning from past papers can significantly boost your chances. Embrace these insights and ace your coding test to take the first step toward an exciting career at Tech Mahindra.
Frequently Asked Questions (FAQs)
1. What level of coding questions does Tech Mahindra include in its test?
The difficulty ranges from beginner to advanced, depending on the role you’re applying for.
2. Which programming languages are accepted?
Tech Mahindra allows candidates to choose from languages such as C, C++, Java, and Python.
3. How many coding problems are included in the test?
Typically, there are 1-2 coding questions to be solved within 45-60 minutes.
4. Where can I access previous Tech Mahindra coding questions?
Official resources on the Tech Mahindra careers portal, along with trusted coding platforms, provide access to past questions.
5. What are the key coding topics to focus on?
Arrays, Strings, Dynamic Programming, and Binary Trees are frequently tested topics.
Disclaimer: While we strive for accuracy, we do not guarantee its completeness or reliability. Readers are encouraged to verify all facts and statistics from the official company website or check independently before making decisions.
Suggested reads:
- Accenture Coding Questions: 5 Coding Solutions for Freshers 2025
- HCL Coding Questions: Coding Pattern with Questions & Solutions
- TCS Ninja Coding Questions: Top 5 Coding Questions with Solution
- Deloitte Coding Questions: Top 5 Coding Questions for Freshers
- TCS Digital Coding Questions: Top 5 Coding Questions & Solutions
Instinctively, I fall for nature, music, humor, reading, writing, listening, traveling, observing, learning, unlearning, friendship, exercise, etc., all these from the cradle to the grave- that's ME! It's my irrefutable belief in the uniqueness of all. I'll vehemently defend your right to be your best while I expect the same from you!
Login to continue reading
And access exclusive content, personalized recommendations, and career-boosting opportunities.
Subscribe
to our newsletter
Blogs you need to hog!
How To Write Finance Cover Letter For Morgan Stanley (+Free Sample!)
55+ Data Structure Interview Questions For 2025 (Detailed Answers)
How To Negotiate Salary With HR: Tips And Insider Advice
Comments
Add comment