Related Articles

# Split the array into odd number of segments of odd lengths

• Last Updated : 22 Jun, 2021

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```

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

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.

Below is the implementation of the above approach:

## C++

 `// CPP to check whether given``// array is breakable or not``#include ``using` `namespace` `std;``  ` `// Function to check``bool` `checkArray(``int` `arr[], ``int` `n)``{``    ``// Check the result by processing``    ``// the first & last element and size``    ``return` `(arr % 2) && (arr[n - 1] % 2) && (n % 2);``}``  ` `// Driver code``int` `main()``{``    ``int` `arr[] = { 1, 2, 3, 4, 5 };``    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr);``    ``cout << (``int``)checkArray(arr, n);``  ` `    ``return` `0;``}`

## Java

 `// Java to check whether given``// array is breakable or not``  ` `class` `GFG``{``      ` `// Function to check``static` `int` `checkArray(``int` `[]arr, ``int` `n)``{``    ``// Check the result by processing``    ``// the first & last element and size``    ``return` `((arr[``0``] % ``2``) > ``0` `&&``            ``(arr[n - ``1``] % ``2``) > ``0` `&&``            ``(n % ``2``) > ``0``) ? ``1` `: ``0``;``}``  ` `// Driver code``public` `static` `void` `main(String[] args)``{``    ``int` `[]arr = { ``1``, ``2``, ``3``, ``4``, ``5` `};``    ``int` `n = arr.length;``    ``System.out.println(checkArray(arr, n));``}``}``  ` `// This code contributed by Rajput-Ji`

## Python3

 `# Python3 to check whether given``# array is breakable or not``  ` `# Function to check``def` `checkArray(arr, n):``      ` `    ``# Check the result by processing``    ``# the first & last element and size``    ``return` `((arr[``0``] ``%` `2``) ``and``            ``(arr[n ``-` `1``] ``%` `2``) ``and` `(n ``%` `2``))``  ` `# Driver code``arr ``=` `[``1``, ``2``, ``3``, ``4``, ``5` `]``n ``=` `len``(arr);``if` `checkArray(arr, n):``    ``print``(``1``)``else``:``    ``print``(``0``)``  ` `# This code is contributed``# by Mohit Kumar`

## C#

 `// C# to check whether given``// array is breakable or not``using` `System;``  ` `class` `GFG``{``      ` `// Function to check``static` `int` `checkArray(``int` `[]arr, ``int` `n)``{``    ``// Check the result by processing``    ``// the first & last element and size``    ``return` `((arr % 2) > 0 &&``            ``(arr[n - 1] % 2) > 0 &&``               ``(n % 2) > 0) ? 1 : 0;``}``  ` `// Driver code``static` `void` `Main()``{``    ``int` `[]arr = { 1, 2, 3, 4, 5 };``    ``int` `n = arr.Length;``    ``Console.WriteLine(checkArray(arr, n));``  ` `}``}``  ` `// This code is contributed by mits`

## PHP

 ``

## Javascript

 ``

Output:

`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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up