ধনাত্মক পূর্ণসংখ্যার একটি অ্যারে arr[ ] দেওয়া হয়েছে। লক্ষ্য হল arr[ ] এর সাবঅ্যারেগুলির গণনা খুঁজে বের করা যেখানে এর উপাদানগুলির গড় রয়েছে arr[ ] এর বাকি উপাদানগুলির গড় থেকে বেশি যা এতে উপস্থিত নেই৷
উদাহরণস্বরূপ
ইনপুট
arr[ ] = { 3, 2, 4 } আউটপুট
Count of number of sub-arrays such that the average of elements present in the sub−array is greater than that do not present in the sub−array are: 2
ব্যাখ্যা
The subarrays are − [ 3 ], [ 2 ], [ 4 ], [ 3,2 ], [ 2,4 ], [ 3,2,4 ]. Average of [ 4 ] is 4 which is more than the average of [ 2,3 ]. Average of [ 3,2,4 ] is 3 which is more than the average of [ ]এর গড় থেকে বেশি
ইনপুট
arr[ ] = { 3, 3, 3 } আউটপুট
Count of number of sub−arrays such that the average of elements present in the sub−array is greater than that do not present in the sub−array are: 1
ব্যাখ্যা
The subarrays are − [ 3 ], [ 3 ], [ 3 ], [ 3,3 ], [ 3,3 ], [ 3,3,3 ]. Average of [ 3,3,3 ] is 3 which is more than the average of [ ]এর গড় থেকে বেশি
নিম্নলিখিত প্রোগ্রামে ব্যবহৃত পদ্ধতি −
এই পদ্ধতিতে একটি উপসর্গ যোগ অ্যারে তৈরি করুন যা new_arr[i] তে সূচক i পর্যন্ত উপাদানের যোগফল সংরক্ষণ করবে। এখন আমাদের পূর্ববর্তী উপাদান পর্যন্ত যোগফল আছে তারপর arr[i] পর্যন্ত যোগফল গণনা করুন এবং j−i+1 হিসাবে উপাদান গণনা করুন এবং গড় গণনা করুন।
-
ইনপুট হিসাবে একটি অ্যারে arr[] নিন।
-
ফাংশন গণনা (int arr[], int size) arr[ ] নেয় এবং সাব-অ্যারেগুলির গণনা প্রদান করে যাতে সাব-অ্যারেতে উপস্থিত উপাদানগুলির গড় সাব-অ্যারেতে উপস্থিত না থাকা থেকে বেশি।
-
পূর্ববর্তী সূচক উপাদানগুলির যোগফল সঞ্চয় করতে একটি অ্যারে new_arr[size] নিন৷
৷ -
i=0 থেকে i
-
এখন লুপের জন্য দুই ব্যবহার করে new_arr[ ] অতিক্রম করুন।
-
এখন আগের সাব্যারেগুলির যোগফল হিসাবে total_1 গণনা করুন। এবং উপাদানগুলি গণনা_1 হিসাবে।
-
গণনা_2 হিসাবে পরবর্তী সাববারে এবং উপাদানগুলির যোগফল হিসাবে total_2 গণনা করুন।
-
চেক_1 =মোট_1 / গণনা_1 হিসাবে গড় গণনা করুন; এবং check_2 =total_2 /count_2;
-
গড় হলে check_1> check_2 তারপর বৃদ্ধির সংখ্যা।
-
loops এর শেষে ফলাফল হিসাবে গণনা ফেরত দেয়।
উদাহরণ
#include <bits/stdc++.h>
using namespace std;
int count(int arr[], int size){
int count = 0;
int new_size = size + 1;
int new_arr[new_size] = { 0 };
for (int i = 1; i < new_size; i++){
new_arr[i] = new_arr[i − 1] + arr[i − 1];
}
for (int i = 1; i < new_size; i++){
for (int j = i; j < new_size; j++){
int total_1 = new_arr[j] − new_arr[i − 1];
int count_1 = j − i + 1;
int total_2 = new_arr[size] − total_1;
int count_2 = 0;
if((size − count_1) == 0){
count_2 = 1;
} else {
count_2 = size − count_1;
}
int check_1 = total_1 / count_1;
int check_2 = total_2 / count_2;
if (check_1 > check_2){
count++;
}
}
}
return count;
}
int main(){
int arr[] = { 2, 6, 2, 4 };
int size = sizeof(arr) / sizeof(arr[0]);
cout<<"Count of number of sub−arrays such that the average of elements present in
the sub−array "<< "is greater than that not present in the sub−array are: "<<count(arr, size);
return 0;
} আউটপুট
যদি আমরা উপরের কোডটি চালাই তবে এটি নিম্নলিখিত আউটপুট −
উৎপন্ন করবেCount the number of sub−arrays such that the average of elements present in the subarrayis greater than that not present in the sub-array are: 6