Arrays are a fundamental data structure in programming, providing a way to store and manipulate collections of data. Whether you're a beginner or an experienced programmer, solving problems involving arrays is crucial for developing strong coding skills. In this blog post, we'll explore a series of array problems, sorted by increasing difficulty, and provide solutions to each. Let's dive in!
Level 1: Essential Operations
1. Count Elements in an Array: How Many Elements Are There?
Knowing the size of your array is crucial. Learn two methods to efficiently determine the number of elements in an array.
Problem: Determine the number of elements stored in an array.
Method 1:
#include <iostream>
using namespace std;
int main()
{
int arr[] = {1, 2, 3, 4};
cout << "Total Elements: " << size(arr) << endl;
return 0;
}
Method 2
#include <iostream>
using namespace std;
int main()
{
int arr[] = {1, 2, 3, 4};
cout << "Total Elements: " << sizeof(arr)/sizeof(arr[0]) << endl;
return 0;
}
Output:
Total Elements: 4
2. Find the Sum of All Elements in an Array: Calculate the Total
Need to calculate the combined value of all elements in an array? This problem walks you through finding the sum of an array.
Problem: Given an array arr of integers, calculate the total sum of its elements.
#include <iostream>
using namespace std;
int main()
{
int arr[5] = {4, 5, 6, 7, 8};
int sum = 0;
for (int i = 0; i < size(arr); i++)
{
sum += arr[i];
}
cout << "The sum of the elements in the array is : " << sum << endl;
}
Output:
The sum of the elements in the array is : 30
3. Find the Product of All Elements in an Array: Multiply Them All
This problem delves into calculating the product (multiplication) of all elements within an array.
Problem: Given an array arr of integers, calculate the product of all its elements.
#include <iostream>
using namespace std;
int main()
{
int arr[] = {1, 2, 3, 4};
int product = 1;
for (int i = 0; i < size(arr); i++)
{
product *= arr[i];
}
cout << "Product: " << product << endl;
return 0;
}
Output:
Product: 24
4. Find the Greatest and Smallest Elements in an Array: Identify the Extremes
Unearth the largest and smallest values within an array using this step-by-step guide.
Problem: Identify the largest and smallest values within an array.
#include <iostream>
using namespace std;
int main()
{
int arr[5] = {4, 5, 6, 7, 8};
int max, min;
max = arr[0];
min = arr[0];
for (int i = 0; i < size(arr); i++)
{
if (arr[i] > max)
{
max = arr[i];
}
else if (arr[i] < min)
{
min = arr[i];
}
}
cout << "Max value: " << max << endl
<< "Min value: " << min << endl;
return 0;
}
Ouput:
Max value: 8
Min value: 4
Level 2: Intermediate Challenges
1. Print Elements Greater Than X in an Array: Filter and Display
Want to find and display only elements that exceed a specific value (X)? This problem equips you with the solution.
Problem: Print elements greater than X in an array.
#include <iostream>
using namespace std;
int main()
{
int arr[] = {1, 3, 5, 7, 9, 10, 13};
int key = 6;
for (int i = 0; i < size(arr); i++)
{
if (arr[i] > key)
{
cout << arr[i] << " ";
}
}
return 0;
}
Output:
7 9 10 13
2. Find the Difference Between Sum of Elements at Even and Odd Indices: Even vs. Odd
This problem presents a unique challenge: calculating the difference between the sum of elements at even and odd indices within the array.
Problem: Find the difference between the sum of elements at even indices and the sum at odd indices.
#include <iostream>
using namespace std;
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};
int sumOdd, sumEven;
sumOdd = 0;
sumEven = 0;
for (int i = 0; i < size(arr); i++)
{
if (i % 2 == 0)
{
sumEven += arr[i];
}
else
sumOdd += arr[i];
}
cout << "The difference between sum of elements at odd and even indices is " << sumEven - sumOdd << endl;
return 0;
}
Output:
The difference between sum of elements at odd and even indices is -4
3. Reverse the Given Array: Flip the Order
Master the art of reversing an array, effectively rearranging elements from the end to the beginning.
Problem: Reverse the given array.
#include <iostream>
using namespace std;
int main()
{
int a[] = {4, 5, 6, 7, 8};
int b[size(a)];
int iteration = 0;
for (int i = size(a) - 1; i >= 0; i--)
{
b[iteration] = a[i];
iteration++;
}
for (int j = 0; j < size(b); j++)
{
cout << b[j] << " ";
}
return 0;
}
Output:
8 7 6 5 4
4. Modify Array Based on Index Conditions: Conditional Transformations
This problem takes array manipulation a step further, instructing you to modify elements based on their even or odd indices.
Problem: Change the value of odd indexed elements to its second multiple and increment all even indexed values by 10
#include <iostream>
using namespace std;
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};
for (int i = 0; i < size(arr); i++)
{
if (i % 2 == 0)
{
arr[i] += 10;
}
else
arr[i] *= 2;
}
for (int i = 0; i < size(arr); i++)
{
cout << arr[i] << " ";
}
return 0;
}
Output:
11 4 13 8 15 12 17 16
5. Identify Students with Marks Below a Threshold: Find Failing Students
Given an array of student marks, this problem helps you identify and print the roll numbers (indices) of students who scored below a specific threshold.
Problem: Given an array of student marks, print the roll number (index) if the mark is less than 35.
#include <iostream>
using namespace std;
int main()
{
int arr[] = {70, 90, 67, 33, 36, 79, 22, 10};
cout << "Students having marks less than 35" << endl;
for (int i = 0; i < size(arr); i++)
{
if (arr[i] < 35)
{
cout << "Roll no. : " << i << endl;
}
}
return 0;
}
Output:
Students having marks less than 35
Roll no. : 3
Roll no. : 6
Roll no. : 7
6. Find the Number of Pairs with a Given Sum: Two Elements, One Target
This intermediate challenge introduces the concept of finding pairs of elements within an array whose sum equals a specific value.
Problem: Count the pairs of elements in an array whose sum equals a specific value.
#include <iostream>
using namespace std;
int main()
{
int arr[] = {2, 3, 4, 5, 6, 7, 8};
int pairs = 0;
int key = 9;
for (int i = 0; i < size(arr); i++)
{
for (int j = i + 1; j < size(arr); j++)
{
if (arr[i] + arr[j] == key)
{
pairs++;
}
}
}
cout << "Pairs having sum 9 are : " << pairs << endl;
return 0;
}
Output:
Pairs having sum 9 are : 3
Level 3: Advanced Techniques
1. Find the Second Largest Element in an Array: Beyond the Maximum
This problem goes beyond finding the largest element. Learn how to identify the second-highest value within an array.
Problem: Identify the element with the second highest value in an array.
#include <iostream>
using namespace std;
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};
int max = arr[0];
for (int i = 0; i < size(arr); i++)
{
if (arr[i] > max)
{
max = arr[i];
}
}
int smax = 0;
for (int i = 0; i < size(arr); i++)
{
if (arr[i] > smax && arr[i] != max)
{
smax = arr[i];
}
}
cout << "Second Largest No. in the given array : " << smax;
return 0;
}
Output:
Second Largest No. in the given array : 7
2. Find the Second Largest Element in a Single Pass: Optimize for Efficiency
This advanced technique demonstrates how to find the second largest element in a single pass through the array, optimizing for efficiency.
Problem: Efficiently find the second largest element in a single pass through the array.
#include <iostream>
using namespace std;
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int max = arr[0];
int smax = 0;
for (int i = 0; i < size(arr); i++)
{
if (arr[i] > max)
{
smax = max;
max = arr[i];
}
else if (smax < arr[i] && max != arr[i])
{
smax = arr[i];
}
}
cout << "Second Largest No. in the given array : " << smax;
return 0;
}
Output:
Second Largest No. in the given array : 8
3. Find the Number of Triplets with a Given Sum: Three Elements, One Target
This problem builds upon the concept of finding pairs. Here, you'll learn how to count triplets of elements that add up to a specific value within the array.
Problem: Count the triplets of elements in an array that add up to a given value.
#include <iostream>
using namespace std;
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};
int key = 14;
int tripletCout = 0;
for (int i = 0; i < size(arr); i++)
{
for (int j = i + 1; j < size(arr); j++)
{
for (int k = j + 1; k < size(arr); k++)
{
if (arr[i] + arr[j] + arr[k] == key)
{
tripletCout++;
}
}
}
}
cout << "No. of triplets: " << tripletCout << endl;
return 0;
}
Output:
No. of triplets: 6
By conquering these problems, you'll gain mastery over 1D arrays, a valuable skill in programming. Feel free to explore further challenges and delve deeper into more complex array operations!