Given an array arr[] and two integers say, x and y, find the number of sub-arrays in…
- User
Given an array arr[] and two integers say, x and y, find the number of sub-arrays in which the number of occurrences of x is equal to the number of occurrences of y.
Examples:
Input: arr[] = [1, 2, 1], x=1,y=2 Output: 2 Explanation: The possible sub-arrays have same equal number of occurrences of x and y are:
- [1, 2], x and y have same occurrence(1).
- [2, 1], x and y have same occurrence(1).
Input: arr[] = [1, 2, 1], x=4, y=6 Output: 6 Explanation: The possible sub-arrays have same equal number of occurrences of x and y are:
- [1], x and y have same occurrence(O).
- [2], x and y have same occurrence(O).
- [1], x and y have same occurrence(O).
- [1, 2], x and y have same occurrence(O).
- [2, 1], x and y have same occurrence(O).
- [1, 2, 1], x and y have same occurrence(O).
Input: arr[] = [1, 2, 1],x=1,y=4 Output: 1 Explanation: The possible sub-array have same equal number of occurrences of x and y is: [2], x and y have same occurrence(1)
Constraints: 1 <= arr.size() <= 106 1 <= arr[i], x, y <= 106
class Solution: def sameOccurrence(self, arr, x, y): # code here
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.