Arrays - 2 (1)
Arrays - 2 (1)
Arrays - 2 (1)
Arrays - 2
Assignments Solution
Q1. Count the number of elements strictly greater than x.
Solution:
#include <iostream>
int main() {
int x;
cin>>x;
int a[5];
for(int i=0;i<5;i++)cin>>a[i];
int ans=0;
for(int i=0;i<5;i++){
if(a[i]>x) ans++;
cout<<ans<<endl;
return 0;
#include <iostream>
int main() {
int arr[5]={10,3,1,21,3};
max3 = max2;
max2 = max;
max = arr[i];
max3 = max2;
max2 = arr[i];
max3 = arr[i];
"<<max3;
return 0;
Java
C++ &+ DSA
DSA
Assignments Solution
Q3. Check if the given array is sorted or not.
Solution:
#include <iostream>
int main() {
int arr[5]={1,2,2,4,7};
cout<<”NO”<<endl;
return 0;
cout<<”YES”<<endl;
return 0;
Q4. Find the difference between the sum of elements at even indices to the sum of elements at odd
indices.
Solution:
#include <iostream>
int main() {
int a[5]={7,2,32,5,20};
int sume=sumo=0;
for(int i=0;i<5;i++){
if(i%2==0)
sume+=a[i];
else
sumo+=a[i];
cout<<abs(sume-sumo);
return 0;
Q5. Given an array of integers, change the value of all odd indexed elements to its second multiple
#include <iostream>
int main() {
int arr[5]={7,2,32,5,20};
for(int i=0;i<5;i++){
Java
C++ &+ DSA
DSA
Assignments Solution
if(i%2==0) arr[i]+=10;
else arr[i]=2*arr[i];
cout<<arr[i]<<” “;
return 0;
Q6. Find the unique number in a given Array where all the elements are being repeated twice with one
#include <iostream>
int main() {
int arr[5]={2,2,1,1,20};
for(int i=0;i<5;i++){
int count=0;
for(int j=0;j<5;j++){
if(arr[i]==arr[j]) count++;
if(count==0){
cout<<arr[i];
return 0;
return 0;
Java
C++ &+ DSA
DSA
THANK
YOU !