9/3/2023 0 Comments Kotlin for each with index![]() Loop through the array and for each iteration.Let us solve this step by step Elements of array, arr, where iteration starts at 0 Note that the length of the array is 10 and the maximum value of any element in the array is <=10. The output for this input will be 10 and 1. Well, let’s consider some sample data and try solving this step by step. A nested for loop would also not be the right solution since we need to solve the problem in O(n) runtime. Using a Set or a Map would not be the right solution with these constraints. We had a discussion about using a Set or may be a HashMap in Java since the main part of the problem was to find the “duplicates”. Now, it was my turn to be silent for the next couple of seconds after he told me about the problem statement with the constraints about not using additional space and O(n) runtime. We need to do this without using additional space in O(n) runtime. Problem Statementįind the elements which appear twice in an array of integers, where the elements in the array being : 1 ≤ a ≤ n ( n = size of array). My immediate reaction – “That is easy and I am sure you got it !” But just then there was a two second silence from his end and then he said that he couldn’t get it right since some additional constraints were mentioned to him ‘later’. The problem was to find the duplicates in an array of integers. ![]() During a conversation with my colleague, he mentioned to me about a problem that he was asked in his technical interview.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |