Open In App

Count subarrays with same even and odd elements

Last Updated : 20 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given an array of N integers, count number of even-odd subarrays. An even – odd subarray is a subarray that contains the same number of even as well as odd integers. 

Examples :  

Input : arr[] = {2, 5, 7, 8} 
Output : 3
Explanation : There are total 3 even-odd subarrays.
               1) {2, 5}
               2) {7, 8}
               3) {2, 5, 7, 8}

Input : arr[] = {3, 4, 6, 8, 1, 10} 
Output : 3
Explanation : In this case, 3 even-odd subarrays are:
               1) {3, 4}
               2) {8, 1}
               3) {1, 10}

This problem is mainly a variation of count subarrays with equal number of 0s and 1s.

A naive approach would be to check for all possible subarrays using two loops, whether they are even-odd subarrays or not. This approach will take O(N^2)                   time.

An Efficient approach solves the problem in O(N) time and it is based on following ideas:  

  • Even-odd subarrays will always be of even length.
  • Maintaining track of the difference between the frequency of even and odd integers.
  • Hashing of this difference of frequencies is useful in finding number of even-odd subarrays.
The basic idea is to use the difference between the frequency of odd and even numbers to obtain an optimal solution. 
We will maintain two integer hash arrays for the positive and negative value of the difference. 
-> Example to understand in better way : 
-> Consider difference = freq(odd) - freq(even) 
-> To calculate this difference, increment the value of 'difference' when there is 
an odd integer and decrement it when there is an even integer. (initially, difference = 0) 
arr[] = {3, 4, 6, 8, 1, 10}
index 0 1 2 3 4 5 6
array 3 4 6 8 1 10
difference 0 1 0 -1 -2 -1 -2
-> Observe that whenever a value 'k' repeats in the 'difference' array, there exists an 
even-odd subarray for each previous occurrence of that value i.e. subarray exists from 
index i + 1 to j where difference[i] = k and difference[j] = k. 
-> Value '0' is repeated in 'difference' array at index 2 and hence subarray exists for 
(0, 2] indexes. Similarly, for repetition of values '-1' (at indexes 3 and 5) and '-2' (at 
indexes 4 and 6), subarray exists for (3, 5] and (4, 6] indexes. 

Below is the implementation of the O(N) solution described above. 

C++

/*C++ program to find total number of
even-odd subarrays present in given array*/
#include <bits/stdc++.h>
using namespace std;
 
// function that returns the count of subarrays that
// contain equal number of odd as well as even numbers
int countSubarrays(int arr[], int n)
{
    // initialize difference and answer with 0
    int difference = 0;
    int ans = 0;
 
    // create two auxiliary hash arrays to count frequency
    // of difference, one array for non-negative difference
    // and other array for negative difference. Size of these
    // two auxiliary arrays is 'n+1' because difference can
    // reach maximum value 'n' as well as minimum value '-n'
    int hash_positive[n + 1], hash_negative[n + 1];
 
    // initialize these auxiliary arrays with 0
    fill_n(hash_positive, n + 1, 0);
    fill_n(hash_negative, n + 1, 0);
 
    // since the difference is initially 0, we have to
    // initialize hash_positive[0] with 1
    hash_positive[0] = 1;
 
    // for loop to iterate through whole
    // array (zero-based indexing is used)
    for (int i = 0; i < n ; i++)
    {
        // incrementing or decrementing difference based on
        // arr[i] being even or odd, check if arr[i] is odd
        if (arr[i] & 1 == 1)
            difference++;
        else
            difference--;
 
        // adding hash value of 'difference' to our answer
        // as all the previous occurrences of the same
        // difference value will make even-odd subarray
        // ending at index 'i'. After that, we will increment
        // hash array for that 'difference' value for
        // its occurrence at index 'i'. if difference is
        // negative then use hash_negative
        if (difference < 0)
        {
            ans += hash_negative[-difference];
            hash_negative[-difference]++;
        }
         
        // else use hash_positive
        else
        {
            ans += hash_positive[difference];
            hash_positive[difference]++;
        }
    }
 
    // return total number of even-odd subarrays
    return ans;
}
 
// Driver code
int main()
{
    int arr[] = {3, 4, 6, 8, 1, 10, 5, 7};
    int n = sizeof(arr) / sizeof(arr[0]);
     
    // Printing total number of even-odd subarrays
    cout << "Total Number of Even-Odd subarrays"
        " are " << countSubarrays(arr,n);
 
    return 0;
}
 
// This code is contributed by Aditya Kumar (adityakumar129)

                    

C

/*C program to find total number of
even-odd subarrays present in given array*/
#include <stdio.h>
 
// function that returns the count of subarrays that
// contain equal number of odd as well as even numbers
int countSubarrays(int arr[], int n)
{
    // initialize difference and answer with 0
    int difference = 0;
    int ans = 0;
 
    // create two auxiliary hash arrays to count frequency
    // of difference, one array for non-negative difference
    // and other array for negative difference. Size of
    // these two auxiliary arrays is 'n+1' because
    // difference can reach maximum value 'n' as well as
    // minimum value '-n'
    int hash_positive[n + 1], hash_negative[n + 1];
 
    // initialize these auxiliary arrays with 0
    for (int i = 0; i < n + 1; i++)
        hash_positive[i] = 0;
    for (int i = 0; i < n + 1; i++)
        hash_negative[i] = 0;
 
    // since the difference is initially 0, we have to
    // initialize hash_positive[0] with 1
    hash_positive[0] = 1;
 
    // for loop to iterate through whole
    // array (zero-based indexing is used)
    for (int i = 0; i < n; i++) {
        // incrementing or decrementing difference based on
        // arr[i] being even or odd, check if arr[i] is odd
        if (arr[i] & 1 == 1)
            difference++;
        else
            difference--;
 
        // adding hash value of 'difference' to our answer
        // as all the previous occurrences of the same
        // difference value will make even-odd subarray
        // ending at index 'i'. After that, we will
        // increment hash array for that 'difference' value
        // for its occurrence at index 'i'. if difference is
        // negative then use hash_negative
        if (difference < 0) {
            ans += hash_negative[-difference];
            hash_negative[-difference]++;
        }
 
        // else use hash_positive
        else {
            ans += hash_positive[difference];
            hash_positive[difference]++;
        }
    }
 
    // return total number of even-odd subarrays
    return ans;
}
 
// Driver code
int main()
{
    int arr[] = { 3, 4, 6, 8, 1, 10, 5, 7 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Printing total number of even-odd subarrays
    printf("Total Number of Even-Odd subarrays are %d ",
           countSubarrays(arr, n));
 
    return 0;
}
 
// This code is contributed by Aditya Kumar (adityakumar129)

                    

Java

// Java program to find total number of even-odd subarrays
// present in given array
class GFG {
 
    // function that returns the count of subarrays that
    // contain equal number of odd as well as even numbers
    static int countSubarrays(int[] arr, int n)
    {
        // initialize difference and answer with 0
        int difference = 0;
        int ans = 0;
 
        // create two auxiliary hash arrays to count
        // frequency of difference, one array for
        // non-negative difference and other array for
        // negative difference. Size of these two auxiliary
        // arrays is 'n+1' because difference can reach
        // maximum value 'n' as well as minimum value '-n'
        // initialize these auxiliary arrays with 0
        int[] hash_positive = new int[n + 1];
        int[] hash_negative = new int[n + 1];
 
        // since the difference is initially 0, we have to
        // initialize hash_positive[0] with 1
        hash_positive[0] = 1;
 
        // for loop to iterate through whole array
        // (zero-based indexing is used)
        for (int i = 0; i < n; i++) {
            // incrementing or decrementing difference based
            // on arr[i] being even or odd, check if arr[i]
            // is odd
            if ((arr[i] & 1) == 1)
                difference++;
            else
                difference--;
 
            // adding hash value of 'difference' to our
            // answer as all the previous occurrences of the
            // same difference value will make even-odd
            // subarray ending at index 'i'. After that, we
            // will increment hash array for that
            // 'difference' value for its occurrence at
            // index 'i'. if difference is negative then use
            // hash_negative
            if (difference < 0) {
                ans += hash_negative[-difference];
                hash_negative[-difference]++;
            } // else use hash_positive
            else {
                ans += hash_positive[difference];
                hash_positive[difference]++;
            }
        }
 
        // return total number of even-odd subarrays
        return ans;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int[] arr = new int[] { 3, 4, 6, 8, 1, 10, 5, 7 };
        int n = arr.length;
 
        // Printing total number of even-odd subarrays
        System.out.println(
            "Total Number of Even-Odd subarrays are "
            + countSubarrays(arr, n));
    }
}
 
// This code is contributed by Aditya Kumar (adityakumar129)

                    

Python3

# Python3 program to find total
# number of even-odd subarrays
# present in given array
 
# function that returns the count
# of subarrays that contain equal
# number of odd as well as even numbers
def countSubarrays(arr, n):
 
    # initialize difference and
    # answer with 0
    difference = 0
    ans = 0
 
    # create two auxiliary hash
    # arrays to count frequency
    # of difference, one array
    # for non-negative difference
    # and other array for negative
    # difference. Size of these two
    # auxiliary arrays is 'n+1'
    # because difference can reach
    # maximum value 'n' as well as
    # minimum value '-n'
    hash_positive = [0] * (n + 1)
    hash_negative = [0] * (n + 1)
 
    # since the difference is
    # initially 0, we have to
    # initialize hash_positive[0] with 1
    hash_positive[0] = 1
 
    # for loop to iterate through
    # whole array (zero-based
    # indexing is used)
    for i in range(n):
     
        # incrementing or decrementing
        # difference based on arr[i]
        # being even or odd, check if
        # arr[i] is odd
        if (arr[i] & 1 == 1):
            difference = difference + 1
        else:
            difference = difference - 1
 
        # adding hash value of 'difference'
        # to our answer as all the previous
        # occurrences of the same difference
        # value will make even-odd subarray
        # ending at index 'i'. After that,
        # we will increment hash array for
        # that 'difference' value for
        # its occurrence at index 'i'. if
        # difference is negative then use
        # hash_negative
        if (difference < 0):
            ans += hash_negative[-difference]
            hash_negative[-difference] = hash_negative[-difference] + 1
         
        # else use hash_positive
        else:
            ans += hash_positive[difference]
            hash_positive[difference] = hash_positive[difference] + 1
 
    # return total number of
    # even-odd subarrays
    return ans
 
# Driver code
arr = [3, 4, 6, 8, 1, 10, 5, 7]
n = len(arr)
 
# Printing total number
# of even-odd subarrays
print("Total Number of Even-Odd subarrays are " +
                    str(countSubarrays(arr, n)))
 
# This code is contributed
# by Yatin Gupta

                    

C#

// C# program to find total
// number of even-odd subarrays
// present in given array
using System;
 
class GFG
{
    // function that returns the
    // count of subarrays that
    // contain equal number of
    // odd as well as even numbers
    static int countSubarrays(int []arr,
                              int n)
    {
        // initialize difference
        // and answer with 0
        int difference = 0;
        int ans = 0;
     
        // create two auxiliary hash
        // arrays to count frequency
        // of difference, one array
        // for non-negative difference
        // and other array for negative
        // difference. Size of these
        // two auxiliary arrays is 'n+1'
        // because difference can
        // reach maximum value 'n' as
        // well as minimum value '-n'
        int []hash_positive = new int[n + 1];
        int []hash_negative = new int[n + 1];
     
        // initialize these
        // auxiliary arrays with 0
        Array.Clear(hash_positive, 0, n + 1);
        Array.Clear(hash_negative, 0, n + 1);
     
        // since the difference is
        // initially 0, we have to
        // initialize hash_positive[0] with 1
        hash_positive[0] = 1;
     
        // for loop to iterate
        // through whole array
        // (zero-based indexing is used)
        for (int i = 0; i < n ; i++)
        {
            // incrementing or decrementing
            // difference based on
            // arr[i] being even or odd,
            // check if arr[i] is odd
            if ((arr[i] & 1) == 1)
                difference++;
            else
                difference--;
     
            // adding hash value of 'difference'
            // to our answer as all the previous
            // occurrences of the same difference
            // value will make even-odd subarray
            // ending at index 'i'. After that,
            // we will increment hash array for
            // that 'difference' value for its
            // occurrence at index 'i'. if
            // difference is negative then use
            // hash_negative
            if (difference < 0)
            {
                ans += hash_negative[-difference];
                hash_negative[-difference]++;
            }
             
            // else use hash_positive
            else
            {
                ans += hash_positive[difference];
                hash_positive[difference]++;
            }
        }
     
        // return total number
        // of even-odd subarrays
        return ans;
    }
     
    // Driver code
    static void Main()
    {
        int []arr = new int[]{3, 4, 6, 8,
                              1, 10, 5, 7};
        int n = arr.Length;
         
        // Printing total number
        // of even-odd subarrays
        Console.Write("Total Number of Even-Odd" +
                               " subarrays are " +
                           countSubarrays(arr,n));
    }
}
 
// This code is contributed by
// Manish Shaw(manishshaw1)

                    

PHP

<?php
// PHP program to find total number of
// even-odd subarrays present in given array
 
// function that returns the count of subarrays
// that contain equal number of odd as well
// as even numbers
function countSubarrays(&$arr, $n)
{
    // initialize difference and
    // answer with 0
    $difference = 0;
    $ans = 0;
 
    // create two auxiliary hash arrays to count
    // frequency of difference, one array for
    // non-negative difference and other array
    // for negative difference. Size of these
    // two auxiliary arrays is 'n+1' because
    // difference can reach maximum value 'n'
    // as well as minimum value '-n'
    $hash_positive = array_fill(0, $n + 1, NULL);
    $hash_negative = array_fill(0, $n + 1, NULL);
 
    // since the difference is initially 0, we
    // have to initialize hash_positive[0] with 1
    $hash_positive[0] = 1;
 
    // for loop to iterate through whole
    // array (zero-based indexing is used)
    for ($i = 0; $i < $n ; $i++)
    {
        // incrementing or decrementing difference
        // based on arr[i] being even or odd, check
        // if arr[i] is odd
        if ($arr[$i] & 1 == 1)
            $difference++;
        else
            $difference--;
 
        // adding hash value of 'difference' to our
        // answer as all the previous occurrences of
        // the same difference value will make even-odd
        // subarray ending at index 'i'. After that, we
        // will increment hash array for that 'difference'
        // value for its occurrence at index 'i'. if
        // difference is negative then use hash_negative
        if ($difference < 0)
        {
            $ans += $hash_negative[-$difference];
            $hash_negative[-$difference]++;
        }
         
        // else use hash_positive
        else
        {
            $ans += $hash_positive[$difference];
            $hash_positive[$difference]++;
        }
    }
 
    // return total number of even-odd
    // subarrays
    return $ans;
}
 
// Driver code
$arr = array(3, 4, 6, 8, 1, 10, 5, 7);
$n = sizeof($arr);
 
// Printing total number of even-odd
// subarrays
echo "Total Number of Even-Odd subarrays".
     " are " . countSubarrays($arr, $n);
 
// This code is contributed by ita_c
?>

                    

Javascript

<script>
// Javascript program to find total
// number of even-odd subarrays
// present in given array
     
    // function that returns the
    // count of subarrays that
    // contain equal number of
    // odd as well as even numbers
    function countSubarrays(arr, n)
    {
     
        // initialize difference
        // and answer with 0
        let difference = 0;
        let ans = 0;
   
        // create two auxiliary hash
        // arrays to count frequency
        // of difference, one array
        // for non-negative difference
        // and other array for negative
        // difference. Size of these
        // two auxiliary arrays is 'n+1'
        // because difference can
        // reach maximum value 'n' as
        // well as minimum value '-n'
        // initialize these
        // auxiliary arrays with 0
        let hash_positive = new Array(n + 1);
        let hash_negative = new Array(n + 1);
        for(let i=0;i<n+1;i++)
        {
            hash_positive[i] = 0;
            hash_negative[i] = 0;
             
        }
   
        // since the difference is
        // initially 0, we have to
        // initialize hash_positive[0] with 1
        hash_positive[0] = 1;
   
        // for loop to iterate
        // through whole array
        // (zero-based indexing is used)
        for (let i = 0; i < n; i++)
        {
         
            // incrementing or decrementing
            // difference based on
            // arr[i] being even or odd,
            // check if arr[i] is odd
            if ((arr[i] & 1) == 1)
            {
                difference++;
            }
            else
            {
                difference--;
            }
   
            // adding hash value of 'difference'
            // to our answer as all the previous
            // occurrences of the same difference
            // value will make even-odd subarray
            // ending at index 'i'. After that,
            // we will increment hash array for
            // that 'difference' value for its
            // occurrence at index 'i'. if
            // difference is negative then use
            // hash_negative
            if (difference < 0)
            {
                ans += hash_negative[-difference];
                hash_negative[-difference]++;
            }
             
            // else use hash_positive
            else
            {
                ans += hash_positive[difference];
                hash_positive[difference]++;
            }
        }
   
        // return total number
        // of even-odd subarrays
        return ans;
    }
     
    // Driver code
    let arr = [3, 4, 6, 8,
            1, 10, 5, 7];
    let n = arr.length;
     
    // Printing total number
        // of even-odd subarrays
    document.write("Total Number of Even-Odd"
                + " subarrays are "
                + countSubarrays(arr, n));
             
    // This code is contributed by avanitrachhadiya2155
</script>

                    

Output
Total Number of Even-Odd subarrays are 7

Complexity Analysis:

  • Time Complexity: O(N), where N is the number of integers. 
  • Auxiliary Space: O(2N), where N is the number of integers.

Another approach:- This approach is much simpler and easy to understand. It can be solved easily by using the same concept of 
Count subarrays with equal numbers of 1’s and 0’s.  Change the odd elements to -1 and even elements to 1. And now count the ways to find sum=0;

Implementation:

C++

#include <bits/stdc++.h>
using namespace std;
// function to count the subarrays having equal number of
// even and odd
long long int countSubarrays(int arr[], int n)
{
    long long int k = 0, currsum = 0, count = 0;
    unordered_map<int, int> map;
    for (int i = 0; i < n; i++) {
        if (arr[i] % 2 == 0)
            arr[i] = 1;
        else if (arr[i] % 2 != 0)
            arr[i] = -1;
        currsum += arr[i];
        if (currsum == k)
            count++;
        if (map.find(currsum - k) != map.end()) {
            count += map[currsum - k];
        }
        map[currsum]++;
    }
   // return total number of even-odd subarrays
    return count;
}
 
// Driver code
int main()
{
    int arr[] = { 3, 4, 6, 8, 1, 10, 5, 7 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Printing total number of even-odd subarrays
    cout << "Total Number of Even-Odd subarrays"
            " are "
         << countSubarrays(arr, n);
}
// this code is contributed by naveen shah

                    

Java

/*package whatever //do not write package name here */
 
import java.util.*;
 
class GFG {
  // function to count the subarrays having equal number of
// even and odd
static long countSubarrays(int arr[], int n)
{
    long k = 0, currsum = 0, count = 0;
    HashMap<Long, Integer> map = new HashMap<>();
    for (int i = 0; i < n; i++) {
        if (arr[i] % 2 == 0)
            arr[i] = 1;
        else if (arr[i] % 2 != 0)
            arr[i] = -1;
        currsum += arr[i];
        if (currsum == k)
            count++;
        if (map.containsKey(currsum - k)) {
            count += map.get(currsum - k);
        }
      map.put(currsum,map.getOrDefault(currsum,0)+1);
      
    }
   // return total number of even-odd subarrays
    return count;
}
   
    public static void main (String[] args) {
        int arr[] = { 3, 4, 6, 8, 1, 10, 5, 7 };
    int n = arr.length;
 
    // Printing total number of even-odd subarrays
    System.out.println("Total Number of Even-Odd subarrays are " + countSubarrays(arr, n));
 
    }
}

                    

Python3

# function to count the subarrays having equal number of
# even and odd
def countSubarrays(arr, n):
    k = 0
    currsum = 0
    count = 0
    map = {}
    for i in range(n):
        if arr[i] % 2 == 0:
            arr[i] = 1
        else:
            arr[i] = -1
        currsum += arr[i]
        if currsum == k:
            count += 1
        if currsum - k in map:
            count += map[currsum - k]
        if currsum not in map:
            map[currsum] = 1
        else:
            map[currsum] += 1
             
    # return total number of even-odd subarrays
    return count
 
arr = [3, 4, 6, 8, 1, 10, 5, 7]
n = len(arr)
 
# Printing total number of even-odd subarrays
print("Total Number of Even-Odd subarrays are", countSubarrays(arr, n))
 
# this code is contributed by akashish__

                    

C#

// Include namespace system
using System;
using System.Collections.Generic;
 
using System.Collections;
 
 
public class GFG
{
  // function to count the subarrays having equal number of
  // even and odd
  public static long countSubarrays(int[] arr, int n)
  {
    var k = 0;
    var currsum = 0;
    var count = 0;
    var map = new Dictionary<long, int>();
    for (int i = 0; i < n; i++)
    {
      if (arr[i] % 2 == 0)
      {
        arr[i] = 1;
      }
      else if (arr[i] % 2 != 0)
      {
        arr[i] = -1;
      }
      currsum += arr[i];
      if (currsum == k)
      {
        count++;
      }
      if (map.ContainsKey(currsum - k))
      {
        count += map[currsum - k];
      }
      map[currsum] = (map.ContainsKey(currsum) ? map[currsum] : 0) + 1;
    }
    // return total number of even-odd subarrays
    return count;
  }
  public static void Main(String[] args)
  {
    int[] arr = {3, 4, 6, 8, 1, 10, 5, 7};
    var n = arr.Length;
    // Printing total number of even-odd subarrays
    Console.WriteLine("Total Number of Even-Odd subarrays are " + GFG.countSubarrays(arr, n).ToString());
  }
}
 
// This code is contributed by aadityaburujwale.

                    

Javascript

// JavaScript Code implementation
 
// function to count the subarrays having equal number of
// even and odd
function countSubarrays(arr, n){
    var k = 0, currsum = 0, count = 0;
     
    var map = new Map();
     
    for(let i=0;i<n;i++){
        if(arr[i]%2==0){
            arr[i] = 1;
        }
        else if(arr[i]%2!=0){
            arr[i] = -1;
        }
        currsum += arr[i];
        if(currsum == k){
            count++;
        }
        if(map.has(currsum-k)){
            count += map.get(currsum-k);
        }
        var temp = (map.has(currsum) ? map.get(currsum) : 0) + 1;
        map.set(currsum, temp);
    }
     
    // return total number of even-odd subarrays
    return count;
}
 
var arr = [ 3, 4, 6, 8, 1, 10, 5, 7 ];
var n = arr.length;
 
// Printing total number of even-odd subarrays
console.log("Total Number of Even-Odd subarrays are " + countSubarrays(arr, n));
 
 
// This code is contributed by lokesh.

                    

Output
Total Number of Even-Odd subarrays are 7

Complexity Analysis:

  • Time Complexity: O(N). 
  • Auxiliary Space: O(N).


Similar Reads

Make all the elements of array odd by incrementing odd-indexed elements of odd-length subarrays
Given an array arr[] of size N, the task is to make all the array elements odd by choosing an odd length subarray of arr[] and increment all odd positioned elements by 1 in this subarray. Print the count of such operations required. Examples: Input: arr[] = {2, 3, 4, 3, 5, 3, 2}Output: 2Explanation:In first operation, choose the subarray {2, 3, 4}
9 min read
Check if a number has an odd count of odd divisors and even count of even divisors
Given an integer N, the task is to check if N has an odd number of odd divisors and even number of even divisors. Examples: Input: N = 36Output: YesExplanation:Divisors of 36 = 1, 2, 3, 4, 6, 9, 12, 18, 36Count of Odd Divisors(1, 3, 9) = 3 [Odd]Count of Even Divisors(2, 4, 6, 12, 18, 36) = 6 [Even] Input: N = 28Output: No Naive Approach: The idea i
9 min read
Absolute difference between sum of even elements at even indices & odd elements at odd indices in given Array
Given an array arr[] containing N elements, the task is to find the absolute difference between the sum of even elements at even indices &amp; the count of odd elements at odd indices. Consider 1-based indexing Examples: Input: arr[] = {3, 4, 1, 5}Output: 0Explanation: Sum of even elements at even indices: 4 {4}Sum of odd elements at odd indices: 4
5 min read
Find the nearest odd and even perfect squares of odd and even array elements respectively
Given an array arr[ ] of size N, the task for each array element is to print the nearest perfect square having same parity. Examples: Input: arr[ ] = {6, 3, 2, 15}Output: 4 1 4 9Explanation:The nearest even perfect square of arr[0] (= 6) is 4.The nearest odd perfect square of arr[1] (= 3) is 1.The nearest even perfect square of arr[2] (= 2) is 4The
5 min read
Count of integers in a range which have even number of odd digits and odd number of even digits
Given a range [L, R], the task is to count the numbers which have even number of odd digits and odd number of even digits. For example, 8 has 1 even digit and 0 odd digit - Satisfies the condition since 1 is odd and 0 is even.545 has 1 even digit and 2 odd digits - Satisfies the condition since 1 is odd and 2 is even.4834 has 3 even digits and 1 od
11 min read
Count numbers from given range having odd digits at odd places and even digits at even places
Given two integers L and R, the task is to count numbers from the range [L, R] having odd digits at odd positions and even digits at even positions respectively. Examples: Input: L = 3, R = 25Output: 9Explanation: The numbers satisfying the conditions are 3, 5, 7, 9, 10, 12, 14, 16 and 18. Input: L = 128, R = 162Output: 7Explanation: The numbers sa
29 min read
Split an Array to maximize subarrays having equal count of odd and even elements for a cost not exceeding K
Given an array arr[] of size N and an integer K, the task is to split the given array into maximum possible subarrays having equal count of even and odd elements such that the cost to split the array does not exceed K. The cost to split an array into a subarray is the difference between the last and first elements of the subarrays respectively. Exa
8 min read
Count subarrays having sum of elements at even and odd positions equal
Given an array arr[] of integers, the task is to find the total count of subarrays such that the sum of elements at even position and sum of elements at the odd positions are equal. Examples: Input: arr[] = {1, 2, 3, 4, 1}Output: 1Explanation: {3, 4, 1} is the only subarray in which sum of elements at even position {3, 1} = sum of element at odd po
7 min read
Modify Binary Tree by replacing all nodes at even and odd levels by their nearest even or odd perfect squares respectively
Given a Binary Tree consisting of N nodes, the task is to replace all the nodes that are present at even-numbered levels in a Binary Tree with their nearest even perfect square and replace nodes at odd-numbered levels with their nearest odd perfect square. Examples: Input: 5 / \ 3 2 / \ 16 19 Output: 9 / \ 4 4 / \ 9 25 Explanation: Level 1: Nearest
13 min read
Even numbers at even index and odd numbers at odd index
Given an array of size n containing equal number of odd and even numbers. The problem is to arrange the numbers in such a way that all the even numbers get the even index and odd numbers get the odd index. Required auxiliary space is O(1).Examples : Input : arr[] = {3, 6, 12, 1, 5, 8} Output : 6 3 12 1 8 5 Input : arr[] = {10, 9, 7, 18, 13, 19, 4,
11 min read
Article Tags :