Hackearth's Data Structures solutions( Takeoff, Help Jarvis! , Pairs Having Similar Elements ) :
Problem 4: Takeoff
Solution: (in c++)
( please guys before moving to the solution try it yourself at least 3-4 times , if you really wanna become a good coder)
This code is simple. There's no need for any explanation.
Problem 5: Help Jarvis!
Solution: (in c++)
( please guys before moving to the solution try it yourself at least 3-4 times , if you really wanna become a good coder)
What i have done here is , first a string of numbers is taken and then the individual digits of that string is stored in a vector of characters of same size as that of string and then sorted the vector.
For example - the input string is 2145
all digits will be stored in a vector and after sorting the vector will look like this:
index value
0 1
1 2
2 4
3 5
Now i declared a temp variable(outside of for loop) which holds the value of the minimum element in the vector i.e first element (0th index) initially. The temp value will be increased by 1 at the end of the loop using temp++(so that temp values forms a normal counting). Now what we have to check is the next elements in the vector forms a normal counting. For this purpose , we will use the for loop to iterate through the vector. The if condition checks the current value is equal to the temp value(expected value) and if it is not equal than we simply print NO and break out of the loop using break statement. The else loop will execute only when temp (expected value) matches vector element. We can printout YES only when all the elements in the vector matches temp value(expected value). We know at the end the i will be equal to 1 less than the size of vector( as indexing starts from zero) and for that purpose only we used an if statement in the else. Hence YES gets printed when all the elements matches with the temp.
Problem 6: Pairs Having Similar Elements
Solution: (in c++)
( please guys before moving to the solution try it yourself at least 3-4 times , if you really wanna become a good coder)
This code is simple. There's no need for any explanation.
Guys , if you have any queries related to a question or need more explanation for a question , comment below!
Bruh! Can u explain the else if condition .why have you incremented both x and y
ReplyDelete