- 1

Find the number of even numbers that are in range between the maximum and minimum elements of an given array

Input: First line contains n (1<=n<=100) Than n numbers are inputed. Output: The number of elements between minimum and maximum. Samples: Input Output 8 2 -1 2 2 1 9 2 4 0 https://code.sololearn.com/c9vyQAQ9CIVM/#cpp

18th Oct 2020, 12:06 PM
Azat Malgazhdar
Azat Malgazhdar - avatar
2 Respostas
+ 3
Rather than saving <min> and <max>, find index of <min> and index of <max> instead. You can then iterate the array from index of <min> to index of <max>, counting even numbers as you go. You may be able to locate the index of <min> and <max> by using just one for-loop 👍
18th Oct 2020, 12:14 PM
Ipang
0
#include <iostream> using namespace std; int main(){ int a; cin>>a; int b[a]; for(int i=0; i<a; i++){ cin>>b[i]; } int min=b[0]; int max=b[0]; int index_min=0; int index_max=0; for(int i=0; i<a; i++){ if(b[i]<min){ min=b[i]; } if(b[i]>max){ max=b[i]; } } for(int i=0; i<a; i++){ if(min==b[i]){ index_min=i; break; } } for(int i=0; i<a; i++){ if(max==b[i]){ index_max=i; break; } } if(index_min > index_max)swap(index_min, index_max); int o = 0; for(int i=index_min+1 ; i<=index_max; i++){ if(b[i]%2 == 0){ o++; } } int *s=&o; cout<<*s; return 0; }
22nd Oct 2023, 5:22 PM
Goku Kujo
Goku Kujo - avatar