Write a C++ program to find the largest element of a given array of
integers.
#include <iostream>
using namespace std;
int main() {
int arr[] = {1, 2, 5, 3, 4};
int n = 5, max = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max)
max
= arr[i];
}
cout << max;
return 0;
}
Write a C++ program to find the largest three elements in an array.
#include <iostream>
using namespace std;
int main() {
int arr[] = {1, 5, 3, 9, 7};
int n = 5;
int first = -1, second = -1, third = -1;
for (int i = 0; i < n; i++) {
if (arr[i] > first) {
third = second;
second = first;
first = arr[i];
} else if (arr[i] > second) {
third = second;
second = arr[i];
} else if (arr[i] > third) {
third = arr[i];
}
}
cout << first << " " << second
<< " " << third;
return 0;
}
Write a C++ program to find the second largest element in an array of
integers.
#include <iostream>
using namespace std;
int main() {
int arr[] =
{5,3,8,7,6,2,2,2,7,7,7,7,8};
int n = 5, first = -1, second = -1;
for (int i = 0; i < n; i++) {
if (arr[i] > first)
{
second =
first;
first =
arr[i];
} else if (arr[i] >
second && arr[i] != first) {
second =
arr[i];
}
}
cout << second;
return 0;
}
Write a C++ program to find the k largest elements in a given array of
integers.
#include <iostream>
using namespace std;
int main() {
int arr[] = {10, 2, 10, 1, 2};
int n = 5, k = 3;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (arr[i] < arr[j]) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
for (int i = 0; i < k; i++) cout << arr[i] <<
" ";
return 0;
}
Write a C++ program to find the second smallest elements in a given array
of integers.
#include <iostream>
using namespace std;
int main() {
int arr[] = {5, 2, 2, 3, 2};
int n = 5;
int first = arr[0], second = arr[0];
for (int i = 0; i < n; i++) {
if (arr[i] < first) {
second = first;
first = arr[i];
} else if (arr[i] < second && arr[i] !=
first) {
second = arr[i];
}
}
if (second == first)
cout << "No second smallest element
exists.";
else
cout << second;
return 0;
}
Write a C++ program to find all elements in an array of integers that are greater then or equal 2.
#include <iostream>
using namespace std;
int main() {
int arr[] = {3, 1, 4, 1, 0};
int n = 5, count = 0;
for (int i = 0; i < n; i++) {
if (arr[i] >= 2)
cout << arr[i] <<
" ";
}
return 0;
}
Write a C++ program to find the most frequent element in an array of
integers.
#include <iostream>
using namespace std;
int main() {
int arr[] = {1, 2, 2, 8, 5,5,5,5};
int n = sizeof(arr)/4;
int maxCount = 0, res = -1;
for (int i = 0; i < n; i++) {
int count = 0;
for (int j = 0; j < n; j++) {
if (arr[i] == arr[j]) count++;
}
if (count > maxCount) {
maxCount = count;
res = arr[i];
}
}
cout << res;
return 0;
}
Write a C++ program to find the next more powerful element of every element
of a given array of integers. Ignore those elements that have no greater
element.
#include <iostream>
using namespace std;
int main() {
int
arr[] = {4,5,2,10,1,0,5,14,7,2};
int n =
sizeof(arr)/4;
for (int
i = 0; i < n; i++) {
int
next = -1;
for
(int j = i + 1; j < n; j++) {
if (arr[j] > arr[i]) {
next = arr[j];
break;
}
}
cout
<< arr[i] << " -> " << next << endl;
}
return
0;
}
Write a C++ program to separate even and odd numbers in an array of
integers. Put all even numbers first, and then odd numbers.
#include <iostream>
using namespace std;
int main() {
int arr[] = {1, 2, 3, 4, 5, 6};
int n = sizeof(arr) / sizeof(arr[0]);
int even[n], odd[n], e = 0, o = 0;
for (int i = 0; i < n; i++) {
if (arr[i] % 2 == 0) {
even[e++] = arr[i];
} else {
odd[o++] = arr[i];
}
}
for (int i = 0; i < e; i++) {
cout << even[i] << " ";
}
for (int i = 0; i < o; i++) {
cout << odd[i] << " ";
}
return 0;
}
_______________________________________________________
#include <iostream>
using namespace std;
int main() {
int arr[] = {1, 2, 3, 4, 5, 6};
int n = sizeof(arr)/4;
int even[n], odd[n], e = 0, o = 0;
for (int i = 0; i < n; i++)
{
if (arr[i] % 2 == 0) even[e++] = arr[i];
else odd[o++] = arr[i];
}
for (int i = 0; i < e; i++) cout << even[i] <<
" ";
for (int i = 0; i < o; i++) cout << odd[i] <<
" ";
return 0;
}
Write a C++ program to separate 0s and 1s from a given array of values
0 and 1.
#include <iostream>
using namespace std;
int main() {
int
arr[] = {0, 1, 0, 1, 0, 1,1,1,0,1,0,1,0,1};
int n =sizeof(arr)/4;
int
count = 0;
for (int
i = 0; i < n; i++) {
if
(arr[i] == 0) count++;
}
for (int
i = 0; i < count; i++)
cout
<< "0 ";
for (int
i = count; i < n; i++)
cout
<< "1 ";
return
0;
}
Comments
Post a Comment