 1596988320

# Shuffle the position of each Array element by swapping adjacent elements

Given an array arr[], the task is to rearrange the array elements by swapping adjacent elements such that no element remains at same position after swapping.

Examples:

Input:_ arr[] = { 1, 2, 3, 4, 5 }_

Output:_ 2 1 5 3 4_

Explanation:

Adjacent elements are swapped as follows:

(1, 2 -> 2, 1)

(3, 4, 5 -> 5, 3, 4)

Input:_ arr[] = {1, 2, 3, 4}_

Output:_ 2 1 4 3_

Explanation:

Adjacent elements are swapped as follows:

1, 2 -> 2, 1

3, 4 -> 4, 3

Approach: The key observation in the problem is that there can be two cases for the arrays to swap the array elements:

Below is the implementation of the above approach:

• C++
• C#

`// C++ program for the above approach `

`#include <bits/stdc++.h> `

`**using**` `**namespace**` `std; `

`// Function to print the array `

`**void**` `print(``**int**` `a[],` `**int**` `n) `

`{ `

`// Loop to iterate over the `

`// elements of array `

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

`cout << a[i] <<` `" "``; `

`} `

`// Function to swap two variables without `

`// using the third variable `

`**void**` `swapTwo(``**int**``& x,` `**int**``& y) `

`{ `

`// Store XOR of all in x `

`x = x ^ y; `

`// After this, y has value of x `

`y = x ^ y; `

`// After this, x has value of y `

`x = x ^ y; `

`} `

`// Function to swap three variables `

`// without using fourth variable `

`**void**` `swapThree(``**int**``& a,` `**int**``& b,` `**int**``& c) `

`{ `

`// Store XOR of all in a `

`a = a ^ b ^ c; `

`// After this, b has value of a `

`b = a ^ b ^ c; `

`// After this, c has value of b `

`c = a ^ b ^ c; `

`// After this, a has value of c `

`a = a ^ b ^ c; `

`} `

`// Function that swaps n integers `

`**void**` `rearrangeArray(``**int**` `a[],` `**int**` `n) `

`{ `

`**if**` `(n % 2 == 0) { `

`**for**` `(``**int**` `i = 0; i < n - 1; i += 2) { `

`// Swap 2 variables without `

`// using 3rd variable `

`swapTwo(a[i], a[i + 1]); `

`} `

`} `

`**else**` `{ `

`**for**` `(``**int**` `i = 0; i < n - 3; i += 2) { `

`// Swap 2 variables without `

`// using 3rd variable `

`swapTwo(a[i], a[i + 1]); `

`} `

`// The last 3 elements will not form `

`// pair if n is odd `

`// Hence, swapp 3 variables without `

`// using 4th variable `

`swapThree(a[n - 1], a[n - 2], a[n - 3]); `

`} `

`// Print the array elements `

`print(a, n); `

`} `

`// Driver Code `

`**int**` `main() `

`{ `

`// Given array arr[] `

`**int**` `arr[] = { 1, 2, 3, 4, 5 }; `

`**int**` `n =` `**sizeof**``(arr) /` `**sizeof**``(arr); `

`// Function Call `

`rearrangeArray(arr, n); `

`**return**` `0; `

`} `

Output:

``````2 1 4 5 3
``````

Time Complexity:_ O(N)_

Auxiliary Space:_ O(1)_

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

#arrays #greedy #mathematical #school programming #swap-program

## Buddha Community  1596988320

## Shuffle the position of each Array element by swapping adjacent elements

Given an array arr[], the task is to rearrange the array elements by swapping adjacent elements such that no element remains at same position after swapping.

Examples:

Input:_ arr[] = { 1, 2, 3, 4, 5 }_

Output:_ 2 1 5 3 4_

Explanation:

Adjacent elements are swapped as follows:

(1, 2 -> 2, 1)

(3, 4, 5 -> 5, 3, 4)

Input:_ arr[] = {1, 2, 3, 4}_

Output:_ 2 1 4 3_

Explanation:

Adjacent elements are swapped as follows:

1, 2 -> 2, 1

3, 4 -> 4, 3

Approach: The key observation in the problem is that there can be two cases for the arrays to swap the array elements:

Below is the implementation of the above approach:

• C++
• C#

`// C++ program for the above approach `

`#include <bits/stdc++.h> `

`**using**` `**namespace**` `std; `

`// Function to print the array `

`**void**` `print(``**int**` `a[],` `**int**` `n) `

`{ `

`// Loop to iterate over the `

`// elements of array `

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

`cout << a[i] <<` `" "``; `

`} `

`// Function to swap two variables without `

`// using the third variable `

`**void**` `swapTwo(``**int**``& x,` `**int**``& y) `

`{ `

`// Store XOR of all in x `

`x = x ^ y; `

`// After this, y has value of x `

`y = x ^ y; `

`// After this, x has value of y `

`x = x ^ y; `

`} `

`// Function to swap three variables `

`// without using fourth variable `

`**void**` `swapThree(``**int**``& a,` `**int**``& b,` `**int**``& c) `

`{ `

`// Store XOR of all in a `

`a = a ^ b ^ c; `

`// After this, b has value of a `

`b = a ^ b ^ c; `

`// After this, c has value of b `

`c = a ^ b ^ c; `

`// After this, a has value of c `

`a = a ^ b ^ c; `

`} `

`// Function that swaps n integers `

`**void**` `rearrangeArray(``**int**` `a[],` `**int**` `n) `

`{ `

`**if**` `(n % 2 == 0) { `

`**for**` `(``**int**` `i = 0; i < n - 1; i += 2) { `

`// Swap 2 variables without `

`// using 3rd variable `

`swapTwo(a[i], a[i + 1]); `

`} `

`} `

`**else**` `{ `

`**for**` `(``**int**` `i = 0; i < n - 3; i += 2) { `

`// Swap 2 variables without `

`// using 3rd variable `

`swapTwo(a[i], a[i + 1]); `

`} `

`// The last 3 elements will not form `

`// pair if n is odd `

`// Hence, swapp 3 variables without `

`// using 4th variable `

`swapThree(a[n - 1], a[n - 2], a[n - 3]); `

`} `

`// Print the array elements `

`print(a, n); `

`} `

`// Driver Code `

`**int**` `main() `

`{ `

`// Given array arr[] `

`**int**` `arr[] = { 1, 2, 3, 4, 5 }; `

`**int**` `n =` `**sizeof**``(arr) /` `**sizeof**``(arr); `

`// Function Call `

`rearrangeArray(arr, n); `

`**return**` `0; `

`} `

Output:

``````2 1 4 5 3
``````

Time Complexity:_ O(N)_

Auxiliary Space:_ O(1)_

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

#arrays #greedy #mathematical #school programming #swap-program 1623911281

## How to Print an Array in Java

#### Introduction

Printing an array is a quick way to give us visibility on the values of the contents inside. Sometimes the array values are the desired output of the program.

In this article, we’ll take a look at how to print an array in Java using four different ways.

While the “best way” depends on what your program needs to do, we begin with the simplest method for printing and then show more verbose ways to do it.

#java #array #how to print an array in java #array in java #print an array in java #print 1594211160

# Minimize swaps to rearrange array such that parity of index and corresponding element is same

Given an array **A[], **the task to find minimum swapping operations required to modify the given array A[] such that for every index in the array, parity(i) = parity(A[i]) where parity(x) = x % 2. If it’s impossible to obtain such an arrangement, then print -1.

Examples:

Input:_ A[] = { 2, 4, 3, 1, 5, 6 }_

Output:_ 2_

Explanation:

Swapping (4, 3) and (5, 6) modifies the array to [2, 3, 4, 1, 6, 5] such that the parity of i and A[i] is same for all indices.

Input:_ A[] = {1, 2, 5, 7}_

Output:_ -1_

Explanation:

The given array cannot be rearranged as per required condition.

Approach:

To solve the problem mentioned above an optimal approach is to choose such an index where parity(i) and parity(A[i]) aren’t same.

• Initialize two variables needodd and needeven to 0 which will store the parity of each element. Check the parity of index if it’s odd then increase needodd value by 1 otherwise increase needeven.
• If needodd and needeven aren’t same, then required arrangement is not possible.
• Otherwise, the final result is obtained by needodd variable, as it is the number of operations that are required. This is because, at any moment, we choose an odd element whose parity is not same with parity of their index and similarly choose an even element and swap them.

#arrays #array-rearrange #swap-program 1595334060

## Maximize distance between two elements of Array by at most X swaps

Given an array arr[] of unique elements and three integers XA and B. The task is to print the maximum possible distance between elements A and B in atmost X swaps with the adjacent elements.

Examples:

Input:_ arr[] = {5, 1, 3, 2}, X = 1, A = 2, B = 3_

Output:_ 2_

Explanation:

3 is swapped with it’s adjacent element 1. So, the distance between element 3 and 2 is 2 and no more interchanges are possible since X = 1.

Input:_ arr = {100, 33, 10, 1}, X = 5, A = 100, B = 1_

Output:_ 3_

Explanation:

As the elements are already the beginning and ending element, the distance between them is already maximized.

Approach: The following steps can be followed to compute the result:

• In case the given X is 0, then |index(A)-index(B)| is returned as final answer.
• If the elements are already at index 0 and n-1, return N-1 as the distance is already maximized.
• Else, the greater index element is swapped X times with their adjacent element till it is less than the size of the array.
• After reaching the end of the array if X > 0; then the smaller index element is swapped with it’s previous elements till X is not equal to 0.

Below is the implementation of the above approach:

filter_none

edit
play_arrow
brightness_4

`// C++ program for the above approach`

`#include <iostream>`

`**using**` `**namespace**` `std;`

`// Function to maximize the distance`

`// between the two elements of the`

`// array by at most X swaps`

`**void**` `max_distance(``**int**` `a[],` `**int**` `n,` `**int**` `x,`

`**int**` `A,` `**int**` `B)`

`{`

`// Initialize the variables`

`**int**` `g = 0, h = 0;`

`// Iterate till the length of the array`

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

`// Check if current element is A`

`**if**` `(a[i] == A)`

`// Store index`

`g = i;`

`// Check if current element is B`

`**if**` `(a[i] == B)`

`// Store index`

`h = i;`

`}`

`// If X = 0, swapping can not be done`

`**if**` `(x == 0) {`

`cout <<` `**abs**``(g - h);`

`}`

`// If elements are at starting and ending`

`// indices, then the distance`

`// is already maximum`

`**else**` `**if**` `((g == 0) && (h == (n - 1)))`

`cout << n - 1 << endl;`

`**else**` `**if**` `((g == n - 1) && (h == 0))`

`cout << n - 1 << endl;`

`**else**` `{`

`// Greater index is incremented`

`// till x > 0 and the`

`// index of element < size of array`

`**if**` `(h > g) {`

`**while**` `((x > 0) && (h < n - 1)) {`

`h++;`

`x--;`

`}`

`// Check if reached the size of array`

`// and x > 0, then the`

`// smaller index is decremented`

`**if**` `(x > 0) {`

`**while**` `((x > 0) && (g > 0)) {`

`g--;`

`x--;`

`}`

`}`

`cout << h - g << endl;`

`}`

`// Greater index is incremented till x>0`

`// and index of element < size of array`

`**else**` `{`

`**while**` `((x > 0) && (g < n - 1)) {`

`g++;`

`x--;`

`}`

`// Check if reached the size of the array`

`// and x > 0 the smaller index`

`// is decremented`

`**if**` `(x > 0) {`

`**while**` `((x > 0) && (h > 0)) {`

`h--;`

`x--;`

`}`

`}`

`cout << g - h << endl;`

`}`

`}`

`}`

`// Driver code`

`**int**` `main()`

`{`

`**int**` `a[] = { 100, 33, 10, 1 };`

`**int**` `x = 5, A = 100, B = 1;`

`**int**` `n =` `**sizeof**``(a) /` `**sizeof**``(a);`

`max_distance(a, n, x, A, B);`

`**return**` `0;`

`}`

Output:

``````3
``````

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

#arrays #swap-program #java #css 1595928176

## Count of right shifts for each array element to be in its sorted position

Given an array **arr[] **of size **N **that contains elements from the range [1, N], the task is to calculate the number of right shifts required for each element to reach its respective position if the array was sorted.

Examples:

_Input: __arr[] = {1, 4, 3, 2, 5}, N = 5 _

_Output: __0 2 0 3 0 _

Explanation:

_The sorted array is {1, 2, 3, 4, 5}. _

_4 is at index 1, so right shift 2 times to reach index 3. (1->2->3) _

_2 is at index 3, so right shift 3 times to reach index 1. (3->4->0->1) _

All the other elements are at their respective positions in the sorted array.

_Input: __arr[]={2, 4, 3, 1, 5}, N = 5 _

_Output: __2 1 0 2 0 _

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

**Approach: **

The idea is to calculate the difference between the actual position and the sorted position of each element of the array. As the elements are from 1 to**N, **the sorted position of each element can be determined without sorting the array. The sorted position of each element is given by **(arr[i]-1). **Therefore, the number of right shifts is given by **(arr[i] – 1 – i + N) % N.**

Below is the implementation of the above approach:

• C++

`// C++ Program to implement`

`// the approach`

`#include <bits/stdc++.h>`

`**using**` `**namespace**` `std;`

`// Function to find the right`

`// shifts required for each`

`// element to reach its sorted`

`// array position in A[]`

`**void**` `findShifts(``**int**` `A[],` `**int**` `N)`

`{`

`// Stores required number of`

`// shifts for each element`

`**int**` `shift[N];`

`**for**` `(``**int**` `i = 0; i < N; i++) {`

`// If the element is`

`// at sorted position`

`**if**` `(i == A[i] - 1)`

`shift[i] = 0;`

`// Otherwise`

`**else**`

`// Calculate right shift`

`shift[i]`

`= (A[i] - 1 - i + N)`

`% N;`

`}`

`// Print the respective shifts`

`**for**` `(``**int**` `i = 0; i < N; i++)`

`cout << shift[i] <<` `" "``;`

`}`

`// Driver Code`

`**int**` `main()`

`{`

`**int**` `arr[] = { 1, 4, 3, 2, 5 };`

`**int**` `N =` `**sizeof**``(arr) /` `**sizeof**``(arr);`

`findShifts(arr, N);`

`**return**` `0;`

`}`

Output:

``````0 2 0 3 0

``````

_Time Complexity: __O(N) _

_Auxiliary Space: _O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

#arrays #greedy #mathematical #array-rearrange