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++
// C++ implementation to find the sum of all the // composite numbers from odd indices of the given array #include<bits/stdc++.h> using namespace std; // Function to check for composite numbers int composite(int n){ int flag = 0; int c = 0; // Check if the factors are greater than 2 for (int j = 1; j <= n; j++){ if (n % j == 0){ c += 1; } } // Check if the number is composite or not if (c >= 3) flag = 1; return flag; } // Function to print the sum of all // composite numbers in the array void odd_indices(int arr[],int n){ int sum = 0; // Iterate for odd indices in the array for (int k = 0; k < n; k += 2){ int check = composite(arr[k]); // Check if the number is composite // then add it to sum if (check == 1) sum += arr[k]; } // return the sum cout << sum << endl; } // Driver code int main(){ int arr[] = {13, 5, 8, 16, 25}; int n = sizeof(arr)/sizeof(arr[0]); odd_indices(arr,n); } // This code is contributed by Surendra_Gangwar |
Java
// Java implementation to find the sum of all the // composite numbers from odd indices of the given array class GFG{ // Function to check for composite numbers static int composite(int n){ int flag = 0; int c = 0; // Check if the factors are greater than 2 for (int j = 1; j <= n; j++){ if (n % j == 0){ c += 1; } } // Check if the number is composite or not if (c >= 3) flag = 1; return flag; } // Function to print the sum of all // composite numbers in the array static void odd_indices(int arr[],int n){ int sum = 0; // Iterate for odd indices in the array for (int k = 0; k < n; k += 2){ int check = composite(arr[k]); // Check if the number is composite // then add it to sum if (check == 1) sum += arr[k]; } // return the sum System.out.print(sum +"\n"); } // Driver code public static void main(String[] args){ int arr[] = {13, 5, 8, 16, 25}; int n = arr.length; odd_indices(arr,n); } } // This code contributed by sapnasingh4991 |
Python3
# Python3 implementation to find the sum of all the # composite numbers from odd indices of the given array # Function to print the sum of all # composite numbers in the array def odd_indices(arr): sum = 0 # Iterate for odd indices in the array for k in range (0, len(arr), 2): check = composite (arr[k]) # Check if the number is composite # then add it to sum sum += arr[k] if check == 1 else 0 # return the sum print (sum) # Function to check for composite numbers def composite(n): flag = 0 c = 0 # Check if the factors are greater than 2 for j in range (1, n + 1): if (n % j == 0): c += 1 # Check if the number is composite or not if (c >= 3): flag = 1 return flag # Driver code if __name__ == "__main__": arr = [13, 5, 8, 16, 25] odd_indices(arr) |
C#
// C# implementation to find the sum // of all the composite numbers from // odd indices of the given array using System; class GFG{ // Function to check for composite numbers static int composite(int n) { int flag = 0; int c = 0; // Check if the factors are greater than 2 for(int j = 1; j <= n; j++) { if (n % j == 0) { c += 1; } } // Check if the number is composite or not if (c >= 3) flag = 1; return flag; } // Function to print the sum of all // composite numbers in the array static void odd_indices(int []arr,int n) { int sum = 0; // Iterate for odd indices in the array for(int k = 0; k < n; k += 2) { int check = composite(arr[k]); // Check if the number is composite // then add it to sum if (check == 1) sum += arr[k]; } // return the sum Console.Write(sum + "\n"); } // Driver code public static void Main(String[] args) { int []arr = { 13, 5, 8, 16, 25 }; int n = arr.Length; odd_indices(arr, n); } } // This code is contributed by Rohit_ranjan |
33
Recommended Posts:
- Bitwise AND of the sum of prime numbers and the sum of composite numbers in an array
- Product of all the Composite Numbers in an array
- XOR of K largest prime and composite numbers from the given array
- XOR of K smallest prime and composite numbers from the given array
- Sum and Product of all Composite numbers which are divisible by k in an array
- Sum and product of k smallest and k largest composite numbers in the array
- Sum of all composite numbers lying in the range [L, R] for Q queries
- Queries for the difference between the count of composite and prime numbers in a given range
- Find the GCD of N Fibonacci Numbers with given Indices
- Sum of elements in an array having composite frequency
- Count and Sum of composite elements in an array
- Magical Indices in an array
- Find all good indices in the given Array
- Count of indices in an array that satisfy the given condition
- Reorder an Array according to given indices with repetition allowed
- Check if the array can be sorted using swaps between given indices only
- Reverse the substrings of the given String according to the given Array of indices
- Find the count of unvisited indices in an infinite array
- Count the pairs in an array such that the difference between them and their indices is equal
- Range sum queries for anticlockwise rotations of Array by K indices
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


