Day 2 : Coding Challenge

Day 2 : Coding Challenge

Problem - Given an integer array nums and an integer val, remove all ccurrences of val in nums in-place. The order of the elements may be changed. Then return the number of elements in nums which are not equal to val.

Consider the number of elements in nums which are not equal to val be k, to get accepted, you need to do the following things:

  • Change the array nums such that the first k elements of nums contain the elements which are not equal to val. The remaining elements of nums are not important as well as the size of nums.
  • Return k.

Solution -

Optimized Solution -

The Problem is easy we have to return the k elements and the array consisting of elements which are not equal to val.

Here we will use the variable to insert the elements which are not equal to val at the front of array simultaneously.


Time Complexity - O(N)

Space Complexity - O(1)



#Codingchallenge #problemsolvingskills #problemsolving #codeforgood #60daysofchallenge #60dayscode #consistency #dsaforproblemsolving

To view or add a comment, sign in

Insights from the community

Others also viewed

Explore topics