Longest Increasing Subarray of Composite Numbers in Array
Last Updated :
28 Nov, 2023
Given an array of integers arr[], the task is to find the longest increasing subarray of composite numbers in an array.
Examples:
Input: arr[] = [1, 4, 7, 6, 8, 10, 12]
Output: [6, 8, 10, 12]
Explanation: The longest increasing subarray of composite numbers in the given array is [6, 8, 10, 12], which consists of the composite numbers 6, 8, 10, and 12. The subarray starts at index 3 (6) and ends at index 6 (12).
Input: arr[] = [10, 15, 23, 9, 27, 30, 45, 21, 8, 12]
Output: [9, 27, 30, 45]
Explanation: The longest increasing subarray of composite numbers in the given array is [9, 27, 30, 45, 21], which consists of the composite numbers 9, 27, 21, 30, and 45. The subarray starts at index 3 (9) and ends at index 6 (45).
Approach: To solve the problem follow the below idea:
- The approach to solve this problem is iterating through the array, checking whether each element is composite or not.
- If the current element is composite, the program checks if it’s greater than the previous element (or if it’s the first element).
- If it is, then the length of the current increasing subarray is incremented, otherwise, it is reset to 1.
- If the length of the current increasing subarray is greater than the maximum length seen so far, the maximum length and the starting index of the subarray are updated.
- Finally, the program returns the longest increasing subarray of composite numbers as a vector. If there is no such subarray, it returns an empty vector.
Here are the steps of the approach:
- Initialize maxLen to 0, len to 0, and startIndex to -1 to keep track of the longest increasing subarray of composite numbers found, the length of the current subarray, and the starting index.
- Check If the current element is composite, If it’s the first element or greater than the previous, increment len.
- Otherwise, start a new subarray.
- And update maxLen to len and startIndex to i – len.
- If not composite or we’ve reached the end of the array.
- Reset len to 0 to start counting a new subarray.
- If a subarray of composite numbers was found (if maxLen > 0), return it starting from startIndex and of length maxLen. Otherwise, return an empty array to indicate no increasing subarray of composite numbers was found in the input array.
Below is the implementation of the code:
C++
#include <bits/stdc++.h>
using namespace std;
bool isComposite( int num)
{
if (num < 2) {
return false ;
}
for ( int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
return true ;
}
}
return false ;
}
vector< int > longestIncreasingSubarrayOfCompositeNumbers(
vector< int >& arr)
{
int n = arr.size();
int maxLen = 0, len = 0, startIndex = -1;
for ( int i = 0; i < n; i++) {
if (isComposite(arr[i])) {
if (i == 0 || arr[i] > arr[i - 1]) {
len++;
}
else {
len = 1;
}
if (len > maxLen) {
maxLen = len;
startIndex = i - len + 1;
}
}
else {
len = 0;
}
}
if (maxLen == 0) {
return {};
}
vector< int > result(maxLen);
for ( int i = 0; i < maxLen; i++) {
result[i] = arr[startIndex + i];
}
return result;
}
int main()
{
vector< int > arr
= { 10, 15, 23, 9, 27, 30, 45, 21, 8, 12 };
vector< int > result
= longestIncreasingSubarrayOfCompositeNumbers(arr);
cout << "Longest increasing subarray of composite "
"numbers:" ;
for ( int i = 0; i < result.size(); i++) {
cout << " " << result[i];
}
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.List;
public class Main {
static boolean isComposite( int num) {
if (num < 2 ) {
return false ;
}
for ( int i = 2 ; i <= num / 2 ; i++) {
if (num % i == 0 ) {
return true ;
}
}
return false ;
}
static List<Integer> longestIncreasingSubarrayOfCompositeNumbers(List<Integer> arr) {
int n = arr.size();
int maxLen = 0 , len = 0 , startIndex = - 1 ;
for ( int i = 0 ; i < n; i++) {
if (isComposite(arr.get(i))) {
if (i == 0 || arr.get(i) > arr.get(i - 1 )) {
len++;
}
else {
len = 1 ;
}
if (len > maxLen) {
maxLen = len;
startIndex = i - len + 1 ;
}
} else {
len = 0 ;
}
}
if (maxLen == 0 ) {
return new ArrayList<>();
}
List<Integer> result = new ArrayList<>();
for ( int i = 0 ; i < maxLen; i++) {
result.add(arr.get(startIndex + i));
}
return result;
}
public static void main(String[] args) {
List<Integer> arr = List.of( 10 , 15 , 23 , 9 , 27 , 30 , 45 , 21 , 8 , 12 );
List<Integer> result = longestIncreasingSubarrayOfCompositeNumbers(arr);
System.out.print( "Longest increasing subarray of composite numbers:" );
for ( int i = 0 ; i < result.size(); i++) {
System.out.print( " " + result.get(i));
}
}
}
|
Python3
def isComposite(num):
if num < 2 :
return False
for i in range ( 2 , num / / 2 + 1 ):
if num % i = = 0 :
return True
return False
def longestIncreasingSubarrayOfCompositeNumbers(arr):
n = len (arr)
maxLen = 0
length = 0
startIndex = - 1
for i in range (n):
if isComposite(arr[i]):
if i = = 0 or arr[i] > arr[i - 1 ]:
length + = 1
else :
length = 1
if length > maxLen:
maxLen = length
startIndex = i - length + 1
else :
length = 0
if maxLen = = 0 :
return []
result = [ 0 ] * maxLen
for i in range (maxLen):
result[i] = arr[startIndex + i]
return result
arr = [ 10 , 15 , 23 , 9 , 27 , 30 , 45 , 21 , 8 , 12 ]
result = longestIncreasingSubarrayOfCompositeNumbers(arr)
print ( "Longest increasing subarray of composite numbers:" , end = "")
for i in range ( len (result)):
print ( " " , result[i], end = "")
|
C#
using System;
using System.Collections.Generic;
class Program
{
static bool IsComposite( int num)
{
if (num < 2)
{
return false ;
}
for ( int i = 2; i <= num / 2; i++)
{
if (num % i == 0)
{
return true ;
}
}
return false ;
}
static List< int > LongestIncreasingSubarrayOfCompositeNumbers(List< int > arr)
{
int n = arr.Count;
int maxLen = 0, len = 0, startIndex = -1;
for ( int i = 0; i < n; i++)
{
if (IsComposite(arr[i]))
{
if (i == 0 || arr[i] > arr[i - 1])
{
len++;
}
else
{
len = 1;
}
if (len > maxLen)
{
maxLen = len;
startIndex = i - len + 1;
}
}
else
{
len = 0;
}
}
if (maxLen == 0)
{
return new List< int >();
}
List< int > result = new List< int >(maxLen);
for ( int i = 0; i < maxLen; i++)
{
result.Add(arr[startIndex + i]);
}
return result;
}
static void Main( string [] args)
{
List< int > arr = new List< int >() { 10, 15, 23, 9, 27, 30, 45, 21, 8, 12 };
List< int > result = LongestIncreasingSubarrayOfCompositeNumbers(arr);
Console.Write( "Longest increasing subarray of composite numbers:" );
foreach ( var num in result)
{
Console.Write( " " + num);
}
}
}
|
Javascript
function GFG(num) {
if (num < 2) {
return false ;
}
for (let i = 2; i <= Math.floor(num / 2); i++) {
if (num % i === 0) {
return true ;
}
}
return false ;
}
function CompositeNumbers(arr) {
const n = arr.length;
let maxLen = 0;
let len = 0;
let startIndex = -1;
for (let i = 0; i < n; i++) {
if (GFG(arr[i])) {
if (i === 0 || arr[i] > arr[i - 1]) {
len++;
} else {
len = 1;
}
if (len > maxLen) {
maxLen = len;
startIndex = i - len + 1;
}
} else {
len = 0;
}
}
if (maxLen === 0) {
return [];
}
const result = [];
for (let i = 0; i < maxLen; i++) {
result.push(arr[startIndex + i]);
}
return result;
}
const arr = [10, 15, 23, 9, 27, 30, 45, 21, 8, 12];
const result = CompositeNumbers(arr);
console.log( "Longest increasing subarray of composite numbers:" , result.join( " " ));
|
Output
Longest increasing subarray of composite numbers: 9 27 30 45
Time Complexity: O(n * sqrt(max_num)), where n is the number of elements in the input array and max_num is the maximum element in the array, due to iterating through the array and checking if each number is composite.
Axillary space: O(m), where m is the length of the longest increasing subarray of composite numbers. This is because the result vector created at the end will have at most m elements.
Share your thoughts in the comments
Please Login to comment...