Sum of all the Composite Numbers from Odd indices of the given array
Last Updated :
29 Dec, 2022
Given an array arr[] of size N which contains at least one composite number. The task is to find out the sum of all the composite numbers in the array which are at odd indices (where indexing is 1 based).
Examples:
Input: arr = [13, 5, 8, 16, 25]
Output: 33
Explanation:
The number in the odd indices are 13, 8, 25 since we follow 1 based indexing.
Out of all these 3 numbers, the composite numbers are 8 and 25, which sums to 33.
Input: arr = [9, 7, 14, 10, 34]
Output: 57
Explanation:
The number in the odd indices are 9, 14, 34 since we follow 1 based indexing.
All three numbers are composite numbers, which sums to 57.
Approach:
To solve the problem mentioned above the main idea is to check for the composite numbers that is, if an integer has more than 2 factors then it’s a composite number otherwise it is not. Then we will iterate and check for the composite numbers in odd indices of the array. If it is a composite number then we will add it to the sum variable otherwise skip it until the end of the array reaches.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
int composite( int n){
int flag = 0;
int c = 0;
for ( int j = 1; j <= n; j++){
if (n % j == 0){
c += 1;
}
}
if (c >= 3)
flag = 1;
return flag;
}
void odd_indices( int arr[], int n){
int sum = 0;
for ( int k = 0; k < n; k += 2){
int check = composite(arr[k]);
if (check == 1)
sum += arr[k];
}
cout << sum << endl;
}
int main(){
int arr[] = {13, 5, 8, 16, 25};
int n = sizeof (arr)/ sizeof (arr[0]);
odd_indices(arr,n);
}
|
Java
class GFG{
static int composite( int n){
int flag = 0 ;
int c = 0 ;
for ( int j = 1 ; j <= n; j++){
if (n % j == 0 ){
c += 1 ;
}
}
if (c >= 3 )
flag = 1 ;
return flag;
}
static void odd_indices( int arr[], int n){
int sum = 0 ;
for ( int k = 0 ; k < n; k += 2 ){
int check = composite(arr[k]);
if (check == 1 )
sum += arr[k];
}
System.out.print(sum + "\n" );
}
public static void main(String[] args){
int arr[] = { 13 , 5 , 8 , 16 , 25 };
int n = arr.length;
odd_indices(arr,n);
}
}
|
Python3
def odd_indices(arr):
sum = 0
for k in range ( 0 , len (arr), 2 ):
check = composite (arr[k])
sum + = arr[k] if check = = 1 else 0
print ( sum )
def composite(n):
flag = 0
c = 0
for j in range ( 1 , n + 1 ):
if (n % j = = 0 ):
c + = 1
if (c > = 3 ):
flag = 1
return flag
if __name__ = = "__main__" :
arr = [ 13 , 5 , 8 , 16 , 25 ]
odd_indices(arr)
|
C#
using System;
class GFG{
static int composite( int n)
{
int flag = 0;
int c = 0;
for ( int j = 1; j <= n; j++)
{
if (n % j == 0)
{
c += 1;
}
}
if (c >= 3)
flag = 1;
return flag;
}
static void odd_indices( int []arr, int n)
{
int sum = 0;
for ( int k = 0; k < n; k += 2)
{
int check = composite(arr[k]);
if (check == 1)
sum += arr[k];
}
Console.Write(sum + "\n" );
}
public static void Main(String[] args)
{
int []arr = { 13, 5, 8, 16, 25 };
int n = arr.Length;
odd_indices(arr, n);
}
}
|
Javascript
<script>
function composite(n){
let flag = 0;
let c = 0;
for (let j = 1; j <= n; j++){
if (n % j == 0){
c += 1;
}
}
if (c >= 3)
flag = 1;
return flag;
}
function odd_indices(arr, n){
let sum = 0;
for (let k = 0; k < n; k += 2){
let check = composite(arr[k]);
if (check == 1)
sum += arr[k];
}
document.write(sum + "<br>" );
}
let arr = [13, 5, 8, 16, 25];
let n = arr.length
odd_indices(arr,n);
</script>
|
Time Complexity: O(n * m), where n is the length of the given array and m is the maximum element in the array.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...