给定大小为N (N> 3)的数组arr [] ,任务是查找相对于所有其他数组元素而言奇偶性(奇/偶)不同的元素的位置。
注意:保证始终有一个与所有其他元素的奇偶校验数字不同的数字。
例子:
Input: arr[] = {2, 4, 7, 8, 10}
Output: 2
Explanation: The only odd element in the array is 7 (= arr[2]). Therefore, required output is 2.
Input: arr[] = {2, 1, 1}
Output: 0
天真的方法:解决此问题的最简单方法是将所有偶数和奇数及其索引存储在Multimap中,并打印存在于Map中大小为1的元素的索引。请按照以下步骤解决问题:
- 初始化两个Multimap ,以获取偶数和奇数。
- 遍历数组并将数组元素及其索引存储在其各自的Multimap中。
- 现在找到大小等于1的Multimap 。打印存储在该Multimap中的数组元素的索引。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to print the array
// element which differs in parity
// with the remaining array elements
int OddOneOut(int arr[], int N)
{
// Multimaps to store even
// and odd numbers along
// with their indices
multimap e, o;
// Traverse the array
for (int i = 0; i < N; i++) {
// If array element is even
if (arr[i] % 2 == 0) {
e.insert({ arr[i], i });
}
// Otherwise
else {
o.insert({ arr[i], i });
}
}
// If only one even element
// is present in the array
if (e.size() == 1) {
cout << e.begin()->second;
}
// If only one odd element
// is present in the array
else {
cout << o.begin()->second;
}
}
// Driver Code
int main()
{
// Given array
int arr[] = { 2, 4, 7, 8, 10 };
// Size of the array
int N = sizeof(arr) / sizeof(arr[0]);
OddOneOut(arr, N);
return 0;
}
Python3
# Python3 program for the above approach
# Function to print the array
# element which differs in parity
# with the remaining array elements
def OddOneOut(arr, N) :
# Multimaps to store even
# and odd numbers along
# with their indices
e, o = {}, {}
# Traverse the array
for i in range(N) :
# If array element is even
if (arr[i] % 2 == 0) :
e[arr[i]] = i
# Otherwise
else :
o[arr[i]] = i
# If only one even element
# is present in the array
if (len(e) == 1) :
print(list(e.values())[0] )
# If only one odd element
# is present in the array
else :
print(list(o.values())[0] )
# Given array
arr = [ 2, 4, 7, 8, 10 ]
# Size of the array
N = len(arr)
OddOneOut(arr, N)
# This code is contributed by divyesh072019.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
using System.Linq;
class GFG {
// Function to print the array
// element which differs in parity
// with the remaining array elements
static void OddOneOut(int[] arr, int N)
{
// Multimaps to store even
// and odd numbers along
// with their indices
Dictionary e = new Dictionary();
Dictionary o = new Dictionary();
// Traverse the array
for (int i = 0; i < N; i++) {
// If array element is even
if (arr[i] % 2 == 0) {
e[arr[i]] = i;
}
// Otherwise
else {
o[arr[i]] = i;
}
}
// If only one even element
// is present in the array
if (e.Count == 1) {
Console.Write(e.First().Value);
}
// If only one odd element
// is present in the array
else {
Console.Write(o.First().Value);
}
}
// Driver Code
public static void Main(string[] args)
{
// Given array
int[] arr = { 2, 4, 7, 8, 10 };
// Size of the array
int N = arr.Length;
OddOneOut(arr, N);
}
}
// This code is contributed by chitranayal.
C++
// C++ program for the above approach
#include
using namespace std;
// Function to print the element
// which differs in parity
int oddOneOut(int arr[], int N)
{
// Stores the count of odd and
// even array elements encountered
int odd = 0, even = 0;
// Stores the indices of the last
// odd and even array elements encountered
int lastOdd = 0, lastEven = 0;
// Traverse the array
for (int i = 0; i < N; i++) {
// If array element is even
if (arr[i] % 2 == 0) {
even++;
lastEven = i;
}
// Otherwise
else {
odd++;
lastOdd = i;
}
}
// If only one odd element
// is present in the array
if (odd == 1) {
cout << lastOdd << endl;
}
// If only one even element
// is present in the array
else {
cout << lastEven << endl;
}
}
// Driver Code
int main()
{
// Given array
int arr[] = { 2, 4, 7, 8, 10 };
// Size of the array
int N = sizeof(arr) / sizeof(arr[0]);
oddOneOut(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Function to print the element
// which differs in parity
static void oddOneOut(int arr[], int N)
{
// Stores the count of odd and
// even array elements encountered
int odd = 0, even = 0;
// Stores the indices of the last
// odd and even array elements encountered
int lastOdd = 0, lastEven = 0;
// Traverse the array
for (int i = 0; i < N; i++) {
// If array element is even
if (arr[i] % 2 == 0) {
even++;
lastEven = i;
}
// Otherwise
else {
odd++;
lastOdd = i;
}
}
// If only one odd element
// is present in the array
if (odd == 1) {
System.out.println(lastOdd);
}
// If only one even element
// is present in the array
else {
System.out.println(lastEven);
}
}
// Driver Code
public static void main(String args[])
{
// Given array
int arr[] = { 2, 4, 7, 8, 10 };
// Size of the array
int N = arr.length;
oddOneOut(arr, N);
}
}
// This code is contributed by jana_sayantan.
Python3
# Python program for the above approach
# Function to prthe element
# which differs in parity
def oddOneOut(arr, N) :
# Stores the count of odd and
# even array elements encountered
odd = 0
even = 0
# Stores the indices of the last
# odd and even array elements encountered
lastOdd = 0
lastEven = 0
# Traverse the array
for i in range(N):
# If array element is even
if (arr[i] % 2 == 0) :
even += 1
lastEven = i
# Otherwise
else :
odd += 1
lastOdd = i
# If only one odd element
# is present in the array
if (odd == 1) :
print(lastOdd)
# If only one even element
# is present in the array
else :
print(lastEven)
# Driver Code
# Given array
arr = [ 2, 4, 7, 8, 10 ]
# Size of the array
N = len(arr)
oddOneOut(arr, N)
# This code is contributed by susmitakundugoaldanga.
C#
// C# program for the above approach
using System;
class GFG
{
// Function to print the element
// which differs in parity
static void oddOneOut(int[] arr, int N)
{
// Stores the count of odd and
// even array elements encountered
int odd = 0, even = 0;
// Stores the indices of the last
// odd and even array elements encountered
int lastOdd = 0, lastEven = 0;
// Traverse the array
for (int i = 0; i < N; i++)
{
// If array element is even
if (arr[i] % 2 == 0)
{
even++;
lastEven = i;
}
// Otherwise
else
{
odd++;
lastOdd = i;
}
}
// If only one odd element
// is present in the array
if (odd == 1)
{
Console.WriteLine(lastOdd);
}
// If only one even element
// is present in the array
else
{
Console.WriteLine(lastEven);
}
}
// Driver code
static void Main()
{
// Given array
int[] arr = { 2, 4, 7, 8, 10 };
// Size of the array
int N = arr.Length;
oddOneOut(arr, N);
}
}
// This code is contributed by divyeshrabadiya07.
输出:
2
时间复杂度: O(N * logN)
辅助空间: O(N)
高效的方法:为了优化上述方法,我们的想法是在两个变量中保留一个偶数和奇数数组元素的计数,并检查哪个计数等于1 。请按照以下步骤解决问题:
- 维护四个变量even,odd以保持数组中偶数和奇数数组元素的数量,并保留lastOdd,lastEven来存储遇到的最后一个奇数和偶数数组元素的索引。
- 遍历数组后,如果发现奇数为1 ,则打印lastOdd 。
- 否则,打印lastEven 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to print the element
// which differs in parity
int oddOneOut(int arr[], int N)
{
// Stores the count of odd and
// even array elements encountered
int odd = 0, even = 0;
// Stores the indices of the last
// odd and even array elements encountered
int lastOdd = 0, lastEven = 0;
// Traverse the array
for (int i = 0; i < N; i++) {
// If array element is even
if (arr[i] % 2 == 0) {
even++;
lastEven = i;
}
// Otherwise
else {
odd++;
lastOdd = i;
}
}
// If only one odd element
// is present in the array
if (odd == 1) {
cout << lastOdd << endl;
}
// If only one even element
// is present in the array
else {
cout << lastEven << endl;
}
}
// Driver Code
int main()
{
// Given array
int arr[] = { 2, 4, 7, 8, 10 };
// Size of the array
int N = sizeof(arr) / sizeof(arr[0]);
oddOneOut(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Function to print the element
// which differs in parity
static void oddOneOut(int arr[], int N)
{
// Stores the count of odd and
// even array elements encountered
int odd = 0, even = 0;
// Stores the indices of the last
// odd and even array elements encountered
int lastOdd = 0, lastEven = 0;
// Traverse the array
for (int i = 0; i < N; i++) {
// If array element is even
if (arr[i] % 2 == 0) {
even++;
lastEven = i;
}
// Otherwise
else {
odd++;
lastOdd = i;
}
}
// If only one odd element
// is present in the array
if (odd == 1) {
System.out.println(lastOdd);
}
// If only one even element
// is present in the array
else {
System.out.println(lastEven);
}
}
// Driver Code
public static void main(String args[])
{
// Given array
int arr[] = { 2, 4, 7, 8, 10 };
// Size of the array
int N = arr.length;
oddOneOut(arr, N);
}
}
// This code is contributed by jana_sayantan.
Python3
# Python program for the above approach
# Function to prthe element
# which differs in parity
def oddOneOut(arr, N) :
# Stores the count of odd and
# even array elements encountered
odd = 0
even = 0
# Stores the indices of the last
# odd and even array elements encountered
lastOdd = 0
lastEven = 0
# Traverse the array
for i in range(N):
# If array element is even
if (arr[i] % 2 == 0) :
even += 1
lastEven = i
# Otherwise
else :
odd += 1
lastOdd = i
# If only one odd element
# is present in the array
if (odd == 1) :
print(lastOdd)
# If only one even element
# is present in the array
else :
print(lastEven)
# Driver Code
# Given array
arr = [ 2, 4, 7, 8, 10 ]
# Size of the array
N = len(arr)
oddOneOut(arr, N)
# This code is contributed by susmitakundugoaldanga.
C#
// C# program for the above approach
using System;
class GFG
{
// Function to print the element
// which differs in parity
static void oddOneOut(int[] arr, int N)
{
// Stores the count of odd and
// even array elements encountered
int odd = 0, even = 0;
// Stores the indices of the last
// odd and even array elements encountered
int lastOdd = 0, lastEven = 0;
// Traverse the array
for (int i = 0; i < N; i++)
{
// If array element is even
if (arr[i] % 2 == 0)
{
even++;
lastEven = i;
}
// Otherwise
else
{
odd++;
lastOdd = i;
}
}
// If only one odd element
// is present in the array
if (odd == 1)
{
Console.WriteLine(lastOdd);
}
// If only one even element
// is present in the array
else
{
Console.WriteLine(lastEven);
}
}
// Driver code
static void Main()
{
// Given array
int[] arr = { 2, 4, 7, 8, 10 };
// Size of the array
int N = arr.Length;
oddOneOut(arr, N);
}
}
// This code is contributed by divyeshrabadiya07.
输出:
2
时间复杂度: O(N)
辅助空间: O(1)