Next same parity element in Circular Array
Last Updated :
21 Sep, 2022
Given a circular array arr[] of size N, the task is to find the next integers of same parity for every element in arr[]. If the next integers with the same parity does not exist, return -1 for that number.
Examples:
Input: arr[] = {2, 4, 3, 6, 5}
Output: 4 6 5 2 3
Explanation: For 2 the next element with the same parity is 4.
For 4 the next element with the same parity is 6.
For 3 the next element with the same parity is 5.
For 6 the next element with the same parity is 2.
For 5 the next element with the same parity is 3.
Input: arr[] = {5, 4, 7, 6}
Output: 7 6 5 4
Approach: The idea to solve the problem is as follows:
Iterate from the back of the array. If the arr[i] is odd, then it will act as the next odd element for any odd integer situated at j where j < i and j is closest to i. The same is true for the even elements also.
Follow the below steps to solve the problem:
- Initialize two variables to store the next odd(Next_Odd) and next even(Next_Even) for current array element.
- Iterate from i = 2*n-1 to 0:
- If i ≥ n, then the loop is used to find the next odd and even for the last odd and even element of the array.
- Otherwise, if arr[i] is odd and another odd element is present, put Next_Odd in resultant array and update Next_Odd to be arr[i].
- If arr[i] is even and another even element is present, put Next_Even in resultant array and update Next_Even to be arr[i].
- Return the resultant array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
vector< int > findElement( int * arr, int n)
{
vector< int > SPE(n, -1);
int Next_Even, Next_Odd;
int Count_Even = 0, Count_Odd = 0;
for ( int i = 2 * n - 1; i >= 0; i--) {
if (i >= n) {
if (arr[i % n] & 1) {
Next_Odd = arr[i % n];
Count_Odd++;
}
else {
Next_Even = arr[i % n];
Count_Even++;
}
}
else {
if (arr[i] & 1) {
if (Count_Odd > 1) {
SPE[i] = Next_Odd;
Next_Odd = arr[i];
}
}
else {
if (Count_Even > 1) {
SPE[i] = Next_Even;
Next_Even = arr[i];
}
}
}
}
return SPE;
}
int main()
{
int arr[] = { 2, 4, 3, 6, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
vector< int > v = findElement(arr, N);
for ( auto i : v) {
cout << i << " " ;
}
}
|
Java
import java.util.*;
public class GFG {
static int [] findElement( int [] arr, int n)
{
int [] SPE = new int [n];
for ( int i = 0 ; i < n; i++) {
SPE[i] = - 1 ;
}
int Next_Even = 0 , Next_Odd = 0 ;
int Count_Even = 0 , Count_Odd = 0 ;
for ( int i = 2 * n - 1 ; i >= 0 ; i--) {
if (i >= n) {
if ((arr[i % n] & 1 ) == 1 ) {
Next_Odd = arr[i % n];
Count_Odd++;
}
else {
Next_Even = arr[i % n];
Count_Even++;
}
}
else {
if ((arr[i] & 1 ) == 1 ) {
if (Count_Odd > 1 ) {
SPE[i] = Next_Odd;
Next_Odd = arr[i];
}
}
else {
if (Count_Even > 1 ) {
SPE[i] = Next_Even;
Next_Even = arr[i];
}
}
}
}
return SPE;
}
public static void main(String args[])
{
int arr[] = { 2 , 4 , 3 , 6 , 5 };
int N = arr.length;
int [] v = findElement(arr, N);
for ( int i = 0 ; i < v.length; i++) {
System.out.print(v[i] + " " );
}
}
}
|
Javascript
<script>
function findElement(arr, n) {
let SPE = new Array(n).fill(-1);
let Next_Even, Next_Odd;
let Count_Even = 0, Count_Odd = 0;
for (let i = 2 * n - 1; i >= 0; i--) {
if (i >= n) {
if (arr[i % n] & 1) {
Next_Odd = arr[i % n];
Count_Odd++;
}
else {
Next_Even = arr[i % n];
Count_Even++;
}
}
else {
if (arr[i] & 1) {
if (Count_Odd > 1) {
SPE[i] = Next_Odd;
Next_Odd = arr[i];
}
}
else {
if (Count_Even > 1) {
SPE[i] = Next_Even;
Next_Even = arr[i];
}
}
}
}
return SPE;
}
let arr = [2, 4, 3, 6, 5];
let N = arr.length;
let v = findElement(arr, N);
for (let i of v) {
document.write(i + " " );
}
</script>
|
Python3
def findElement(arr, n):
SPE = [ - 1 ] * n
Next_Even = None
Next_Odd = None
Count_Even = 0
Count_Odd = 0
for i in range ( 2 * n - 1 , - 1 , - 1 ):
if (i > = n):
if (arr[i % n] & 1 ):
Next_Odd = arr[i % n]
Count_Odd + = 1
else :
Next_Even = arr[i % n]
Count_Even + = 1
else :
if (arr[i] & 1 ):
if (Count_Odd > 1 ):
SPE[i] = Next_Odd
Next_Odd = arr[i]
else :
if (Count_Even > 1 ):
SPE[i] = Next_Even
Next_Even = arr[i]
return SPE
arr = [ 2 , 4 , 3 , 6 , 5 ]
N = len (arr)
v = findElement(arr, N)
for i in v:
print (i, end = " " )
|
C#
using System;
public class GFG{
static public void Main (){
}
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...