हमें दोहराए जाने वाले या डुप्लिकेट तत्वों की एक सरणी के साथ दिया गया है और कार्य उन सभी तत्वों के उत्पाद को ढूंढना है जो दिए गए सरणी में गैर-दोहराव या विशिष्ट हैं और परिणाम प्रदर्शित करते हैं।
उदाहरण
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