📌  相关文章
📜  偶数索引为偶数,奇数索引为奇数

📅  最后修改于: 2022-05-13 01:57:49.275000             🧑  作者: Mango

偶数索引为偶数,奇数索引为奇数

给定一个大小为n的数组,其中包含相等数量的奇数和偶数。问题是以这样一种方式排列数字,即所有偶数都得到偶数索引,奇数得到奇数索引。所需的辅助空间为 O(1)。
例子 :

Input : arr[] = {3, 6, 12, 1, 5, 8}
Output : 6 3 12 1 8 5 

Input : arr[] = {10, 9, 7, 18, 13, 19, 4, 20, 21, 14}
Output : 10 9 18 7 20 19 4 13 14 21 

来源:亚马逊面试经历 |设置 410。

方法 :

  • 从左边开始,保留两个索引,一个用于偶数位置,另一个用于奇数位置。
  • 从左边遍历这些索引。
  • 在偶数位置应该有偶数,在奇数位置应该有奇数。
  • 每当有 mismatch 时,我们交换奇数和偶数索引处的值。

以下是上述方法的实现:

CPP
// C++ implementation to arrange
// odd and even numbers
#include 
using namespace std;
  
// function to arrange odd and even numbers
void arrangeOddAndEven(int arr[], int n)
{
   int oddInd = 1;
    int evenInd = 0;
    while (true)
    {
        while (evenInd < n && arr[evenInd] % 2 == 0)
            evenInd += 2;
              
        while (oddInd < n && arr[oddInd] % 2 == 1)
            oddInd += 2;
              
        if (evenInd < n && oddInd < n)
            swap (arr[evenInd], arr[oddInd]);
              
        else
            break;
    }
}
  
// function to print the array
void printArray(int arr[], int n)
{
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
}
  
// Driver program to test above
int main()
{
    int arr[] = { 3, 6, 12, 1, 5, 8 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    cout << "Original Array: ";
    printArray(arr, n);
  
    arrangeOddAndEven(arr, n);
  
    cout << "\nModified Array: ";
    printArray(arr, n);
  
    return 0;
}


Java
// Java implementation to
// arrange odd and even numbers
  
import java.util.*;
import java.lang.*;
  
class GfG {
  
// function to arrange
// odd and even numbers
public static void arrangeOddAndEven(int arr[], int n)
{
    int oddInd = 1;
    int evenInd = 0;
    while (true)
    {
        while (evenInd < n && arr[evenInd] % 2 == 0)
            evenInd += 2;
              
        while (oddInd < n && arr[oddInd] % 2 == 1)
            oddInd += 2;
              
        if (evenInd < n && oddInd < n)
            {
                int temp = arr[evenInd];
                arr[evenInd] = arr[oddInd];
                arr[oddInd] = temp;
            }
              
        else
            break;
    }
}
  
// function to print the array
public static void printArray(int arr[], int n)
{
    for (int i = 0; i < n; i++)
        System.out.print(arr[i] + " ");
}
      
// Driver function 
public static void main(String argc[]){
    int arr[] = { 3, 6, 12, 1, 5, 8 };
    int n = 6;
  
    System.out.print("Original Array: ");
    printArray(arr, n);
  
    arrangeOddAndEven(arr, n);
  
    System.out.print("\nModified Array: ");
    printArray(arr, n);
}
}
  
// This code is contributed by Sagar Shukla


Python3
# Python3 implementation to
# arrange odd and even numbers
  
def arrangeOddAndEven(arr,  n):
      
    oddInd = 1
    evenInd = 0
    while (True):
          
        while (evenInd < n and arr[evenInd] % 2 == 0):
            evenInd += 2
               
        while (oddInd < n and arr[oddInd] % 2 == 1):
            oddInd += 2
               
        if (evenInd < n and oddInd < n):
                temp = arr[evenInd]
                arr[evenInd] = arr[oddInd]
                arr[oddInd] = temp;
               
        else:
            break
   
# function to print the array
def printArray(arr,  n):
    for i in range(0,n):
        print(arr[i] , "",end="")
  
       
# Driver function 
def main():
    arr = [ 3, 6, 12, 1, 5, 8 ]
    n = 6
   
    print("Original Array: ",end="")
    printArray(arr, n)
   
    arrangeOddAndEven(arr, n)
   
    print("\nModified Array: ",end="")
    printArray(arr, n)
      
if __name__ == '__main__':
    main()
# This code is contributed by 29AjayKumar


C#
// C# implementation to
// arrange odd and even numbers
using System;
  
class GFG {
  
    // function to arrange
    // odd and even numbers
    public static void arrangeOddAndEven(int[] arr, int n)
    {
     int oddInd = 1;
     int evenInd = 0;
    while (true)
    {
        while (evenInd < n && arr[evenInd] % 2 == 0)
            evenInd += 2;
               
        while (oddInd < n && arr[oddInd] % 2 == 1)
            oddInd += 2;
               
        if (evenInd < n && oddInd < n)
            {
                int temp = arr[evenInd];
                arr[evenInd] = arr[oddInd];
                arr[oddInd] = temp;
            }
               
        else
            break;
    }
    }
  
    // function to print the array
    public static void printArray(int[] arr, int n)
    {
        for (int i = 0; i < n; i++)
            Console.Write(arr[i] + " ");
    }
  
    // Driver function
    public static void Main()
    {
        int[] arr = { 3, 6, 12, 1, 5, 8 };
        int n = 6;
  
        Console.Write("Original Array: ");
        printArray(arr, n);
  
        arrangeOddAndEven(arr, n);
  
        Console.Write("\nModified Array: ");
        printArray(arr, n);
    }
}
  
// This code is contributed by Sam007


Javascript


输出 :

Original Array: 3 6 12 1 5 8 
Modified Array: 6 3 12 1 8 5