This content provides a pragmatic shortcut for corporate gatekeeping, reducing complex algorithmic thinking to standardized drills for exam clearance. It is a necessary survival kit that prioritizes rote pattern matching over deep engineering intuition.
深掘り
前提条件
- データがありません。
次のステップ
- データがありません。
深掘り
TCS NQT Coding Questions Solution (One Shot) | TCS NQT Preparation 2027 | TCS Actual PYQ 2026インデックス作成:
TCS NQT Mock Test Series : https://knacademycourses.graphy.com/courses/TCS-NQT-Mock-test--677fe71c30656545a7e88eaa TCS NQT Discussion Group 2027 : https://chat.whatsapp.com/CrXX2kOKx0y9Hi3M9tcJMv
Hey guys, welcome to this video. In this video, we will be talking about TCS NQT 2027 preparation. And this video is very very important for all of you because this is a oneshot video. And in this one short video, I am providing you the compilation the compilation of 2026 batch actual exam solutions. Okay. So basically in this video what you can expect. So TCS NQT is conducted in 2026.
Okay. And in this T TCS NQT actually many shifts in the many shifts the coding questions were medium to hard.
Okay. Were medium to hard. And in this video I will be providing you the compilation of the all solution. Please keep this in your mind that in the solution videos in the solution videos the introductions the introductions and the promotions will be very repetitive because this video is the compilation.
This is the compilation of different different videos uh of the solutions which TCS asked this year and we recorded the solutions on the same day.
In this video, I am combining all the videos of the solution and I am providing you the oneshot solution video for all the coding question. Yes, the introduction and the promotions can be repetitive but you can use this video because in this video the actual questions which got asked in 2026 we will be solving in this one complete video. Make sure to like this video. I am expecting at least 500 likes on this video. Also, you will be getting the link of the mock test series in the description for your 2027 preparation in which it is very important that you start your preparation as soon as possible. Uh uh you can purchase this course and as soon as possible you can start your preparation. Please join the WhatsApp group for 2027 batch. Also start with the daily practice coding sheets and the mock test paper. Let's start with the compilation video. Let's start. Hello my dear students. So in this video we will be solving TCS first shift exam coding question. It was the second coding question which was medium level asked by TCSQT today. Okay. So many candidates actually passed the 7 by7 test cases. But I just wanted to clarify one thing that in today's exam I have not seen the exam uh uh from my eyes right. So this question one student told me on the call. So I have written the exact question what he had told me on the call. So I am solving the question based on the inputs of the candidate. I have not seen the exact question but this was the candidate uh who told me this question. Okay. And if someone who have given today's exam, you can tell me in the comment section if it was same or not because whatever the candidate told me, I am solving that question. Okay. So basically this was the question which got asked. I will be discussing each and every aspect. Don't worry about it. Make sure to like this video. Make sure to subscribe this channel because we will be discussing a lot of things about TCS NQT on this channel. Also, if you have your exam on 26 or whatever you want to prepare. So, see today's exam was was the exact photocopy of the live test which we are providing. Okay. So, ignore everything.
Just go for the live test. The live test which we have provided here. A lot of live test are there which you can use for your practice. Okay. Reasoning and verbal is coming almost same which we are providing. Numeric is also almost same. Okay, which we are providing in the test. Numeric is coming hard. Verbal is medium level and reasoning is also coming like easy to medium. But like you will easily easily do verbal and reasoning. That's my guarantee. Okay, for numeric only it is little bit hard which is coming. Okay, that also you will do because the cutff will be less in that. Okay, we have already provided the left uh live test in which the questions are of TCS level only. Okay.
So, uh talk let's talk about this. Okay.
You are given n bankang transaction.
Each transaction has sender receiver amount time stamp. A transaction is considered fraud if sender receiver and amount are same. Time is spent uh time stamp difference is less than or equals to 60 seconds. Find and print all such fraud transactions. Okay. So, this is the question which student told me. Uh the example in input is given here. So, uh let's talk about this. Okay. So basically basically what this question is saying okay we are given the n bank transactions. Okay. N bank transactions will be there. So the value of n is given. Okay. We need to take the n transactions. Okay. Now each transaction has three things. Sender receiver amount and time stamp. Okay. So first one here anu. So Anu is sender.
Okay. John is receiver.
Okay. This is the amount like 200. Okay.
And this is the time stamp like uh 1 0 0 0. Okay. This is the time stamp. Okay.
These four things they have provided us.
Okay. First of all, we need to take input of these four things. Sender receiver amount and time stamp. Okay. A transaction is considered fraud if the sender receiver amounts are same or the time stamp difference is less than equals to 60 seconds. Find and print all the fraud transactions. Okay. So if you will see this closely see the first transaction and second transaction the sender name is same the receiver name is same the amount is same and the time difference if you will see here is less than 60 seconds right it is 1,000 it is 150 so it's a fraud transaction both are fraud okay next one this Ram sam 300 2000 okay again this is Ram arm sam 3000 uh 3002 uh 250 again both same okay the time difference if you will see is less than equals to 60 right so that's why it is also fraud only transaction is this one if you will look closely here ano john two 10050 and 2000 this is a legit transaction because if you will see there is no duplicate transactions there is no transaction with less than 60 seconds difference.
Okay. And uh everything is fine here. So only if you will see this is the legit transaction. Now I don't know what we need to print in the output. Maybe the fraud transactions. So I am printing here the fraud transactions in the output. Okay. In the actual exam but this was the question. Okay. Now how will we be doing this question? There are total two aspects. The first thing we need to take the input. Okay.
If the input is given like this you can take the input in two styles. Right? So first one I am assuming they are providing the input in the right manner.
Okay. If they are providing the input in the right manner what we can do is we can build a structure for this kind of input. Okay. First thing is this.
Suppose they are providing this in the string. So suppose the input is uh provided as a string then also we have discussed about it right. What we will do is see we will create a variable. Okay, that is f first is suppose a string sender.
Second one is string receiver.
Third one uh we can create double amount.
Okay, another one we can create double time stamp.
Okay. So these four variables we will create and suppose this is the string right. We will split this string based on spaces.
We will split the string based on spaces. And after that the first one we will store in sender. Second one we will store in receiver. Third one we will store in uh amount and fourth one we will be storing in time stamp. So these kind of like four variables we can create. Okay. These kind of four variables we can create and whatever we are getting in the input we can one by one split them okay we can store them in different different like strings then after that we can create uh like uh a separate arrays for these right this is the sender this is the receiver uh this kind of separate arrays we can create but it will create a complicated I think it will create a complicated uh uh uh structure so I think the better thing is After splitting the input directly don't store them in the individual variables we can create a structure for that okay the same kind of a structure we can create okay so if you know how to create the structures if you are using C++ Java or whatever language you are using I think this question will be easy to you okay so let's start first of all we create the structure uh in in C++ okay one structure we are creating that is transaction okay In transaction we are storing sender name, receiver name as a string. In the double we are storing the amount and in the long long we are storing the time. Okay. Whatever uh based on the constants you can take the variables. Okay. After that we are taking the input of n the number of transactions. Okay. After taking the number of transactions we are creating one vector transaction. Okay. In this uh vector transaction what we are storing?
We are basically storing if you can see here we are taking the input. Okay. So after taking the input, this is the vector uh uh uh of transaction type. The name is T. Okay. So in this uh T vector, the type if you are seeing here, we we have provided as transaction. So the type is this one which is transaction structure, right? It will be having sender, receiver, amount and time. Okay, these four things will be there in this structure in this array. Okay. So we are taking the input. In the input first of all we are storing the sender name. Then then we are storing the receiver name.
Then we are storing the amount and the then we are storing in the time. So suppose this is the uh this is the input which is Anu John 200 and 1,000. Suppose this is our input. So first of all we are storing in T of I dot sender. So the first one which is Anu that will be stored in sender. Second one is John it will be stored in receiver. Third one is 200 which will be stored in amount. Fourth one is 1,000 which we will store in time. So our input is there in vector t.
Okay. So after storing that our vector t will be looking like this. Okay. There will be sender here, receiver here, amount here and there will be a transaction here and there will be some columns. So this kind of a structure will be there. In the sender it will be anu then there will in the receiver it will be John. In the amount it will be 200 in the time stamp it will be 1,000. This kind of structure will be there and more inputs we will be taking the more structures we will be uh storing like this only. And this will be t of zero. Then for t of one also it will be the same structure with the different values. Okay. Then we are creating one vector which is fraud.
Okay. In this vector basic basically it's a boolean vector in which we will be storing if the transaction is fraud or not. Okay. Then what we need to do is we need to compare all the transactions.
See I am not doing the optimization here. Why? Because I think the optimization is not needed in the question. Okay. If it was needed I will create one separate question and I will provide the optimized code. But I think without optimized code was working in this like two two loop code was working here. So that's why I did not optimize here. Okay. So basically what we will be doing here we will be comparing all the rows right all the transactions. So we will be starting one loop from i= to0 to i is less than n. Second loop we will be starting from j which will be starting from i + 1 that means from next transaction we will be starting okay j is less than n and j ++. What we will be comparing T of I dot sender is equals to T of J dot sender and then T of I receiver is equals to equals to T uh T of J of receiver and then T of I of amount is equals to T of G of amount and absolute difference between T of I time minus T of J dot time is less than or equals to 60. Okay. So if the amount is same okay the difference will be zero.
Sorry uh uh if the time is same the difference will be zero. Okay. And also if the time stamp is less than or equals to 60. So this kind of absolute uh uh difference will work. Also we need to compare if everything is same like the sender name. So T of I equals to sender is equals to T of J or sender is not. So we are comparing two transaction right?
One transaction is on place. Second transaction is on J place. Right? We are comparing two transactions here like we do in SQL. Right? I hope you you guys have written these kind of codes in SQL also. So uh we are comparing if everything is same if the sender name is same if the receiver name is same. If the amount is same and if the absolute difference between the time is less than or equals to 60 then in the if index of fraud uh uh um uh vector we will be storing true and the J index also we will be storing true that the both transactions are fraud. Okay. Then simply we got the fraud transactions.
Okay. After that in the see out we will be printing the fraud transactions. So we will be putting a loop like this.
Okay. Then we will be checking if the fraud fraud I that means if it is true okay we will be printing the transaction like this that ti sender ti dot receiver ti dot amount and ti time. So this way we can print our fraud transaction. Now I don't know what was the exact output that got asked in TCS but this is the main code okay which they asked today. I hope the question is clear how we are comparing each record how we are seeing the time difference how we are storing the data. Okay. So this code is in C++ for right now. Okay. uh you can take the screenshot or you can write the code and you can ask the chat GPT to convert it into Java Python if you want. Okay. Uh so this this was the logic okay I hope the logic is clear how we are storing the things how we are doing the things okay everything is clear and if you have done this question how many test case you have passed you can tell me in the comment section thank you so much for watching hi everyone so in this video we will be solving the both coding question from TCSQT which got asked in the first shift today morning in TSNQ exam. Okay, why we are solving the both question?
One question I already solved before but we are solving both question because in the last video some candidates uh told me that they asked we need to solve it using hashmap but that is not the case.
Okay, you just need to pass the test cases. They mentioned that it is recommended to use hashmap. It is not like you only need to uh use hashmap.
Why they have mentioned that? Because the input was complicated. So they just mentioned the notes so that you can think about how we can store the input right. So it is not uh like that it is mandatory that you should use hashmap but in this video we will be using the hashmap solution also also that was little complicated. The first question is very very easy before going with it uh I just wanted to talk about the mock test series. Uh so as you guys already know the both questions of today were very very easy. Aptitude questions were very similar to the live test which we have provided in our course. So there are a lot of live mock tests which you can see in our course. Okay. If you are purchasing the series the discount code also you will be getting in the description. The main thing is um the aptitude question which got asked today was very very similar. So uh if you want you can purchase the TCSQ mock series.
The link will be in the description and um uh through that you can basically practice okay that what kind of paper you can expect in your actual exam. Let us start with the first question. So the first question says a gym offers membership for 3 months. It's uh the 5,000 cost for 6 months is 7,000 cost for 9 months is 12,000 cost for 12 months is 15,000 cost. Okay. Given an input number of months written the total cost if the input cannot be formed using this plan sprint error. Okay. So basically what uh we need to do in this question basically see they are saying the cost are uh the cost are respective to the months uh which they have mentioned. The ba basic thing is if we can make the plan or not. Okay. So suppose if if someone ask if I can a 15 months plan. Yes, I can make a 15-month plan. Okay, I can use the 3month plan five times. Okay, 3 month plan. Five times I can use. Right. If someone will ask me can I uh make uh 18 months plan?
Yes, I have multiple ways. I can use 3 months plan six time, 6 months plan three time, 9 months plan two times.
Right? So what we need to check is basically if the number which are which they are providing in the input is the multiple of it or not. Okay. If it is a multiple of this then definitely we can form a plan. If it is not a multiple of it definitely we cannot form the plan.
Okay. So very simple if else based question. Okay. So uh suppose if someone says u I don't know what uh uh they wanted to print but if someone says uh we need um uh uh basically um uh a 24 months plan. Okay.
If we need a 24 months plan what will be the output? Okay. So for a 24 months plan simply it was uh if you can see here it's 15,000. Okay. 15,000 uh uh multiply by 2 okay which is 30,000 rupees okay so that thing we can print I don't know what they wanted to print but this kind of output we can give okay so uh basically we can directly use the if else's condition to solve this question let's solve this question very very easy question okay so uh again we will take the input of n which is the total number of months if the n is divisible by 12 which is modulus by 12 is equals to 0 we will print n / L multiply by 15. This will be the plan cost. Else if we will be using if it is divisible by 9, we will be multiplying by 12 12,000.
Otherwise, we will be uh multiplying it with 7,000 or 5,000. Okay. Depending upon what what is the plan. Okay. Else we will be printing the error if the plan cannot be formed. Okay. And I think this solution passed 7 by seven test cases or maybe 6x7 test cases. Okay.
This solution passed these many test.
Even if the solution uh if the candidates are printing direct like see out 6,000 see out 7,000 according to the plan okay because I think out of seven test cases six test cases were like this only in which you can directly print the amount also there was no multiple okay so it will just uh pass six out of seven test cases okay very simple easy if else based question next question which I wanted to discuss okay so I hope question you guys already know if you don't know I will just explain There are multiple transactions given. Okay, we will be solving using map approach in this video. Suppose a transaction says Anu, Ram 200 and 1,000. Okay. So there will be some transaction given uh in this format in which the sender name is given here.
So this is sender, this is receiver, this is the receiver name, this is the amount and this is the time. Okay, we need to find out the fraud transaction.
The fraud transactions are if if the transaction is same. So suppose again it is Anu Ram 2,000 uh sorry 200 and 1,000 both transactions are same. So this is a fraud transaction or the difference between the time stamp is less than 60 seconds. So suppose it is Anu Ram 200 and 1,50. So difference between 1,000 and 1,50 is 50 seconds, right? So again this is a fraud transaction. So we need to print out a fraud transactions. Okay.
If uh if if the transaction is genuine suppose what will be the genuine transaction that will be suppose Anu Ram suppose it is 200 but suppose it is 2,000 so timestamp difference you can see here okay so timestamp difference is a lot it is not within 60 okay we need to only uh give the front transaction if the values of the transaction is same or the time stamp is less than 60 Okay. So basically this is the question. Now see for solving this question our approach will be very simple. The output of the transactions are given like this. So suppose n transactions are given like this. Okay. Or we need to print the fraud transactions.
Suppose five transactions are given.
Okay. And suppose the transaction uh is like suppose the sender is a receiver is b the amount is 200 and first time stamp is 1,000. Second one is A B 200 and 1050. Third one is B C 100 and 2,000.
Okay. So these kind of transactions uh will be given here. Okay. First of all, all these transactions are given in string. So we need to take the input first. Okay. Take the input first and we need to store it in a proper data structure. Right? so that we can store it in a map properly. Then we will solve this question. So let's start. First of all, we will be conduct uh creating one structure. Okay. In that structure, we will be storing the sender uh receiver.
See, if you want, you can eliminate the structure as well. You can directly sol uh store that in variables and that variables you can use. But here I am using a structure just for a better code quality. Okay. So two strings we are taking sender and receiver in which we will be storing the names. Then amount and the time. Okay. Then we are storing the total number of transaction. Here we are taking the input of the total number of transactions.
Then we are creating one vector which will be storing the transactions. Okay.
Now the vector type is transaction. The vector type is transaction. Okay. And this transaction type will hold two strings. One is sender, one is receiver.
two um one double which is amount and one long long which is time. Okay, that we will be storing here. So now we will be uh running one loop. Okay, after running the loop we will be reading the line. So a string line we have created get line we will be use. Okay, because it is in strings then we will be using the string stream. Okay, then after using the string stream we will be splitting it. Okay, then we will be storing basically T of sender receiver amount and time. Okay, all the four things. Okay, we are storing in transactions of i index. So suppose in the zero index, what will store here? A as a sender, b as receiver, 200 is amount and 1,000. Okay, that is stored like this. So in t of zero, this will be stored in t of one in t of 1 the another transaction like a b 200 and 1,50 will be stored. Okay. So this is our teeth teeth basically uh this is our T vector which is individually uh storing all the structure. So you can say this is all the structure. So I can say these all are in the middle bracket and there's a comma between these structures. Okay. So this kind of format is there. Okay. So we are storing this. Okay. So we are storing like this. Say this is our full array. The first structure is here. Then comma then second structure is here. So this is t of zero. Then this is t of one. Okay. This kind of a structure we are storing in our complete array. Okay.
Then we are creating one map for basically storing it and optimizing the code. Okay. How we'll be uh using that map for storing this. So first of all I'm creating one map. The string the string will store the whole key. Okay.
And the value will be storing the time stamp. How? See suppose this is the transaction which is a b and 200 is amount and 1,000 is time stamp. Okay.
First we need to check first we need to check if the transaction is same.
[snorts] So suppose if the transaction is same that is completely fine but the transition if the suppose transition is different then we need to check the difference difference between the time stamps right we need to check the difference between the time stamps. So only the difference between the time stamps we will be needing right. So we can store the time stamp as value and we can store all these three parts which is a b and 200 because we will be not needing this as key. So we will be storing in our map like this. So suppose the first record will be stored like this is the key which is a pipe b pipe 200. This is the key and the value which it it will restore is the time stamp which is 1,000. Second key will be suppose a pipe b pipe 200 and the time stamp is 1050. So in this structure we will be storing our map. So the key will be sender pipe receiver pipe amount. Okay. So we will be uh running one loop. Okay. Here we are extracting. So we are forming the key.
How we are forming the key? T of I dot sender plus pipe T of I dot receiver pipe plus two string whatever the amount is that and in that key we are pushing we are pushing okay so in the map we are pushing what thing we are pushing we are pushing the time stamp and the index because index also we will be needing okay if we want to print the transaction that then we will be needing the index also so we are pushing the transaction time and the index for that we are creating the map like this so the structure There is a string. This string will be holding the key. Okay, the whole key. And we want to store the time stamp plus the index. So time stamp is in long long. So we will be creating one vector which will be storing the pair of long long, int. This int will be storing the index. Okay. So this will be our code.
Then this vector we will be creating which will be storing the fraud transactions which transactions are fraud. Okay. Then we will be running one loop on our map. Okay, one loop we will be running on our map and we will be storing the timestamp and the index whatever the index is. So suppose this is the first index, this is the second index. This is a vector, right? This is a vector. So basically the keys vector, right? So we are running the loop on the map and we are storing the vector here.
Okay, of the loop. So it dot second we are storing the loop here. Okay, don't worry about the syntax. If you know about the maps definitely I think you guys know about the syntax. Okay. Then we will be uh sorting our vector. Okay.
So suppose see how our map will be looking like. I'll just explain you.
Suppose there are uh four transactions.
Okay. A comma b comma 20,000. Okay. A comma b comma 200 comma 1050. A comma b comma 200 comma 2,000. Okay. Then B comma C comma 200 comma 2500 how our map will be looking like okay these are the transaction first key will be see this is the key A pipe B pipe 200 okay and this key will be storing what 1,00 and the index like 1 second it will be storing because key will be unique right so it will be storing 1050 2 third it will be storing 2,00 3 okay with index it is storing the time stamp another key which is B comma C 200 it will be storing 2,500 okay that's how our map will be looking like okay so now we are sorting the timestamps right we are sorting the timestamps why we are sorting this vector because see we want the difference should be between 60 seconds right the different if the difference is between 60 seconds then definitely that is a fraud transaction. So we are sorting that we are sorting the timestamps right. Then we are running we are initializing the value of j as0. We are starting our loop okay uh uh i uh from index uh zero we are running a loop on our timestamps. Okay. And we are checking if if the value okay in the vector we are storing the time stamp right. So we are checking if if please listen carefully. If the time stamp is greater than 60. Okay. If the time stamp is greater than 60 then we are doing J++. Why we are doing that? Okay. See after sorting suppose my J is pointing out here. Okay. I am on this index.
Right. If all the transactions who are coming into the next 60 seconds I can ignore them. Right? because all those transactions will be fraud transactions right so suppose I am on 1,000 time stamp I can say any transaction that is genuine okay that transaction will be after 1,60 right because 60 seconds gap should be there okay that's why what we are doing if if the ve of I do first minus ve of J dot first we are subtracting we are subtracting the difference and we are checking if If it is greater than 60 okay then we are increasing our index otherwise we will store all other transaction which is coming in between okay all the in between transactions we will be storing that in as a fraud transaction. So in our fraud in our fraud vector we will be storing those transactions as uh uh uh fraud. Okay. And we will be making the boolean index as true. Okay. So this is the basic code which we are doing. And at last we will be printing all our fraud transactions. Now see I will be giving you one summary like what we have done. Okay what we have done please listen the summary because it might get confusing because my previous solution was very easy that's why I uploaded the previous solution but some candidates are asking we want the map solution. Map solution was little bit complicated.
Okay because here we are using one additional data structure for optimizing the code.
Okay. So now listen carefully what we are doing. Basically suppose in our map this is the key which is a b 200. Okay.
Then it is storing suppose 1,00 1,00 uh 40,50 and 2,000. Okay. Suppose I am starting from here. So my J index will point out here which is zero index. Okay. So what I will be doing here is I will be looping out from the next index and what I will be seeing if the difference is greater than 60. Okay. So if I will check if the difference is greater than 60. So is the difference greater than 60? No, it is 40. Okay. So I will go on the another index though. So my I will increase right? My I will increase to this index then this index. Now I will again check if the difference is greater than 60. No it is not greater than 60.
again my I will increase to this point.
Okay. Now when I will reach to this point I will see the difference is 1,000 that is greater than 60. Okay. Now what I will do is I will do J++. Okay. So my transaction from here it will move out to here. Okay. From my transaction here I will move out here. Okay. So I will move out to next index. Okay. So this is the basic thing which we are doing.
Okay. We are ignoring all the transaction which are coming in between.
Okay, which are coming in between we are ignoring all the transactions. Okay, and that's how that's how all our fraud transaction we are storing. If any transaction is fraud that we will be storing okay even if the transaction is same it will be storing after that.
Okay. So all the transaction which is coming in the range of 60 we are ignoring that. Okay. And we are storing that in our fraud fraud array. Okay.
Fraud boolean array. I hope this is clear. If it is not clear, you can re-watch the video or you can check my previous solution. Okay? So then it will clear. It will make more sense. Okay? Or you can re-watch the video because it was little complicated to explain this.
Okay? If I will write down the code, it was easy. But to explaining it was little complicated. I tried my best. I hope you guys got it. Thank you so much for watching. Hi everyone. So in this qu uh video, we will be solving 21st March second question because most of the students did not solve this question today. Okay? because they did not pass the test cases in this question. It was the tough question. You can say uh students uh find difficulties in solving this question. So I am solving this question. Okay. Also I am solving this question with the guidance of the student who solved the 7 by seven test cases in this question. Okay. So only one student I found who passed 7 by seven test cases in this question. Okay.
So what uh what is the approach he told me based on his approach I'm solving this question. Okay. So why I'm telling you guys this because see I have not seen the question from my eyes. So I will ask the question uh I I will ask this to the student like how you have solved this because today actually I asked to the many candidates like how you solved this question but they actually did not pass the test cases.
only one student I got who passed all the test cases in this question that's why I'm using his approach to solving this question okay so first of all make sure to like this video and second thing before starting the question today this kind of response I got from the students okay so thank you so much sir all because of your course thesis and prep thank you so much for providing such content the level of numeric question was same okay again the next response yes sir thanks for your help and structure guidelines throughout the preparation. Again he did very great in the exam. Okay. And his approach only I will be uh using in this question. Again uh uh numeric I will get 12 plus verbal 15 plus reasoning 13 to 14 advanced coding one question. Thanks for your effort sir. Okay everyone today appreciated me. I like I am in sky today because his friends are appreciating me a lot today. So thank you so much. Okay.
coding thank you reasoning English same mock test everyone is appreciating thank you sir it's all because of your course okay so today actually I am glad that students are appreh appreciating so much our effort because on the result day only okay right and still if you have your TCS this exam if you I know you have very less time to prepare I would suggest to focus on these live test they are helping a lot of students okay so you can just go in this section which is TCSQT actual exam live test okay in this if you can see here there are a lot of live test provided so you can use them for your practice and definitely they will helping you out for clearing your foundation section okay the link of the mock test series will be in the description let's start with this question so the Question says okay there will be total n number of soldier we need to place the soldier there are the values array given there the end number given each soldier number chosen from the value array no two adjacent soldier have the same number the first soldier must be having the number one the last soldier must be having the number end find the total number of valid arrangements okay so you will not understand the the question like this I will be explaining you so basically they are saying the there is a number given which is n= to 4 okay n= to four. That means total four values will be given to you like 1 2 3 4. Okay. So n is telling us the size of values array. Okay. Now one n number will be given to us. Now they are saying the first soldier will be always one. Okay. The last soldier will be the element which they have provided here in the end. So suppose one is here and three is here. Okay. Because three will be the last soldier. Now we need to find all the val valid arrangements right and the final output is seven. The condition is there can be no two soldiers who are adjacent and the number is same.
For example 1 2 4 3. This arrangement is valid. But if I will say 1143 then this arrangement is not valid because two soldiers are adjacent right that's why so seven valid arrangements will be suppose one is here three is here okay so let's use one can we use one here no so we can use another number can we use one here yes we can use one here okay in between we can use two In between we can use four right. So 1 4 1 3 these are the two valid arrangements.
Now again starting from one ending from last. These are the fixed two positions.
Okay. Now two I can choose here and I can choose here. Right? So if I will choose here two I can choose four here.
Then again I can choose this arrangement. There will be 1 4 2 3.
These are the two arrangements. Okay.
This way we can find seven arrangements like this. 1 2 1 3 1 2 4 3 1 3 1 3 1 3 2 3 1 3 4 3 uh 1 4 1 3 and 1 4 2 3. Okay, these will be our seven valid arrangements. Okay, in which no two soldiers are adjacent. Okay, and these are the valid arrangements. So output is seven. Okay, so this was the question.
Okay. Now most of the question uh most of the students today did not pass the test cases in this. Okay. I will be telling you the reason also that student told me that why most of the students failed to pass the test cases. Okay. So see let's start with the approach. How we'll be how we will be solving this question. Approach is pretty easy. We will be directly using recursion. There is no need to use DP here because the recursion solution passed all the test cases.
So we need to find the valid sequence right. At the starting position we will fix one. At the ending position we will be fixing three. Okay.
And now we will run our recursion solution. How we will run our recursion solution? We will be running [clears throat] a loop. We will be running a loop for all numbers. For all numbers from the value as array. Okay, we will be fixing our index and we will be trying all the numbers there if the number can be uh can be fit there or not. For an example, we are running a loop on this array and suppose our index is this one. Okay, we will try with one here. So suppose one is here. Okay, so can we try one here? No, because the last number is same, right? So current index number should be not same as index minus one. Right? So one cannot be here.
Okay. So we will not place one here.
Then two. Can we place two here? Yes. So two we can place here. And then we will pass this arrangement. Okay. To the next recursion. And we will do this index is fixed right. So we will do index + one. So because for next index we need to find the another solution.
Okay. Now we will be having two choices.
Either we can use this. Okay.
Suppose at this index we are using two.
Okay. Or we can use another number.
Suppose three. We can use here three also. Okay. So that way our recursion will work and how our recursion tree will look like. I will make a recursion tree. So it will be easy to you to find out how our recursion is working. Okay.
So this is our recursion tree. One is fixed. Okay. One is fixed. Three is fixed. Okay. One is the starting number.
three is the uh uh last number. So our recursion tree is working like this. So for first sub for first index okay we will be using numbers from the values array 1 2 3 and four. Okay there we will be using all the values and we will be checking if we can proceed with it or not. So at the first index we will be trying one.
This is the first solution. We will be trying two. This is the another solution. We will be trying three.
This will be the another solution. And we will be trying four. So 1 4 3. And these are the blank spaces. Now these are the all possible four uh values which we can place right. Then we will expand the tree. So if you will see here the index the index is not matching.
Like if you can see here the index is not matching index and index minus one is same. So this solution is not valid we will drop it. Suppose this is a valid solution then again we will expand the four solution here. Okay. For the next place that means for index + one. So currently it was for first index then it will be for second index. There will be four solution. 1 2 1 3 1 2 2 3 1 2 3 3 1 2 4 3 will be the four possible solution. This can be a valid arrangement. Okay, this is fine. And now we don't have any index. So count ++ okay then like this is the one valid arrangement. Then 1 2 2 3 again both index are same. Drop this solution. 33 both are same. Again drop this solution.
1 2 4 3 this is the valid arrangement.
So count plus+ again this is the valid arrangement. So this kind of recursion tree we will be using for each and every number we will be expanding our tree and we will be counting the arrangements.
This will will be our solution approach.
I hope you guys got it. Okay. So for that okay how we are writing the code.
See first we are taking the input of n.
You can use c in n and then then you can run a for loop and you can take the values in the input. Then at the end you can use c in end. Right? These are the three values which we need to take as input. Then we will be passing that in our count arrangements function n values and end. Okay? So all these three values we are passing here. Okay. Now this is the basic thing which I wanted to discuss. Okay. So now at the first index we are assigning one. At the last index we are assigning our ending number which is three as given in the input. Our starting count is zero. Number of possible arrangements. Then in the solve function we are passing one because from one index we need to start uh the filling of the arrangements. We are passing our array. We are passing values count and n. Okay. Everything we are passing. This is our arrangement array.
Okay. This is our arrangement array.
This is our values array. This is our count of the total number of uh arrangements and this is the size. Okay.
Then this is the function. Right. So first we are checking our test cases.
Okay. The first base case the base case is okay if the index is equals to n minus one that means we reached we reached to the n minus one position okay if we reached to the n minus one position what we will be checking if index is not equals to index minus one.
Okay, if the index is not equals to n uh index minus one, that means both numbers are not same, then we will be doing count++, right? This is the thing which we need to do because the no adjacency two numbers can be same. So we will be doing count++. This is our base case, right? And we will be returning. Okay.
Next, we will try all our values for filling the arrangement which we discussed in the approach. So we will be starting our loop from zero to the values dot size. Right? So this loop we will be uh continuing we will be assigning the uh one by one all the values in the well uh uh variable then we will be checking if well is not equals to a r arrangement of index minus one. Okay so if both are same or not if both are same then we will be dropping.
So uh then we will be not using the recursion logic and we will be dropping that value right both should be not same the current value and the index minus one right. So we are checking this condition. Then at the current index we will be assigning that value which we have discussed. So suppose this is our arrangement 1 and three. Okay. So in the loop suppose first we have one. So now these are our value right? 1 2 3 4. So for this place one came. So this condition will fail because one and one these both are same index minus one right. So this will drop. Now there is two right? So we will place two here. So a ar r r of index will become two. So this will become two and then we will pass this this arrangement for the next index. So this index we will be filling right. So we will be passing index + one a r r values count and n. So this way we will be getting all our solution through recursion. The main thing why the test cases were failing. The student told me everywhere we need to use the pass by reference. Okay pass by reference we need to use everywhere. If we are not using the pass by reference everywhere then the test cases were failing in the actual exam and this was the actual solution which the student did and he passed all the test cases. Okay, I hope the solution is clear. I hope the question is clear. Thank you so much for watching this video. If you want to prepare you can purchase the TCSQ mock test series. The link will be in the description. Hi everyone. So start we will be solving the question.
This was the second question and I was working so hard for TCS.
hectic tensions. So it was so hard but today I am relaxed. So if you have your exam on 26th April, so you can purchase our TCS mock test series question sheets.
previous question.
So you can purchase the mock series and at least 100 question for each subject which you can use for your practice.
Okay. So link of the mock series will be in the description start one by one. So question you are given n numbers of person w the weight of person and y is the maximum capacity of the balloon find the maximum number of people that can fly in the balloon such that the total weight does not exceed y I don't know question number 40, 50, 60, 30, and 20. And 100 max, right?
Right.
Simple question.
So I think rightals to right functional or right sort function.
Step right increase 50 to 50 break final answer.
So I hope next video. Thank you so much for watching. Hi everyone. So in this video we will be discussing about the coding question that got asked in today's shift 26th March. So we will be discussing about the coding question in this video.
Okay. Also make sure to like this video.
Also one thing that you will be getting the link of the mock test series in the description. So you can purchase the mock test series. The major thing is all the test uh which you will be getting the mock test. Okay. The level will be very very similar to TCS and so you can attempt the live test. After attempting the live test, you will be thanking uh you will be seeing your rankings. Based on your rankings, you can judge yourself that what ranking you are getting, what ranking other students are getting.
Based on that, you can judge your performance. Okay? So these live test will be helping you out. So you can purchase the TCSQ mock test series if you have your exam on 1st or 3rd April.
And there are a lot of mock test and live test are available for practice.
Let's solve this question which got asked in uh today's shift. The this question is not difficult. Okay, let's talk about this. First of all, the question is saying maximum product subset which is saying you are given an array of integers can include a negative 0 and positive. We need to find the maximum product possible by multiplying any subset of elements. So suppose this is the array given. So here we can choose any elements. We need to find out the maximum product from this array.
Okay. So if you will see the uh these elements the maximum product will be 4 * 3 * 2. Okay. So that will be 24. Okay.
Now let's discuss about this. Okay. We can do this. But how we are doing this?
If we if we can do this in maths then definitely we can do this in coding as well. Okay. So first of all we will try to understand that with the mathematics how we will be doing this. Okay. Suppose this is the array 1 2 3 4. Okay. What we what we can do here is all the elements are positive. So if all elements are positive, what we need to do is we will multiply all the elements and that will that will give us our answer. Like um if you will calculate this this is 24 that that's what we can get as maximum. Okay.
Suppose there is zero here 0 2 3 4. So we can if there is any zeros we can ignore the zeros and we can multiply all other positive elements all positive elements. Okay that will give us our answer. Okay. Now these two conditions are sorted and solved. Okay. Now coming to the next condition. Suppose there is suppose there is odd number of odd number of negative elements. For an example u there is -1 -2 -3 1 2 and 3. Okay.
Suppose here we need to find out the maximum product from this array. Okay.
What will be the maximum product? Let's solve it. Let let's solve this with maths. If we can solve this with maths definitely we can solve this with coding. Okay. So that's how you actually need to think in your exam. Don't write the logic first. Please think in your mind first via maths. After solving this question using maths, you can solve in coding. Okay. So if you want to get the maximum product from here, we will multiply all the elements from positive elements. Right? So that's 3 multiply by 2 multiply by 1. Okay. Now from negative elements, first of all, if there are total odd number of elements, we will be ignoring one one element. So it will make it even.
And which element we will be ignoring?
We will be ignoring the maximum value from negative. Right? So the maximum value from the negative element is minus one. Okay? If we will include this, it will not give us maximum product because if we want to get the maximum product, we will be multiplying with the smallest elements of negative, right? So that will be minus3 and minus2. Okay? So multiply by -3 and -2. This will give us the maximum product from this array.
Right? So it is 6 * 6 that is 36. So 36 will be the answer. So see now it comes a very important point a very important point. Okay. If we have total even number of negative numbers even number of negative numbers then the minus minus will get cancel. Okay. So what we can do is if we have even number of negative numbers we will be multiplying all the elements and we will be ignoring the zero. Okay we will be multiplying all the positive elements we will be multiplying all the negative element uh negative elements and uh we will be ignoring the zero. That way we can get the maximum product. Only condition which we need to use is okay which will be different okay because we will be doing all multiplication after ignoring the zero for either if the all elements are positive all elements are positive and the element count of the negative elements are even. Okay. But only one condition which we need to use is if the negative elements are odd then what we need to do is we need to remove the maximum element from negative okay and then we will be multiplying all the elements okay that's what we did in the example right so that that will be our logic I think the logic is clear okay so it's simple first of all we will be storing the array size on n okay if n is equals to equals to one return ar of only one element is there we will be returning the first element itself.
Okay. Now for storing the maximum negative element if you remember in our code what we have stored uh we have removed the negative element uh the maximum element from negative right that we will be storing in this max negative.
Okay max in max negative we will be storing that element. Okay. So uh the also we will be storing the count of the negative elements. Okay. Because just we have discussed we will be needing two things. The count of the negative elements if it is even or odd. Okay. And we will be also storing the maximum negative element. So if the count is odd so we can remove that element. Okay. And the counts of zero as well we will be storing. Okay. And the product the final product value we will be storing here in the product. Okay. Now we will be looping our array. Okay. If the current element is zero, we will be increasing the count of zero and we will continue our loop. Okay, if the element is less than zero, okay, if the element is less than zero, then we will be increasing the count of the negative element. Okay, because we need to count how many negative elements are there, if they are odd or even. Okay, also we will be checking if this element is the max negative element. So we will be storing the value of max negative element by doing max of max negative comma x. Okay.
So that way we can store we can check all the elements and we can store the value of the maximum negative element.
Okay. Then after this condition we can multiply the value of all elements. Right? So put multiply by x. Okay? It doesn't matter if it is a positive element, if it is a negative element. Okay? We are multiplying each and every uh element.
Right? We are multiplying each and every element. It doesn't matter if it is negative or positive. Okay. At last we will be checking the count. Based on that we will be doing that. Okay. So that way we will be getting the whole product. Okay. After getting the whole uh product we will be checking if the count of 0 is equals to equals to n.
That means if all elements are zero.
Okay. If all elements are are zero we will be returning zero. Okay. We will be returning zero. If the count of 0 is equals to equals to n. If the all elements are zero we will be returning zero right as our answer. Then we will be checking if the count of negative element. Okay. If the count of negative element is odd. Okay. If the count of negative element is odd. Okay. Then we will be checking if then we are checking one more special case. Okay. If the count of negative uh negative element is one, that means only one negative element is there. Okay. And count of zero plus count of negative is equals to n. Okay. Count of zero plus count of negative element is equals to n. then also we will be returning that uh uh the value as zero. Okay, then also we will be returning the value of Z uh as zero.
Okay, because if there is only one negative element, okay, if there is only one negative element and all the other elements are zero. So the maximum value which we get from the array that will be zero only. Okay, so that's why we will be directly returning zero. Okay. After that, after that, okay, if if the negative element count is odd, okay, what we need to do is whatever the maximum element is, we multiplied all the elements here, right? So, product multiply by X we did, right? So, we will divide our product by the max negative because we need to remove that element, right? So, we will be dividing that by max negative. Okay? Then at final, we will be returning our product. Okay? So by this logic what we are doing here I will just explain you in summary.
Suppose this is our array -2 -3 -4 2 3 4 Okay. So we are multiplying all the elements. Okay. So we are doing -2 multiply by -3 multiply by -4. Okay.
Multiply by 2 multiply by 3 mult*ly by 4. We did this. Okay. So after that we will be uh actually calculating the product. Okay. So it will be 24 * 24.
Okay. So it will be minus 24 multiply by 24. Okay. This will be our product value. Okay. Now after computing this we will be checking the count of negative element. So count of negative element here is three. Right? If the count of negative element is three, we will be storing the maximum negative element also. So the maximum negative element is uh is minus2 here. Right? Minus2 here.
So we will be doing divided by minus2.
So it will be 12. So 12 * 24. Okay. This will be our final answer. Okay, this will be our final answer. And even if you will look here, if you want to find out the maximum answer from this, okay, definitely we will be ignoring the the smallest negative element or the maximum element. Okay, from here and whatever the count is, we will be getting that will be our final answer. Okay, so that that's what we are doing. Also, we are tackling some special cases so that all test cases should pass. Okay, so the question is simple. If you can solve this question using maths which we have discussed right uh like multiplying all the positive element and multiplying all the negative element as well and if the count is even no problem at all because even count will cancel out the negative sign but if the count is odd we will be uh ignoring the maximum element okay from negative so that's that's what we will be doing in this question so I hope this question is clear and see I will be just showing you the Java solution as well see Here this is the Java solution.
Okay. Please check here.
Okay. And this is the Python solution.
Please check here.
Okay. It is almost same also. If you got the inputs in comma separated array like this - 1 2 3 4. We already discussed how to split right? We use the split function. We can directly split this array using comma. So using comma you can split this. Okay. And also if there are extra wide spaces, you can use the while loop. Okay. How to take coding input? In that video we already discussed about this. So I don't think so that in taking the input there should be any issue. Thank you so much for watching. Hi everyone. So this was the second coding question which got asked today in 26th March morning shift. Okay.
So we will be solving this. Okay. First of all I want to discuss about this.
See, I don't know because I was not giving the exam. But if the recursion logic was working for this question, then this question is easy. And if the recursion logic was not working for this, then there's a concept called cycle that we will be discussing in this video. Uh so that solution I would say will be little bit difficult to find out for the students who never solved the questions which are related to cycles.
Okay? Because this question was based on cycles. Okay? So if you have never studied about cycles then this question can be felt uh difficult. But if the recursion logic was working in this question like uh the recursion uh logic was passing the test cases then I think this question is easy. Okay. So I will be talking about both the test cases.
Okay. So basically this question says we have a input like this. Okay. Okay. So before going ahead with this we have a mock test series which is TC and mock test series. There are a lot of live test which are going through. So you can go through it. The link of the mock test series will be in the description. You can attempt the live test before your exam. If you have your exam on 1st April, third April or in the April month and these live test will help you out to actually um perform well in your exam because the questions will be very similar what TCS will ask in your actual exam. Okay. So the mock test series will link will be in the description which you can use for your preparation. So let's discuss about this. So see basically this question says a array A the formation is 10 20 50 and 40. For array B the formation is 50 20 40 and 10. Okay. We need to transform the A uh uh eth array in a B uh array of B. Okay.
And basically when we want to transfer we will be doing the swaps. We will be swapping and we want to find out the minimum operations. Okay. So if I will perform the swapping operation, what I can do is I can check here. Okay, I can check here uh 50. Okay, if you will uh if you will see here, okay, so 50 I can swap here with uh with 40, right? So one swap will be this. So it will be 10, 20, 40 and 50, right? So this is the first swap. Second swap I can do is I can swap 10 with 50. Okay. So it will be 50 20 40 and 10. So this will be my two operations. Okay. This will be my two operations. Okay. In this two operations I can transform this array A into the array B. This was the question. Okay. So two operations will be required. I can swap any two elements. Okay. So uh minimum operations we need to find out.
So two operations will be there. Okay.
Now see if if we want to implement the recursion logic. I think the recursion logic is very very easy. Okay. And based on that if the T error was not there in the exam then I think this question is easy. If the T error was there I don't know about it then this question is tough. Okay. So the array is saying 10 20 what is the array?
10 20 50 40. Okay. 10 50 40. This is the eighth array. Okay. And our be array is 10 oh sorry 50 20 40 and 10. Okay. So by using the recursion array what we can do is what we can do is okay I will run my recursion logic here. I will run my recursion logic here. Okay and I will I will swap the elements.
Okay, I will swap the elements and I will be counting the operations. So what I will be doing here? I will be matching the index. Okay, I will be matching the index. So if I will make the index of this then this is zero index, one index, two index or three index. Okay, now [clears throat] based on these index I will be checking with these elements.
Okay. So 50 will be on third index. So it will be three. Okay, 20 is on second index, it will it will be two. 40 is on fourth index, it will be four. And um 10 is on first index. So one or I can say it is 0 3 zero based indexing 1 and two.
Okay. So this is the index of B based on A. Okay. Now based on this index, okay, in recursion I can swap the elements. I can swap the elements. Okay. Based on the index, right? So swap zero with two right. Swap zero index with second index. Okay. So I can swap this and I can do count ++ and then I will again pass the whole array in my recursion function. And that way I will be computing the minimum operations. Okay. I will be making one uh one variable which is minimum operation which will be storing all the swaps uh which I will be passing in the function and that will store our final count. Okay. But this approach will take a lot of time complexity right I hope the approach is simple and clear but it will take a lot of time complexity so I am not sure if it will work or not so we will be discussing the approach which is called cycle okay cycle detection so see if you have done any question previously related to cycles okay I think this approach will be easy to you if you don't know how to detect the cycles in the array okay then this approach can be felt difficult to you. Okay. So, basically there's a concept called cycle. I will be not going into detail but a cycle concept is there in the arrays when some elements are pointing out the other element and they again are pointing out to some other element.
Okay. For an example if I will convert the indexes here. So again if you will see here this is zeroth index this is one it is two and it is three. Again if I will convert the indexes from here it is second index it is one index it is uh third index and if it is it is zero index. Okay. So actually at the place of zero it should be two right if we will do the swaps. So at the place of zero it should be two at the now if I talking about two. So I will go to the two and I will check here at the place of two it should be three.
Okay, it should be three, right? And at the place of three, it should be zero.
So now if you can see here, at the place of zero, it should be two. At the place of two, it should be three. And at the place of three, it should be zero. So this is a cycle, right? This is a cycle.
Okay? And if the input is long, there can be multiple cycles like this. Okay?
And if we want to sort out this cycle like we have 0 2 3 okay we have cycle between this okay how many elements are there total four elements if we if we will see in the cycle the cycle size is four right but how many elements are there total three elements are there right so how many swaps actually uh uh it will be requiring it will be requiring element elements minus one swap. Okay, elements minus one swap.
Okay, why? See, if I have three positions and I want to settle down A, B, C on these three position. Okay, and these on these three position I want to settle down A B C. If I will decide where A will be sitting, where B will be sitting, automatically the place of C will be there. Okay. So only we need to decide two places, right? So that's what we will be doing here. If we will be arranging three elements in the arrangement of three elements if we will be doing two swaps for the third element automatically it will be on the correct position. Okay.
So that's why the count of the swaps will be two right. So for this this uh cycle if you will see here the count will be two.
Okay because see we have three elements 0 2 and three and if we will place 2 and three at the right point automatically G zero will be uh following the right place right that's why the value of the swaps will be two total number of elements minus one. Okay. Okay. So after swapping this 0 2 and three our array will be in the correct manner like B.
Okay. So the final answer will be two.
This is called the cycle concept. Okay.
And yes there can be the multiple cycles. But the thing is if there are multi multiple cycles. Suppose there is a cycle like this. Okay. Like 0 is pointing two, two is pointing three, three is again pointing zero. Then again we have a cycle. Suppose five is pointing to 7, 7 is pointing to 8, 8 is pointing to six and six is pointing to five. Again this is a cycle. Okay.
Suppose these two cycles are there in a big input. So the count from this so the count from this okay will be two and the count from this like four elements are there will be three. So total number of swaps required in this array will be five. Okay 3 + 2. So this logic we will be using here. Okay. The cycle detection thing we will be doing here and this is the optimized logic. I hope the logic is clear but I just wanted to clarify you with the example once again. Okay. So it will make more sense. Our a array the value of a array is 10. Okay 20 50 and 40. Our value of b array is 50.
Okay. Then 20 40 10.
Now listen me carefully. First of all, we will be finding out the positions of this and we will be ma mapping the position to the B array. Okay. So 10 is on zero. 20 is on one. 50 is on two and 40 is on three. If I will do the mapping of the position. See the position of 50.
50 is on two position. So two will be mapped here. On 20 one will be mapped here. On 40 will be mapped here. And on 10 zero will be mapped here. I will start with index zero. Okay, I will ind start with index zero and I will check if the element is at the proper position. So if you I will say here so on the zerooth zerooth position it should be the second element. Okay, because see here the mapping. Okay, on zero it should be second. Okay, so I will start from here. Okay. And okay on the zero it it should be two. Okay. So I will increase my cycle here. Okay. If it should be two here I will be directly going to the two position. Okay. Second position. So when I will go to the two position I'm just visiting to the position and I am counting the total number of elements. So here if you will see here the total number of elements here is now two zero and uh uh uh uh or you can say count here is one. the length of the cycle. Okay, the length of the cycle is one here. Okay, from 0 to two. Okay, now I move to the second uh second position. On the second position, if I will see the mapping, it is pointing to three. Okay, it is pointing to three. Okay, so two is pointing to three. So, I will go on third position here. If I will see here on the third position, it should be zero. So, three again is pointing to zero. Okay. So, now see the length of cycle. The cycle length is 1 2 and three. Total three length is there between the cycle. Okay.
If the length of the cycle is three.
Okay. The swaps required will be two. So my count of the swaps will be now two.
Okay. Now I have count two. Okay. After this count two. Okay. After this count two and after all these swappings I will check from here. Okay. And every swapping when every swapping is done.
Okay. When every swapping is done basically if every swapping is done and I will be checking if all elements are visited. Okay. So right now if you will see here we have visited the zerooth index. So we will mark this as visited.
Okay. We will visited second index. So we will mark this as visited.
We have visited the third index. So we will mark this as visited any index left first index this is left. So now we will check here on the first index that is it visited or not. So it is not visited but now we will be checking the position at the second index it is one and the position is one. So both are same. If both are same no cycle detection required. We will move with the another element the next element. But this is already visited. This is already visited. So our array is completed and our final answer is two. Total number of swipe required are two. Okay. So this is the logic here. I hope you guys got it.
If you have not got it again please watch this video re again. Now let's talk about this. So whatever we have discussed in the logic we will be doing the same. First of all we are mapping the position here. I am I have created one map. After creating the map, okay, I am assigning the position. Okay. Okay.
So position if you guys remember we have uh assigned the position to the index right here. So 50 will be 2, 20 will be 1, 40 will be three and 10 here will be zero. Right. So the same thing we are doing here we are assigning at the position map all the indexes for B of I.
Okay. After that we have created one visited array which we just discussed.
The value of swaps is zero. Then we started our loop. We have checked we are checking if the uh if if the number is visited okay uh if the number is visited and okay and if the position is matching with the mapping position. So the ath position is m matching with b position.
We can continue our loop. Right? And the element is visited. Okay. If it is not visited, the cycle size is currently zero. Okay. The cycle size is currently zero. Okay. We will be assigning the index value to our variable j. Then we will be running one loop. Okay. Visited.
Okay. From visited. So in this visited loop, we will be detecting our cycle.
How we are detecting our cycle? We are starting our loop not visited. Okay. So suppose our cycle is like this 0 2 3 then again zero. Right. So we are starting with zero. Right. So currently it is not visited. Okay. Then we will mark this as visited. Okay. After marking this as visited which we have done done here. Okay. We will be going to the mapping position. So for zero the mapping position was two. Right. So we are going from 0 to two. So we are assigning that mapping position here to J. Okay. So the mapping position we assigned. Okay. After assigning the mapping position here we are increasing the size of cycle. Right. This is the thing which we have done. After that again okay we will be checking here if it is visited. So no two is again not visited. Okay. So we will mark two as visited. After that again from two we will be going to the mapping position.
The mapping position of two is three. So we will visit to three. Okay. And we will be increasing our cycle size to two. Okay. Then from three again we will mark this as visited. Okay. From three we will we will be going to zero again because the mapping position of three is 0. So again we will be going to zero.
But zero is now visited. We will be doing cycle plus. So the size of cycle will be now three. But zero is wasted.
So this loop will be ended. So our cycle will be looking like 0 2 and three. Now cycle size is three. So number of swaps will be required uh will be cycle size minus one. So the number of step required will be two. Okay. And that way we will be returning our final swaps.
Okay. So and this loop will continue the i loop will continue going to check the multiple cycles. Okay. At last we will be returning our swaps and this will be our final solution. So I hope the logic is clear and if you have any doubt see from my side I tried my best to explain in the simplest way. This concept is not easy. If you are finding it difficult please watch this video again and you will be getting it. This is the Java solution and this is the Python solution. Okay. Thank you so much for watching.
Hi everyone. So starting questions first question already same Same apps TCSQ.
will be in the description questions.
So you can purchase link of this uh TCSQ mock test series will be in the description which will help you a lot for your preparation mock test series.
It's encoding first question was given a input for a minimum charge required for a laptop which is working and second is array in which how much laptop is charge given we need to print the number of laptop that can work 6% right out two laptops which are greater than that. For example, five to five number six or seven simply check if a r of i is greater than n. Okay.
Count last say total number of laptops answering.
So 2 3 6 7 1 input string for an example. 1 2 3 4 in. Okay. So map and finally we will get our final uh array like this. Okay.
Scanning line nextes and string or numbers by converting to integrate.
Input read.
question. Second question discuss you are given an integer and representing the number of initially in a happy state. So suppose integer value neration 70% of happy people sad or 30% happy or happy.
Second condition 50% of sad remain sad and 50% of sad become happy new current state again Happy% happy% out of 30% sad.
Happy 45 happy remaining 55 sader new happy new Starting new happy% right 0.3 happy Right.
MIP 0.5 sad people in sad people 0.5% of happy% 0.5% 0.5% asked print sad or happy. So basically calculation calculation and yeah this is the solution. So these are the solution for the coding questions got asked on 1st of April but link of the mock test series will be in the description which you can use for your preparation if you have your TCS NQT exam. Thank you so much for watching. Hi everyone. So this video solve third April coding questions TCSQ exam.
So questions definitely.
So make sure to like this video. Let's solve all the coding questions.
You are preparing for TCS and so you can purchase our mock test series.
So you can enroll in our test series.
So you can purchase it.
exams. So definitely it will be helping out to prepare and see your ranks. Okay.
So Tesulate speed and speed formula distance time s= to d / t formula so kilome hour change kilome 0.1 km or second kilome sorry second.
So second 1x3 a second 1x 3 600 0 rulon distance 01 360 0 Right.
Okay. So 3 6 0 * 0.1.6.
So 3.6 simple distance.
Okay for 3.6 Six.
Right.
Inut space first value integer D and secondut for distance.
Simple question.
First question.
So next question. Given an integer array find the maximum using also prints 202 same question same question off campus TCSQ same question repeat6 algorithm Max.
So basically - 3 4 - 1 - 2 1 5 - 3 rules.
First rule.
Okay.
simple value.
So I hope Startus 3 - 3 index match.
Compare index index compare current indexus Startus start element -3 + 4 current index four or comput one index compare indus 1 + 1 Two.
Two.
Five. Five.
Sevenus 3.
But Max starting index. Starting index, right? J index. So I max and this is the max value.
So basically like four -1 - 2 + 1 and five and answer maximum sum or I index value.
So this was the question. I hope you're clear.
Simple.
I hope I hope you guys like this video. Thank you so much for watching. Hi everyone.
So video solve 3rd April second shift questions. So 3rd April exam question second shift and questions solve. Make sure to like this video and let's start the exam. So you can purchase the TSQ mock test series link will be in the description. The thing is yes, they are very very helpful for the actual assessment.
focusing.
So definitely if you want you can enroll in our mock test series plus the discount coupon code description the link of the mock test series will be in the description start min and max frequency element element with minimum frequency and element with maximum frequency.
For example, 1 2 2 3 3 3 3 3 and four.
Minimum and maximum frequency elements.
Frequency or frequency value Start.
value.
Minimum frequency minimum element with minimum frequency, right?
Second element maximum ele one element minimum frequency three element maximum frequency bus simput Frequency.
Then minimum frequency maximum element first element maxerum [snorts] frequency element minimum Frequency element minimum element frequency frequency minimum frequency maximum element or maximum element return simple Next question.
question easy but I will consider this question as medium hard medium hard category question n people number from 1 to n every k person is deleted return the last remaining person for an example 1 to n= 7 and k= 3.
The answer n = 7 and k = 3.
Delete.
Delete.
Delete.
Delete.
Delete.
Basically circular like for example 1 2 3 4 5 6 or 7 table right element Delete array for an example.
modulose three delete delete right But so 1 2 3 4 5 6 or 7. Circular table.
Circular table. 1 2 3 4 5 6 7 1 2 3 Next 2 or two element.
Starting point four 5 6 7 1 2 or nu Circular array, right? Circular array 7 1 2 4 or five or n 2 3 counting start elements to 4 5 7 or 4 5 1 4 5 1 4 5 to 1 2 3 5 1 4 on counting start 1 2 3 1 four So last Same elements.
1 2 3 4 5 6 7 0 index first index two three pair four P five or six index cancel one or two element So basically 2 3 4 5 6 0 1 Right.
Fourth index. First index. Fifth index.
Second index.
Sixth index. Third index. Index.
Index.
Fourth ind.
Fifth index.
Suppose X or Y converted index converted index and counting start index right index.
So basically if you will see here it's nothing but x + 1 or 3 2 or 5 it's nothing but x + k k gap right but three and so 4 1 it's 0 1 2 3 4 and 5. So basically it is nothing but x + k x + kul.
So for example ind index.
So fun.
A element index right answer otherwise Joseph Nusion So function nution for solution modulus original index.
So nod and convert to a 0 + 3 k value 3 modulus by k n 7 to 3 modulus by 7 about 1 4 convert 1 + 3 modulus by 7 to [snorts] four modulus by 7 answer Four three uh sorry uh two so 2 uh uh + 3 modulus by 7 to 2 convert five three six fourth index zero convert fourth index zero convert answer 4 + 3 modulus by 7 modulus by 7 is fourth index convert index one convert So n basically nus one kodain thinking definitely difficult. So I hope thank you so much for watching. Hi everyone. So 14th April second question 20th 20th 14th exams.
So make sure Right.
So, you can purchase our TCSQD mock test series link description. It will be super helpful for you.
So link of this TSQ series will be in the description which you can purchase if you have your exam in the upcoming time. Okay. So let's start. First question.
You are given an array of integer. Your task is to sort the array using quick sort algorithm. Always choose the last element as the pivot. During each partition step, print the index of the pivot after placing it in correct position. Finally, print the sorted array. Question.
For example, a array.
Suppose 1 6 9 8 7 3 and TCS comp.
function.
So start [snorts] divide divide based on pivot element divide.
For example, Left side for example.
foreign.
Right.
Left part. Same. Right. Same.
Quick sort function 21 left part and 8 21 last element already.
Two or one two or one sort multiple parts.
So basically [snorts] function 169 87 7 325. Suppose for an example element.
I elements.
I pointer One piv. element.
I + I inial IP I increase or Jinter Jinter Pinter element.
element.
important element.
I plus I increase I swore And 1 2 Eight for seven or seven 9 five oriv element plus I swep I 1 2 5 7 6 and Left side question index.
Okay. During each partition step, okay, print the index of the pivot after placing it in it. Correct positioniv 0 1 2 3 print.
Next element first question start for example 2 4 6 9 Last element I start if the element is less than pivot plus.
If the element is less than pivot discuss last I + 1 index I plusivis and pivot. Right.
Simple. I hope you have your exam. Thank you so much for watching.
関連おすすめ
Ubuntu Touch Q&A 190
UBports
241 views•2026-05-17
Learning k8s ep. 3 - The end of the VM
devcentral
102 views•2026-05-15
Iterators and Generators: Real Use Cases
jsmentor-uk
188 views•2026-05-17
The 4 Bit AI Training Trick
explaquiz
414 views•2026-05-19
Image to 3D World Workflow 👀
badxstudio
843 views•2026-05-16
Why Learn Algorithms in the AI Era
bitsandproofs
245 views•2026-05-17
NFA - Transition Diagram and Transition Table
nesoacademy
198 views•2026-05-19
DSA Topics and Algorithms Overview #coding
DSA-in-Minutes1
423 views•2026-05-15











