কম্পিউটার

C++-এ একটি অ্যারেতে অ-পুনরাবৃত্ত (স্বতন্ত্র) উপাদানগুলির পণ্য


আমাদেরকে পুনরাবৃত্তি করা বা সদৃশ উপাদানগুলির একটি অ্যারে দেওয়া হয়েছে এবং কাজটি হল সেই সমস্ত উপাদানগুলির গুণফল খুঁজে বের করা যা প্রদত্ত অ্যারেতে পুনরাবৃত্তি হয় না বা স্বতন্ত্র।

উদাহরণ

Input-: arr[] = {2, 1, 1, 2, 3, 4, 5, 5 }
Output-: 120
Explanation-: Since 1, 2 and 5 are repeating more than once so we will take them into 
consideration for their first occurrence. So result will be 1 * 2 * 3 * 4 * 5 = 120
Input-: arr[] = {1, 10, 9, 4, 2, 10, 10, 45, 4 }
Output-: 32400
Explanation-: Since 10 and 4 are repeating more than once so we will take them into consideration 
for their first occurrence. So result will be 1 * 10 * 9 * 4 * 2 * 45  = 32400

নিম্নলিখিত প্রোগ্রামে ব্যবহৃত পদ্ধতি

  • একটি অ্যারেতে ডুপ্লিকেট উপাদানগুলি ইনপুট করুন
  • ভাল পদ্ধতির জন্য একটি অ্যারের উপাদানগুলিকে আরোহী ক্রমে সাজান যাতে কোন অ্যারের উপাদানটি পুনরাবৃত্তি হচ্ছে তা নির্ধারণ করা সহজ হবে এবং পণ্যের জন্য এটি বিবেচনা করবেন না
  • একটি অ্যারেতে সমস্ত স্বতন্ত্র উপাদান খুঁজুন এবং ফলাফল সংরক্ষণ করে তাদের গুণ করতে থাকুন
  • একটি অ্যারের সমস্ত স্বতন্ত্র উপাদানের গুণফল হিসাবে চূড়ান্ত ফলাফল প্রদর্শন করুন

অ্যালগরিদম

Start
Step 1-> Declare function to find the product of all the distinct elements in an array
   int find_Product(int arr[], int size)
   Declare and set int prod = 1
   Create variable as unordered_set<int> s
   Loop For  i = 0 and i < size and i++
      IF s.find(arr[i]) = s.end()
         Set  prod *= arr[i]
         Call s.insert(arr[i])
      End
   End
   return prod
Step 2 -: In main()
   Declare and set int arr[] = { 2, 1, 1, 2, 3, 4, 5, 5 }
   Calculate the size of an array int size = sizeof(arr) / sizeof(int)
   Call find_Product(arr, size)
Stop

উদাহরণ

include <bits/stdc++.h>
using namespace std;
//function that calculate the product of non-repeating elements
int find_Product(int arr[], int size) {
   int prod = 1;
   unordered_set<int> s;
   for (int i = 0; i < size; i++) {
      if (s.find(arr[i]) == s.end()) {
         prod *= arr[i];
         s.insert(arr[i]);
      }
   }
   return prod;
}
int main() {
   int arr[] = { 2, 1, 1, 2, 3, 4, 5, 5 };
   int size = sizeof(arr) / sizeof(int);
   cout<<"product of all non-repeated elements are : "<<find_Product(arr, size);
   return 0;
}

আউটপুট

product of all non-repeated elements are : 120

  1. C++ এ একটি অ্যারেতে সমস্ত মৌলিক সংখ্যার গুণফল

  2. অ্যারের উপাদানগুলির গুণনের জন্য C++ প্রোগ্রাম

  3. C++ এ একটি পণ্য অ্যারে ধাঁধা?

  4. STL ব্যবহার করে C++ এ অ্যারে পণ্য