Computer Science, asked by anubhavrathore, 1 year ago

write a program input 10 element in an array and print the half of all even position elements otherwise print its twice

Answers

Answered by Anonymous
3
______. JAVA ______






_____________________________




import java.io.*;

 

class SegregateOddEven

{

    static void segregateEvenOdd(int arr[])

    {

        /* Initialize left and right indexes */

        int left = 0, right = arr.length - 1;

        while (left < right)

        {

            /* Increment left index while we see 0 at left */

            while (arr[left]%2 == 0 && left < right)

                left++;

 

            /* Decrement right index while we see 1 at right */

            while (arr[right]%2 == 1 && left < right)

                right--;

 

            if (left < right)

            {

                /* Swap arr[left] and arr[right]*/

                int temp = arr[left];

                arr[left] = arr[right];

                arr[right] = temp;

                left++;

                right--;

            }

        }

    }

 

    /* Driver program to test above functions */

    public static void main (String[] args)

    {

        int arr[] = {12, 34, 45, 9, 8, 90, 3};

 

        segregateEvenOdd(arr);

 

        System.out.print("Array after segregation ");

        for (int i = 0; i < arr.length; i++)

            System.out.print(arr[i]+" ");

    }

}





_________. C ++ ___________



___________________

#include <iostream>

using namespace std;

 

// fuction is rearrange the array in given way.

void rearrangeEvenAndOdd(int arr[], int n)

{

    // variables

    int j = -1;

 

    // quick sort method

    for (int i = 0; i < n; i++) {

 

        // if array of element

        // is odd then swap

        if (arr[i] % 2 == 0) {

 

            // increment j by one

            j++;

 

            // swap the element

            swap(arr[i], arr[j]);

        }

    }

}

 

int main()

{

    int arr[] = { 12, 10, 9, 45, 2, 10, 10, 45 };

    int n = sizeof(arr) / sizeof(arr[0]);

 

    rearrangeEvenAndOdd(arr, n);

 

    for (int i = 0; i < n; i++)

        cout << arr[i] << " ";


Similar questions