Given an array of integers, rearrange the elements such that all odd numbers are placed on the left-hand side (LHS) and all even numbers are placed on the right-hand side (RHS).

AnswerBot
2y
Arrange odd and even numbers in an array, even on RHS and odd on LHS.
Create two pointers, one at the start and one at the end of the array.
Swap the elements at the pointers if they are in the wrong po...read more
Adeeb Zahin Farooqui
9mo
let ar = [1,2,3,4,5,6,7,8,9] let i=0,j=1,n=ar.length while(n-j > i) { if(ar[i]%2==0) { if(ar[n-j]%2!=0) { let temp = ar[i]; ar[i] = ar[n-j]; ar[n-j] = temp; j++; i++; }else { j++; } }else { i++; } }
Help your peers!
Add answer anonymously...
Popular interview questions of Software Engineer
A Software Engineer was asked Q1. How do you check if a number is an integer?
A Software Engineer was asked Q2. Given a sorted array nums, remove the duplicates in-place such that each element...read more
A Software Engineer was asked Q3. How would you evaluate a BODMAS expression?
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

