Length of longest subsequence having sum of digits of each element as a Composite Number
Last Updated :
06 Jul, 2021
Given an array arr[] consisting of non-negative integers, the task is to print the length of the longest subsequence from the given array whose sum of digits of each element is a composite numbers.
Examples:
Input: arr[] = {13, 55, 7, 3, 5, 21, 233, 144, 89}
Output: 4
Explanation: Following array elements have sum of digits equal to a composite number:
- 13 -> 1 + 3 = 4
- 55 -> 5 + 5 = 10
- 233 -> 2 + 3 + 3 = 8
- 144 -> 1 + 4 + 4 = 9
Therefore, the required the longest subsequence is {13, 55, 233, 144} of length 4.
Input: arr[] = {34, 13, 11, 8, 3, 55, 23}
Output: 3
Explanation: Following array elements have sum of digits equal to a composite number:
- 13 -> 1 + 3 = 4
- 8 -> 8 = 8
- 55 -> 5 + 5 = 10
Therefore, the required the longest subsequence is {13, 8, 55} of length 3.
Approach: Follow the steps given below to solve the problem:
- Traverse the given array.
- For each array element, check if the sum of its digits is prime or the sum of its digits is equal to 1.
- If sum of its digits is prime, then proceed to the next array element. Otherwise, increase the length of the required subsequence by 1.
- Finally, after complete traversal of the array, print the length of The subsequence obtained.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define N 100005
void SieveOfEratosthenes( bool prime[],
int p_size)
{
prime[0] = false ;
prime[1] = false ;
for ( int p = 2; p * p <= p_size; p++) {
if (prime[p]) {
for ( int i = p * 2; i <= p_size; i += p)
prime[i] = false ;
}
}
}
int digitSum( int number)
{
int sum = 0;
while (number > 0) {
sum += (number % 10);
number /= 10;
}
return sum;
}
void longestCompositeDigitSumSubsequence(
int arr[], int n)
{
int count = 0;
bool prime[N + 1];
memset (prime, true , sizeof (prime));
SieveOfEratosthenes(prime, N);
for ( int i = 0; i < n; i++) {
int res = digitSum(arr[i]);
if (res == 1) {
continue ;
}
if (!prime[res]) {
count++;
}
}
cout << count << endl;
}
int main()
{
int arr[] = { 13, 55, 7, 3, 5, 1,
10, 21, 233, 144, 89 };
int n = sizeof (arr)
/ sizeof (arr[0]);
longestCompositeDigitSumSubsequence(
arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int N = 100005 ;
static void SieveOfEratosthenes( boolean []prime,
int p_size)
{
prime[ 0 ] = false ;
prime[ 1 ] = false ;
for ( int p = 2 ; p * p <= p_size; p++)
{
if (prime[p])
{
for ( int i = p * 2 ; i <= p_size; i += p)
prime[i] = false ;
}
}
}
static int digitSum( int number)
{
int sum = 0 ;
while (number > 0 )
{
sum += (number % 10 );
number /= 10 ;
}
return sum;
}
static void longestCompositeDigitSumSubsequence( int []arr,
int n)
{
int count = 0 ;
boolean []prime = new boolean [N + 1 ];
for ( int i = 0 ; i <= N; i++)
prime[i] = true ;
SieveOfEratosthenes(prime, N);
for ( int i = 0 ; i < n; i++)
{
int res = digitSum(arr[i]);
if (res == 1 )
{
continue ;
}
if (prime[res] == false )
{
count++;
}
}
System.out.println(count);
}
public static void main(String[] args)
{
int []arr = { 13 , 55 , 7 , 3 , 5 , 1 ,
10 , 21 , 233 , 144 , 89 };
int n = arr.length;
longestCompositeDigitSumSubsequence(arr, n);
}
}
|
Python3
N = 100005
def SieveOfEratosthenes(prime,
p_size):
prime[ 0 ] = False
prime[ 1 ] = False
p = 2
while p * p < = p_size:
if (prime[p]):
for i in range (p * 2 ,
p_size + 1 , p):
prime[i] = False
p + = 1
def digitSum(number):
sum = 0
while (number > 0 ):
sum + = (number % 10 )
number / / = 10
return sum
def longestCompositeDigitSumSubsequence(arr, n):
count = 0
prime = [ True ] * (N + 1 )
SieveOfEratosthenes(prime, N)
for i in range (n):
res = digitSum(arr[i])
if (res = = 1 ):
continue
if ( not prime[res]):
count + = 1
print (count)
if __name__ = = "__main__" :
arr = [ 13 , 55 , 7 , 3 , 5 , 1 ,
10 , 21 , 233 , 144 , 89 ]
n = len (arr)
longestCompositeDigitSumSubsequence(arr, n)
|
C#
using System.Collections.Generic;
using System;
class GFG{
static int N = 100005;
static void SieveOfEratosthenes( bool []prime,
int p_size)
{
prime[0] = false ;
prime[1] = false ;
for ( int p = 2; p * p <= p_size; p++)
{
if (prime[p])
{
for ( int i = p * 2; i <= p_size; i += p)
prime[i] = false ;
}
}
}
static int digitSum( int number)
{
int sum = 0;
while (number > 0)
{
sum += (number % 10);
number /= 10;
}
return sum;
}
static void longestCompositeDigitSumSubsequence( int []arr,
int n)
{
int count = 0;
bool []prime = new bool [N + 1];
for ( int i = 0; i <= N; i++)
prime[i] = true ;
SieveOfEratosthenes(prime, N);
for ( int i = 0; i < n; i++)
{
int res = digitSum(arr[i]);
if (res == 1)
{
continue ;
}
if (prime[res] == false )
{
count++;
}
}
Console.WriteLine(count);
}
public static void Main()
{
int []arr = { 13, 55, 7, 3, 5, 1,
10, 21, 233, 144, 89 };
int n = arr.Length;
longestCompositeDigitSumSubsequence(arr, n);
}
}
|
Javascript
<script>
let N = 100005;
function SieveOfEratosthenes(prime,p_size)
{
prime[0] = false ;
prime[1] = false ;
for (let p = 2; p * p <= p_size; p++)
{
if (prime[p])
{
for (let i = p * 2; i <= p_size; i += p)
prime[i] = false ;
}
}
}
function digitSum(number)
{
let sum = 0;
while (number > 0)
{
sum += (number % 10);
number =Math.floor(number/ 10);
}
return sum;
}
function longestCompositeDigitSumSubsequence(arr,n)
{
let count = 0;
let prime = new Array(N + 1);
for (let i = 0; i <= N; i++)
prime[i] = true ;
SieveOfEratosthenes(prime, N);
for (let i = 0; i < n; i++)
{
let res = digitSum(arr[i]);
if (res == 1)
{
continue ;
}
if (prime[res] == false )
{
count++;
}
}
document.write(count);
}
let arr=[13, 55, 7, 3, 5, 1,
10, 21, 233, 144, 89 ];
let n = arr.length;
longestCompositeDigitSumSubsequence(arr, n);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(log10(maxm)), where maxm is the maxm array element
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...