Quick actions

cmd+k|ctrl+k

Navigation

Languages

segregateArrayForOddsAndEvenElements

Snippet info

Language

Cpp

Visibility

public

Author

preeti.gaur1790

Created

2022-05-30T10:49:47.444502Z

Updated

2022-05-30T10:49:47.444502Z

#include <iostream>
using namespace std;



void segregateArrayForOddsAndEvenElements(int arr[], int length) {
    int low = 0;
    int high = length-1;
    
    while (low < high) {
        
        while(arr[low] % 2 == 0 && low < high) {
            low ++;
        }
        
        while(arr[high] % 2 != 0 && low < high) {
            high --;
        }
        
        if(low < high) {
            int temp = arr[low];
            arr[low] = arr[high];
            arr[high] = temp;
        }
    }
    
    for(int i = 0; i< length; i++) {
        cout<< arr[i] << " ";
    }
    
}

int main() {
    int arr[] = {1,3,4,7,8,2};
    int length = sizeof(arr) / sizeof(arr[0]);
    segregateArrayForOddsAndEvenElements(arr, length);
 
    return 0;
}
INFO