You are given an array arr of length n. Find and print the element at the mid index array ignoring all the indices at which negative numbers are present in the array.
Note : In case there are two mid indices, print the element at the smaller index. You may assume that there will be at least one positive integer in array.
You are given an array of integers. The task is to find and print the element located at the middle index of the array, but you need to ignore any elements at indices where the value is negative(-).
3 Basic Steps to Understand the Problem on mid index array ignoring all the indices at which negative numbers are present in the array.
- Array Filtering: The array might contain negative(-) numbers, but they should be ignored when determining the “middle” element of the array.
- Middle Element: The “middle” element is defined as the element at the index
len(filtered_array) // 2
of a new array containing only the whole number elements of the original array. - Output: Once the middle element is determined from the filtered array, it will be printed or returned.
ACCENTURE INTERVIEW CODING QUESTION 2024 | 16th AUG
We have coded this Ques for you Javascript, Java, Python, & C++:
In Python
def find_mid_element(arr):
# Step 1: Filter out non-negative elements from the original array
filtered_array = [num for num in arr if num >= 0]
# Step 2: Calculate the middle index
mid_index = len(filtered_array) // 2
# Step 3: Retrieve and print the middle element
print(filtered_array[mid_index])
# Define the array
arr = [2, -3, 5, -1, 7, 8, -2]
# Call the function to find and print the middle element
find_mid_element(arr) // Output: 7
Array Definition: The array arr
is defined directly.
Filtering: List comprehension is used to filter out non-negative(whole numbers) elements.
Middle Index Calculation: The middle index is calculated using integer division (//
).
Output: The element at the middle index is printed using print()
.
In Java
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
// Define the array
int[] arr = {2, -3, 5, -1, 7, 8, -2};
// Call the method to find and print the middle element
findMidElement(arr);
}
public static void findMidElement(int[] arr) {
// Step 1: Initialize an empty list to store non-negative elements
List<Integer> filteredArray = new ArrayList<>();
// Step 2: Filter out non-negative elements from the original array
for (int num : arr) {
if (num >= 0) {
filteredArray.add(num);
}
}
// Step 3: Calculate the middle index
int midIndex = filteredArray.size() / 2;
// Step 4: Retrieve and print the middle element
System.out.println(filteredArray.get(midIndex));
}
} // Output: 7
Array Definition: The array arr
is defined inside the main
method().
Filtering: Non-negative elements are stored in an ArrayList
.
Middle Index Calculation: The middle index is calculated using integer division.
Result: The element at the middle index is printed using System.out.println()
ACCENTURE INTERVIEW CODING QUESTION 2024 | 20th AUG
In Javascript
function findMidElement(arr) {
// Filter out negative numbers
const filteredArr = arr.filter(num => num >= 0);
// Find the middle index
const midIndex = Math.floor(filteredArr.length / 2);
// Print the element at the middle index
console.log(filteredArr[midIndex]);
}
// Example usage:
const arr = [2, -3, 5, -1, 7, 8, -2];
findMidElement(arr); // Output: 7
Array Definition: The array arr
is directly defined in the script with a mix of positive and negative integers.
Filtering: The filter()
method creates a new array filteredArr
containing only non-negative numbers.
Middle Index Calculation: The middle index of filteredArr
is computed using integer division with Math.floor()
to ensure it’s an integer.
Result: The element at the middle index of filteredArr
is printed to the console using console.log()
.
In C++
#include <iostream>
#include <vector>
void findMidElement(std::vector<int> arr) {
// Step 1: Initialize an empty vector to store non-negative elements
std::vector<int> filteredArray;
// Step 2: Filter out non-negative elements from the original array
for (int num : arr) {
if (num >= 0) {
filteredArray.push_back(num);
}
}
// Step 3: Calculate the middle index
int midIndex = filteredArray.size() / 2;
// Step 4: Retrieve and print the middle element
std::cout << filteredArray[midIndex] << std::endl;
}
int main() {
// Define the array
std::vector<int> arr = {2, -3, 5, -1, 7, 8, -2};
// Call the function to find and print the middle element
findMidElement(arr);
return 0;
} // Output: 7
Array Definition: The array arr
is defined as a std::vector<int>
.
Filtering: Non-negative elements(whole number elements) are added to a std::vector<int>
.
Middle Index Calculation: The middle index is calculated using integer division.
Output: The element at the middle index is printed using std::cout
.