Program to make pair of elements alternatively

Description

This program will explain how to make pair of elements from taking elements from
mid with elements from start alternatively.

Example 1 : arr[] = {2,7,6,3,5,9}
            After making pair the array will be
            arr[] = {2,3,7,5,6,9}
            
Example 2 :  arr[] = {7,3,9,5,4,8,6}
            After making pair the array will be
            arr[] = {7,4,3,8,9,6,5}

C/C++

/* C Program to make pair of elements alternatively */
//Save it as MakePairOfElements.c

#include<stdio.h>
int main(){

    int i, n, val1;

    printf("Enter the size of array : ");
    scanf("%d",&n);

    //Declaring the array
    int arr[n];
    //Declaring the new array to store the value from first array
    int arrNew[n];

    printf("Enter the array elements : ");
    for(i=0;i<n;i++) {
        scanf("%d",&arr[i]);
    }

    /*Dividing the array into two parts, taking upper 
    limit if size of array is odd*/
    if(n%2==0) {
        val1=(n/2);
    }else if(n%2!=0) {
        val1=((n+1)/2);
    }

    //Storing the first part of the array into new array
    for(i=0;i<val1;i++) {
        arrNew[i*2] = arr[i];
    }
    int count=1;
    //Storing the first part of the array into new array
    for(i=val1;i<n;i++) {
        arrNew[count] = arr[i];
        count = count+2;
        if(count>=n) {
            break;
        }
    }

    //Printing the final array
    printf("New array after making pair : \n");
    for(i=0;i<n;i++) {
        printf("%d ",arrNew[i]);
    }
}
Input:
Enter the size of array : 
7
Enter the array elements : 
7
3
9
5
4
8
6

Output:
New array after making pair : 
7 4 3 8 9 6 5

Java

/* Java Program to make pair of elements alternatively */
//Save it as MakePairOfElements.java

import java.io.*;
import java.util.Scanner;

public class MakePairOfElements {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        
        System.out.println("Enter the size of array : ");
        int n = scanner.nextInt();
        
        //Declaring the array
        int arr[] = new int[n];
        //Declaring the new array to store the value from first array
        int arrNew[] = new int[n];
        
        System.out.println("Enter the array elements : ");
        for(int i=0;i<n;i++) {
            arr[i] = scanner.nextInt();
        }
        
        int val1=0;
        
       /*Dividing the array into two parts, taking upper
                limit if size of array is odd*/
        if(n%2==0) {
            val1=(n/2);
        }else if(n%2!=0) {
            val1=((n+1)/2);
        }
        
        //Storing the first part of the array into new array
        for(int i=0;i<val1;i++) {
            arrNew[i*2] = arr[i];
        }
        int count=1;
        //Storing the first part of the array into new array
        for(int i=val1;i<n;i++) {
            arrNew[count] = arr[i];
            count = count+2;
            if(count>=n) {
                break;
            }
        }
        
        //Printing the final array
        System.out.println("New array after making pair : ");
        for(int i=0;i<n;i++) {
            System.out.print(arrNew[i]+" ");
        }
    }
}
Input:
Enter the size of array : 
6
Enter the array elements : 
2
7
6
3
5
9

Output:
New array after making pair : 
2 3 7 5 6 9

Related Programs

1) Program to Print How Many Numbers Smaller than Current Number
2) Remove Duplicate Elements From Array
3) Program to form a number using entered digits
4) Program to merge two sorted arrays
5) Program to swap maximum and minimum element of Array
6) Program to Add two Matrices
7) Program to Multiply two Matrices
8) Program to Rearrange an array such that arr[i]=i
9) Program to Find Mode of a Array
10) Program to Check Array is Perfect or Not
Share Me

Leave a Reply