Split the array into odd number of segments of odd lengths
Last Updated :
02 Nov, 2023
Given an array of n length. The task is to check if the given array can be split into an odd number of sub-segment starting with odd integer and also the length of sub-segment must be odd. If possible the print ‘1’ else print ‘0’.
Examples:
Input: arr[] = {1, 3, 1}
Output: 1
1, 3, 1, can be split into 3 sub-segments of length odd i.e. 1
with starting and ending elements as odd.
Input: arr[] = {1, 3, 1, 1}
Output: 0
Approach:
Points to think before proceeding the actual solution:
- Order of sets made by adding an even number of the set with odd order is always even and vice-versa.
- Order of sets made by adding an odd number of the set with odd order is always odd and vice-versa.
Using the above lemma of number theory, the solution of the given problem can be easily calculated with below-proposed logic:
If the given array starts and ends with an odd integer and also the size of the array is odd then only the given array can be broken down into an odd number of sub-segments starting & ending with an odd number with odd size, else not.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
bool checkArray( int arr[], int n)
{
return (arr[0] % 2) && (arr[n - 1] % 2) && (n % 2);
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << ( int )checkArray(arr, n);
return 0;
}
|
Java
class GFG
{
static int checkArray( int []arr, int n)
{
return ((arr[ 0 ] % 2 ) > 0 &&
(arr[n - 1 ] % 2 ) > 0 &&
(n % 2 ) > 0 ) ? 1 : 0 ;
}
public static void main(String[] args)
{
int []arr = { 1 , 2 , 3 , 4 , 5 };
int n = arr.length;
System.out.println(checkArray(arr, n));
}
}
|
Python3
def checkArray(arr, n):
return ((arr[ 0 ] % 2 ) and
(arr[n - 1 ] % 2 ) and (n % 2 ))
arr = [ 1 , 2 , 3 , 4 , 5 ]
n = len (arr);
if checkArray(arr, n):
print ( 1 )
else :
print ( 0 )
|
C#
using System;
class GFG
{
static int checkArray( int []arr, int n)
{
return ((arr[0] % 2) > 0 &&
(arr[n - 1] % 2) > 0 &&
(n % 2) > 0) ? 1 : 0;
}
static void Main()
{
int []arr = { 1, 2, 3, 4, 5 };
int n = arr.Length;
Console.WriteLine(checkArray(arr, n));
}
}
|
Javascript
<script>
function checkArray(arr, n)
{
return ((arr[0] % 2) > 0 &&
(arr[n - 1] % 2) > 0 &&
(n % 2) > 0) ? 1 : 0;
}
var arr = [ 1, 2, 3, 4, 5 ];
var n = arr.length;
document.write(checkArray(arr, n));
</script>
|
PHP
<?php
function checkArray( $arr , $n )
{
return ( $arr [0] % 2) &&
( $arr [ $n - 1] % 2) && ( $n % 2);
}
$arr = array ( 1, 2, 3, 4, 5 );
$n = sizeof( $arr );
echo checkArray( $arr , $n );
?>
|
Output:
1
Time Complexity: O(1), since there is only basic arithmetic that takes constant time.
Auxiliary Space: O(1), since no extra space has been taken.
Approach#2: Using Greedy Approach
We can use a greedy approach to split the array into odd number of segments of odd lengths. We can start with the first element of the array and keep adding odd-length segments until we reach the end of the array.
Algorithm
1. Initialize a variable ‘count’ to 0.
2. Initialize a variable ‘i’ to 0.
3. Iterate over the array from index 0 to n-1, where n is the length of the array.
4. If the current element is odd and the sum of its index and value is odd, increment ‘count’.
5. If ‘count’ is odd and we have reached the end of the array, return 1, else return 0.
C++
#include <iostream>
#include <vector>
using namespace std;
int splitArrayIntoOddSegments(vector< int >& arr) {
int count = 0;
for ( int i = 0; i < arr.size(); i++) {
if (arr[i] % 2 != 0 && (i + arr[i]) % 2 != 0) {
count += 1;
}
}
if (count % 2 != 0 && arr.size() % 2 != 0) {
return 1;
} else {
return 0;
}
}
int main() {
vector< int > arr = {1, 2, 3, 4, 5};
cout << splitArrayIntoOddSegments(arr) << endl;
return 0;
}
|
Java
public class Main {
public static void main(String[] args) {
int [] arr = { 1 , 2 , 3 , 4 , 5 };
System.out.println(splitArrayIntoOddSegments(arr));
}
public static int splitArrayIntoOddSegments( int [] arr) {
int count = 0 ;
for ( int i = 0 ; i < arr.length; i++) {
if (arr[i] % 2 != 0 && (i + arr[i]) % 2 != 0 ) {
count++;
}
}
if (count % 2 != 0 && arr.length % 2 != 0 ) {
return 1 ;
} else {
return 0 ;
}
}
}
|
Python3
def split_array_into_odd_segments(arr):
count = 0
for i in range ( len (arr)):
if arr[i] % 2 ! = 0 and (i + arr[i]) % 2 ! = 0 :
count + = 1
if count % 2 ! = 0 and len (arr) % 2 ! = 0 :
return 1
else :
return 0
arr = [ 1 , 2 , 3 , 4 , 5 ]
print ( split_array_into_odd_segments(arr))
|
C#
using System;
using System.Collections.Generic;
namespace SplitArrayIntoOddSegments {
class Program {
static int SplitArrayIntoOddSegments(List< int > arr)
{
int count = 0;
for ( int i = 0; i < arr.Count; i++) {
if (arr[i] % 2 != 0 && (i + arr[i]) % 2 != 0) {
count += 1;
}
}
if (count % 2 != 0 && arr.Count % 2 != 0) {
return 1;
}
else {
return 0;
}
}
static void Main( string [] args)
{
List< int > arr = new List< int >{ 1, 2, 3, 4, 5 };
Console.WriteLine(SplitArrayIntoOddSegments(arr));
}
}
}
|
Javascript
function splitArrayIntoOddSegments(arr) {
let count = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i] % 2 !== 0 && (i + arr[i]) % 2 !== 0) {
count += 1;
}
}
if (count % 2 !== 0 && arr.length % 2 !== 0) {
return 1;
} else {
return 0;
}
}
const arr = [1, 2, 3, 4, 5];
console.log(splitArrayIntoOddSegments(arr));
|
Time Complexity: O(n), where n is the length of the array.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...