Arrays 1 Solutions.
Arrays 1 Solutions.
Solution :
#include <iostream>
using namespace std;
int main() {
int arr[5]={1,2,3,4,5};
int pdt = 1;
for(int i=0;i<5;i++){
pdt = pdt*arr[i];
}
cout<<pdt;
return 0;
}
Find the second largest element in the given Array in one pass.
Solution :
#include <iostream>
using namespace std;
int main() {
int arr[6]={12 ,35, 1, 10, 29, 1};
int max1=max2=INT_MIN;
for(int i=0;i<5;i++){
if(max1<a[i]){
max2=max1;
max1=a[i];
}
else if(max2<a[i] && a[i]!=max1){
max2=a[i];
}
}
if(max2==INT_MIN){
cout<<”No second element exists”<<endl;
}
else cout<<max2<<endl;
return 0;
}
Solution :
#include <iostream>
using namespace std;
int main(){
int arr[5] = {1,2,3,4,5};
int min = INT_MAX;
for(int i=0;i<5;i++){
min = min(min,arr[i]);
}
cout << min;
return 0;
}
Solution :
#include<iostream>
using namespace std;
int main() {
int arr[5]={1,2,2,4,5};
bool flag = false;
for(int i=0;i<5;i++){
for(int j=i+1;j<5;j++){
if(arr[i]==arr[j]){
flag = true;
cout<<arr[i];
break;
}
}
}
if(flag==false) cout << ”No duplicate”;
return 0;
}
WAP to find the smallest missing positive element in the sorted Array that contains only positive
elements.
Solution :
#include<iostream>
using namespace std;
int main() {
cout<<”enter 5 elements of the array”<<endl;
int a[5];
for(int i=0;i<5;i++){
cin>>a[i];
}
int x=0;
bool flag=false;
for(int i=0;i<n;i++){
if(a[i]!= x){
cout<<x<<endl;
flag=true;
break;
}
else x++;
}
if(flag==false) cout<<x<<endl;
return 0;
}
Solution :
49