Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java code as well as the runtime and space requirements using Big O nums should

ID: 3873077 • Letter: J

Question

Java code as well as the runtime and space requirements using Big O

nums should contain only even numbers after this function returns. Brute force static void removeOdds(ArrayList nums) () nums should contain only even numbers after this function returns. You will receive 0 credit for an algorithm that runs in worse than linear time. For an extra challenge, try it using O(1) space. " static void removeOdds(ArrayList nums) () sentence should contain the same characters but in reverse order when this function returns. For example: "I am a house." should return "esuoh a ma I*I static void reverse(char sentence) sentence should contain the same words but in reverse order when this function returns. You may assume the sentence contains only a single punctuation make at the end and you should leave it there. You may ignore capitalization. For example: "l am a house." should return "house a am I." For an extra challenge, try it using O(1) space. I static void reverse(char sentence)

Explanation / Answer

static void removeOdds(ArrayList<Integer> nums)

{

for(i=0;i<l;i++)

{

if(nums[i]%2==1)

{

System.out.println("This is an odd number ");

nums.remove(i);

}

}

}

The above algorithm is scanning the list in linear time because we are traversing till the last element .

So time omplexity = O(n) and space complexity = O(1) as we are not using extra space.

static void reverse(char[] sentence )

{

for (int i = sentence.length-1; i>=0; i--)

{

            System.out.print(sentence[i]);

}

}

In this function we are just printing the each character of string using direct index , but we are doing of all elements of the string so time complexity - O(1*n)=O(n) , where n the length of string or sentence .

Here we are not using any extra space so space complexity = O(1) .

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote