Wednesday, July 3, 2024
HomeData ModellingData Structure & AlgorithmLexicographically smallest character from an array that satisfies the given conditions

Lexicographically smallest character from an array that satisfies the given conditions

Given a character array, str[] consisting of N lowercase alphabets, and an integer array, arr[] consisting of numbers in the range [0, N ā€“ 1]. Following are the operations that will be performed in the problem:

  1. Traverse the character array str[] from left to right.
  2. For every ith index, store the smallest odd length(>1) intervals (if exists), say [L, R] such that str[L] = str[R].

The task is to find the lexicographically smallest character from str[] that satisfies exactly one of the following conditions:Ā 

  • No odd length intervals exist for the character where str[L] == str[R].
  • All intervals of the character must contain different indexed array element.

Examples:

Input: str[] = { ā€˜aā€™, ā€˜bā€™, ā€˜cā€™, ā€˜aā€™, ā€˜eā€™, ā€˜aā€™, ā€˜aā€™ }, arr[] = { 4, 6 }Ā 
Output: aĀ 
Explanation:Ā 
Possible smallest odd-length interval of the character ā€˜aā€™ in str are { [0, 6], [3, 5] }Ā 
Since the interval [0, 6] contains arr[1] and the interval [3, 5] contains arr[0]. Therefore, the required output is ā€˜aā€™.

Input: str[] = { ā€˜aā€™, ā€˜bā€™, ā€˜cā€™, ā€˜dā€™ }, arr[] = { 3, 4 }Ā 
Output: aĀ 
Explanation:Ā 
Since no interval exist for any element of the character array. Therefore, the lexicographically smallest character of str[] is ā€˜aā€™.

Input: str[] = { ā€˜zā€™, ā€˜zā€™, ā€˜zā€™, ā€˜zā€™ }, arr[] = { 2 }Ā 
Output: -1Ā 
Explanation:Ā 
Possible smallest odd-length intervals of the character ā€˜zā€™ are { [0, 2], [1, 3] }Ā 
Since the interval [0, 2] contain arr[0] and the interval [1, 3] also contain arr[0] which is not distinct indexed array element. Therefore, the required output is -1.

Approach: The idea is to find the smallest odd-length intervals for every possible indices of str[]. Traverse each distinct character of str[] and check if the character satisfies the above conditions or not. If more than one characters satisfy the above conditions then print the lexicographically smallest character from them. Follow the steps below to solve the problem:

  1. Initialize an array, say hash[], to check if a character present in the character array, str[] or not.
  2. Initialize an ArrayList, say intervals[], of the form { X, Y } to store the start and end point of the smallest odd-length intervals.
  3. Traverse the array hash[] and store all the smallest odd-length intervals of each distinct character of str[].
  4. Sort intervals[] on the increasing order of X.
  5. Sort the array arr[] in increasing order.
  6. Check all the intervals of a character satisfy the above conditions or not by performing the following operations:Ā 
    • Initialize a PriorityQueue say, PQ to store the intervals on increasing order of Y of the intervals.
    • Traverse the arr[] array from left to right using variable i. For every ith index of arr[], traverse the intervals[] using variable j and check if start point of intervals[j] is less than arr[i] or not. If found to be true then insert the interval into PQ.
    • If arr[i] is present in a range of the interval, then remove the top element of PQ to ensure that the distinct indexed array elements are assigned to different intervals.
    • If at any point of time arr[i] is less than the end point of intervals[i] or the end point of top element of PQ is less than arr[i] then print -1.
    • Otherwise, print the lexicographically smallest character from the array arr[] that satisfies the conditions.

Below is the implementation of the above approach:

C++




#include <bits/stdc++.h>
using namespace std;
// Structure of an interval
class SpecialInterval {
public:
Ā Ā Ā Ā int low, high;
Ā Ā Ā Ā SpecialInterval(int low, int high)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā // Stores start point of
Ā Ā Ā Ā Ā Ā Ā Ā // an interval
Ā Ā Ā Ā Ā Ā Ā Ā this->low = low;
Ā Ā Ā Ā Ā Ā Ā Ā // Stores end point of
Ā Ā Ā Ā Ā Ā Ā Ā // an interval
Ā Ā Ā Ā Ā Ā Ā Ā this->high = high;
Ā Ā Ā Ā }
};
// Function to check if a character
// present in arr[] or not
bool checkChar(string str)
{
Ā Ā Ā Ā bool hash[26] = { false };
Ā Ā Ā Ā for (int i = 0; i < str.length(); i++) {
Ā Ā Ā Ā Ā Ā Ā Ā hash[str[i] - 'a'] = true;
Ā Ā Ā Ā }
Ā Ā Ā Ā return *hash;
}
// Function to check if all the intervals of a
// character satisfies the condition or not
bool checkIfValid(vector<SpecialInterval> intervals,
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā vector<int> arr)
{
Ā Ā Ā Ā // If no intervals found for
Ā Ā Ā Ā // the current character
Ā Ā Ā Ā if (intervals.size() == 0) {
Ā Ā Ā Ā Ā Ā Ā Ā return true;
Ā Ā Ā Ā }
Ā Ā Ā Ā // Sort intervals on increasing
Ā Ā Ā Ā // order of start point
Ā Ā Ā Ā sort(intervals.begin(), intervals.end(),
Ā Ā Ā Ā Ā Ā Ā Ā Ā [](SpecialInterval a, SpecialInterval b) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return a.low < b.low;
Ā Ā Ā Ā Ā Ā Ā Ā Ā });
Ā Ā Ā Ā // Store the intervals on increasing
Ā Ā Ā Ā // order of end point of interval
Ā Ā Ā Ā priority_queue<int, vector<int>, greater<int> > pq;
Ā 
Ā Ā Ā Ā // Stores count of array elements that
Ā Ā Ā Ā // is present in different intervals
Ā Ā Ā Ā int count = 0, j = 0;
Ā Ā Ā Ā for (int i = 0;
Ā Ā Ā Ā Ā Ā Ā Ā Ā i < arr.size() && count < intervals.size(); i++) {
Ā Ā Ā Ā Ā Ā Ā Ā while (j < intervals.size()) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā SpecialInterval interval = intervals[j];
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (interval.low > arr[i])
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā j++;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā pq.push(interval.high);
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā if (pq.size() > 0) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int high = pq.top();
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā pq.pop();
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (arr[i] > high) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā count++;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
Ā Ā Ā Ā return count == intervals.size();
}
// Function to find the intervals
// for each distinct character of str[]
vector<SpecialInterval> findSpecialIntevals(string str,
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā char ch)
{
Ā Ā Ā Ā // Stores the odd index
Ā Ā Ā Ā // of the character array
Ā Ā Ā Ā int oddPrev = -1;
Ā Ā Ā Ā // Stores even index of
Ā Ā Ā Ā // the character array
Ā Ā Ā Ā int evenPrev = -1;
Ā Ā Ā Ā vector<SpecialInterval> intervals;
Ā Ā Ā Ā for (int i = 0; i < str.length(); i++) {
Ā Ā Ā Ā Ā Ā Ā Ā if (str[i] == ch) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (i % 2 == 0) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (evenPrev == -1) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā evenPrev = i;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā SpecialInterval interval(evenPrev, i);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā intervals.push_back(interval);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā evenPrev = -1;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (oddPrev == -1) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā oddPrev = i;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā SpecialInterval interval(oddPrev, i);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā intervals.push_back(interval);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā oddPrev = -1;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
Ā Ā Ā Ā return intervals;
}
// Function to print lexicographically smallest
// character that satisfies the condition
void printAnswer(vector<char> str, vector<int> arr)
{
Ā Ā Ā Ā sort(arr.begin(), arr.end());
Ā Ā Ā Ā bool possible = false;
Ā Ā Ā Ā // Traverse all possible distinct
Ā Ā Ā Ā // character of str
Ā Ā Ā Ā for (int ch = 0; ch < 26; ch++) {
Ā Ā Ā Ā Ā Ā Ā Ā if (!checkChar(string(str.begin(), str.end())))
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā continue;
Ā Ā Ā Ā Ā Ā Ā Ā vector<SpecialInterval> intervals
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = findSpecialIntevals(
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā string(str.begin(), str.end()), ch + 'a');
Ā Ā Ā Ā Ā Ā Ā Ā possible = checkIfValid(intervals, arr);
Ā Ā Ā Ā Ā Ā Ā Ā if (possible) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā cout << char(ch + 'a') << endl;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
Ā Ā Ā Ā if (!possible)
Ā Ā Ā Ā Ā Ā Ā Ā cout << -1 << endl;
}
// Driver Code
int main()
{
Ā Ā Ā Ā vector<char> str
Ā Ā Ā Ā Ā Ā Ā Ā = { 'a', 'b', 'c', 'a', 'e', 'a', 'a' };
Ā Ā Ā Ā vector<int> arr = { 4, 6 };
Ā Ā Ā Ā printAnswer(str, arr);
Ā Ā Ā Ā return 0;
}


Java




// Java Program to implement
// the above approach
Ā 
import java.io.*;
import java.util.*;
Ā 
class GFG {
Ā 
Ā Ā Ā Ā // Structure of an interval
Ā Ā Ā Ā static class SpecialInterval {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Stores start point of
Ā Ā Ā Ā Ā Ā Ā Ā // an interval
Ā Ā Ā Ā Ā Ā Ā Ā int low = 0;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Stores end point of
Ā Ā Ā Ā Ā Ā Ā Ā // an interval
Ā Ā Ā Ā Ā Ā Ā Ā int high = 0;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Initialize a interval
Ā Ā Ā Ā Ā Ā Ā Ā SpecialInterval(int low, int high)
Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā this.low = low;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā this.high = high;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Function to check if a character
Ā Ā Ā Ā // present in arr[] or not
Ā Ā Ā Ā static boolean[] checkChar(char[] str)
Ā Ā Ā Ā {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // hash[i]: Check if character i
Ā Ā Ā Ā Ā Ā Ā Ā // is present in arr[] or not
Ā Ā Ā Ā Ā Ā Ā Ā boolean[] hash = new boolean[26];
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Traverse the character array
Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i < str.length; i++) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Mark arr[i] as true
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā hash[str[i] - 'a'] = true;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā return hash;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Function to check if all the intervals of a
Ā Ā Ā Ā // character satisfies the condition or not
Ā Ā Ā Ā private static boolean
Ā Ā Ā Ā checkIfValid(List<SpecialInterval> intervals, int[] arr)
Ā Ā Ā Ā {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // If no intervals found for
Ā Ā Ā Ā Ā Ā Ā Ā // the current character
Ā Ā Ā Ā Ā Ā Ā Ā if (intervals.size() == 0) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return true;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Sort intervals on increasing
Ā Ā Ā Ā Ā Ā Ā Ā // order of start point
Ā Ā Ā Ā Ā Ā Ā Ā Collections.sort(
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā intervals,
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā (interval1,
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā interval2) -> interval1.low - interval2.low);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Store the intervals on increasing
Ā Ā Ā Ā Ā Ā Ā Ā // order of end point of interval
Ā Ā Ā Ā Ā Ā Ā Ā PriorityQueue<SpecialInterval> pq
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = new PriorityQueue<SpecialInterval>(
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā intervals.size(),
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā (interval1, interval2)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā -> interval1.high - interval2.high);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Stores count of array elements that
Ā Ā Ā Ā Ā Ā Ā Ā // is present in different intervals
Ā Ā Ā Ā Ā Ā Ā Ā int count = 0;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Stores index of an interval
Ā Ā Ā Ā Ā Ā Ā Ā int j = 0;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Traverse the character array
Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 0;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā i < arr.length && count < intervals.size();
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā i++) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Traverse intervals[] array
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā while (j < intervals.size()) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Stores interval
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // at j-th index
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā SpecialInterval interval = intervals.get(j);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // If start point of interval
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // greater than arr[i]
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (interval.low > arr[i])
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Update j
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā j++;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Insert interval into pq
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā pq.offer(interval);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // If count of intervals
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // in pq greater than 0
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (pq.size() > 0) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Stores top element of pq
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā SpecialInterval interval = pq.poll();
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // arr[i] does not lie in
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // the range of the interval
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (arr[i] > interval.high) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Update count
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā count++;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā return count == intervals.size();
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Function to find the intervals
Ā Ā Ā Ā // for each distinct character of str[]
Ā Ā Ā Ā private static List<SpecialInterval>
Ā Ā Ā Ā findSpecialIntevals(char[] str, char ch)
Ā Ā Ā Ā {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Stores the odd index
Ā Ā Ā Ā Ā Ā Ā Ā // of the character array
Ā Ā Ā Ā Ā Ā Ā Ā int oddPrev = -1;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Stores even index of
Ā Ā Ā Ā Ā Ā Ā Ā // the character array
Ā Ā Ā Ā Ā Ā Ā Ā int evenPrev = -1;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Stores all intervals for each
Ā Ā Ā Ā Ā Ā Ā Ā // distinct character of str
Ā Ā Ā Ā Ā Ā Ā Ā List<SpecialInterval> intervals
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = new ArrayList<SpecialInterval>();
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Traverse the character array
Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i < str.length; i++) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (str[i] == ch) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // If i is even
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (i % 2 == 0) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // If evenPrev not
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // equal to -1
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (evenPrev == -1) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Update evenPrev
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā evenPrev = i;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Initialize an interval
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā SpecialInterval interval
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = new SpecialInterval(evenPrev,
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā i);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Insert current interval
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // into intervals
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā intervals.add(interval);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Update evenPrev
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā evenPrev = -1;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // If oddPrev not
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // equal to -1
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (oddPrev == -1) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Update oddPrev
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā oddPrev = i;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Initialize an interval
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā SpecialInterval interval
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = new SpecialInterval(oddPrev,
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā i);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Insert current interval
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // into intervals
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā intervals.add(interval);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Update oddPrev
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā oddPrev = -1;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā return intervals;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Function to print lexicographically smallest
Ā Ā Ā Ā // character that satisfies the condition
Ā Ā Ā Ā static void printAnswer(char[] str, int arr[])
Ā Ā Ā Ā {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Sort the array
Ā Ā Ā Ā Ā Ā Ā Ā Arrays.sort(arr);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Check if a character is present in
Ā Ā Ā Ā Ā Ā Ā Ā // str that satisfies the condition
Ā Ā Ā Ā Ā Ā Ā Ā boolean possible = false;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // hash[i]: Check if character i
Ā Ā Ā Ā Ā Ā Ā Ā // is present in arr[] or not
Ā Ā Ā Ā Ā Ā Ā Ā boolean[] hash = checkChar(str);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Traverse all possible distinct
Ā Ā Ā Ā Ā Ā Ā Ā // character of str
Ā Ā Ā Ā Ā Ā Ā Ā for (int ch = 0; ch < 26; ch++) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // If ch present in str
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (!hash[ch])
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā continue;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Find all the intervals for
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // current character
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā List<SpecialInterval> intervals
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = findSpecialIntevals(str,
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā (char)(ch + 'a'));
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Update possible
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā possible = checkIfValid(intervals, arr);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // If a character found in str that
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // satisfies the condition
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (possible) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā System.out.println((char)(ch + 'a'));
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // If no character found that
Ā Ā Ā Ā Ā Ā Ā Ā // satisfies the condition
Ā Ā Ā Ā Ā Ā Ā Ā if (!possible)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā System.out.println(-1);
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Driver Code
Ā Ā Ā Ā public static void main(String[] args)
Ā Ā Ā Ā {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā char[] str = { 'a', 'b', 'c', 'a', 'e', 'a', 'a' };
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā int arr[] = { 4, 6 };
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā printAnswer(str, arr);
Ā Ā Ā Ā }
}


Python3




# Structure of an interval
class SpecialInterval:
Ā 
Ā Ā Ā Ā # Initialize a interval
Ā Ā Ā Ā def __init__(self, low, high):
Ā Ā Ā Ā Ā Ā Ā Ā self.low = low
Ā Ā Ā Ā Ā Ā Ā Ā self.high = high
Ā 
# Function to check if a character
# present in arr[] or not
Ā 
Ā 
def checkChar(s):
Ā 
Ā Ā Ā Ā # hash[i]: Check if character i
Ā Ā Ā Ā # is present in arr[] or not
Ā Ā Ā Ā hash = [False] * 26
Ā 
Ā Ā Ā Ā # Traverse the character array
Ā Ā Ā Ā for i in range(len(s)):
Ā Ā Ā Ā Ā Ā Ā Ā # Mark arr[i] as true
Ā Ā Ā Ā Ā Ā Ā Ā hash[ord(s[i]) - 97] = True
Ā 
Ā Ā Ā Ā return hash
Ā 
# Function to check if all the intervals of a
# character satisfies the condition or not
Ā 
Ā 
def checkIfValid(intervals, arr):
Ā 
Ā Ā Ā Ā # If no intervals found for
Ā Ā Ā Ā # the current character
Ā Ā Ā Ā if len(intervals) == 0:
Ā Ā Ā Ā Ā Ā Ā Ā return True
Ā 
Ā Ā Ā Ā # Sort intervals on increasing
Ā Ā Ā Ā # order of start point
Ā Ā Ā Ā intervals.sort(key=lambda x: x.low)
Ā 
Ā Ā Ā Ā # Store the intervals on increasing
Ā Ā Ā Ā # order of end point of interval
Ā Ā Ā Ā pq = []
Ā 
Ā Ā Ā Ā # Stores count of array elements that
Ā Ā Ā Ā # is present in different intervals
Ā Ā Ā Ā count = 0
Ā 
Ā Ā Ā Ā # Stores index of an interval
Ā Ā Ā Ā j = 0
Ā 
Ā Ā Ā Ā # Traverse the character array
Ā Ā Ā Ā for i in range(len(arr)):
Ā Ā Ā Ā Ā Ā Ā Ā if count >= len(intervals):
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā # Traverse intervals[] array
Ā Ā Ā Ā Ā Ā Ā Ā while j < len(intervals):
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # Stores interval
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # at j-th index
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā interval = intervals[j]
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # If start point of interval
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # greater than arr[i]
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if interval.low > arr[i]:
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # Update j
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā j += 1
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # Insert interval into pq
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā pq.append(interval)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā pq.sort(key=lambda x: x.high)
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā # If count of intervals
Ā Ā Ā Ā Ā Ā Ā Ā # in pq greater than 0
Ā Ā Ā Ā Ā Ā Ā Ā if len(pq) > 0:
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # Stores top element of pq
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā interval = pq[0]
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā pq.pop(0)
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # arr[i] does not lie in
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # the range of the interval
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if arr[i] > interval.high:
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # Update count
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā count += 1
Ā 
Ā Ā Ā Ā return count == len(intervals)
Ā 
# Function to find the intervals
# for each distinct character of str[]
Ā 
Ā 
def findSpecialIntervals(s, ch):
Ā Ā Ā Ā # Stores the odd index
Ā Ā Ā Ā # of the character array
Ā Ā Ā Ā oddPrev = -1
Ā 
Ā Ā Ā Ā # Stores even index of
Ā Ā Ā Ā # the character array
Ā Ā Ā Ā evenPrev = -1
Ā 
Ā Ā Ā Ā # Stores all intervals for each
Ā Ā Ā Ā # distinct character of str
Ā Ā Ā Ā intervals = []
Ā 
Ā Ā Ā Ā # Traverse the character array
Ā Ā Ā Ā for i in range(len(s)):
Ā Ā Ā Ā Ā Ā Ā Ā if s[i] == ch:
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # If i is even
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if i % 2 == 0:
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # If evenPrev not
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # equal to -1
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if evenPrev == -1:
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # Update evenPrev
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā evenPrev = i
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else:
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # Initialize an interval
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā interval = SpecialInterval(evenPrev, i)
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # Insert current interval
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # into intervals
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā intervals.append(interval)
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # Update evenPrev
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā evenPrev = -1
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else:
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # If oddPrev not
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # equal to -1
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if oddPrev == -1:
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # Update oddPrev
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā oddPrev = i
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else:
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # Initialize an interval
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā interval = SpecialInterval(oddPrev, i)
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # Insert current interval
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # into intervals
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā intervals.append(interval)
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # Update oddPrev
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā oddPrev = -1
Ā 
Ā Ā Ā Ā return intervals
Ā 
# Function to print lexicographically smallest
# character that satisfies the condition
Ā 
Ā 
def printAnswer(strs, arr):
Ā 
Ā Ā Ā Ā # Sort the array
Ā Ā Ā Ā arr.sort()
Ā 
Ā Ā Ā Ā # Check if a character is present in
Ā Ā Ā Ā # str that satisfies the condition
Ā Ā Ā Ā possible = False
Ā 
Ā Ā Ā Ā # hash[i]: Check if character i
Ā Ā Ā Ā # is present in arr[] or not
Ā Ā Ā Ā hash = checkChar(strs)
Ā 
Ā Ā Ā Ā # print(hash)
Ā Ā Ā Ā # Traverse all possible distinct
Ā Ā Ā Ā # character of str
Ā Ā Ā Ā for ch in range(26):
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā # If ch present in str
Ā Ā Ā Ā Ā Ā Ā Ā if not hash[ch]:
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā continue
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā # Find all the intervals for
Ā Ā Ā Ā Ā Ā Ā Ā # current character
Ā Ā Ā Ā Ā Ā Ā Ā intervals = findSpecialIntervals(strs, chr(ch + 97))
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā # Update possible
Ā Ā Ā Ā Ā Ā Ā Ā possible = checkIfValid(intervals, arr)
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā # If a character found in str that
Ā Ā Ā Ā Ā Ā Ā Ā # satisfies the condition
Ā Ā Ā Ā Ā Ā Ā Ā if possible:
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā print(chr(ch + 97))
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break
Ā 
Ā Ā Ā Ā # If no character found that
Ā Ā Ā Ā # satisfies the condition
Ā Ā Ā Ā if not possible:
Ā Ā Ā Ā Ā Ā Ā Ā print(-1)
Ā 
Ā 
# Driver Code
strs = ['a', 'b', 'c', 'a', 'e', 'a', 'a']
Ā 
arr = [4, 6]
Ā 
printAnswer(strs, arr)
Ā 
# The code is contributed by phasing17


Javascript




// Javascript Program to implement
// the above approach
Ā 
// Structure of an interval
class SpecialInterval {
Ā Ā Ā Ā // Initialize a interval
Ā Ā Ā Ā constructor(low, high)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā this.low = low;
Ā Ā Ā Ā Ā Ā Ā Ā this.high = high;
Ā Ā Ā Ā }
}
Ā 
// Function to check if a character
// present in arr[] or not
function checkChar(str)
{
Ā 
Ā Ā Ā Ā // hash[i]: Check if character i
Ā Ā Ā Ā // is present in arr[] or not
Ā Ā Ā Ā let hash = new Array(26);
Ā 
Ā Ā Ā Ā // Traverse the character array
Ā Ā Ā Ā for (let i = 0; i < str.length; i++) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Mark arr[i] as true
Ā Ā Ā Ā Ā Ā Ā Ā hash[str[i].charCodeAt(0) - 97] = true;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā return hash;
}
Ā 
// Function to check if all the intervals of a
// character satisfies the condition or not
function checkIfValid(intervals, arr)
{
Ā 
Ā Ā Ā Ā // If no intervals found for
Ā Ā Ā Ā // the current character
Ā Ā Ā Ā if (intervals.length == 0) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā return true;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Sort intervals on increasing
Ā Ā Ā Ā // order of start point
Ā Ā Ā Ā intervals.sort(function(a, b){
Ā Ā Ā Ā Ā Ā Ā Ā return a.low - b.low;
Ā Ā Ā Ā });
Ā 
Ā Ā Ā Ā // Store the intervals on increasing
Ā Ā Ā Ā // order of end point of interval
Ā Ā Ā Ā let pq = [];
Ā 
Ā Ā Ā Ā // Stores count of array elements that
Ā Ā Ā Ā // is present in different intervals
Ā Ā Ā Ā let count = 0;
Ā 
Ā Ā Ā Ā // Stores index of an interval
Ā Ā Ā Ā let j = 0;
Ā 
Ā Ā Ā Ā // Traverse the character array
Ā Ā Ā Ā for (let i = 0; i < arr.length && count < intervals.length; i++) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Traverse intervals[] array
Ā Ā Ā Ā Ā Ā Ā Ā while (j < intervals.length) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Stores interval
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // at j-th index
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā let interval = intervals[j];
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // If start point of interval
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // greater than arr[i]
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (interval.low > arr[i])
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Update j
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā j++;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Insert interval into pq
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā pq.push(interval);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā pq.sort(function(a, b){
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return a.high - b.high;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā });
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // If count of intervals
Ā Ā Ā Ā Ā Ā Ā Ā // in pq greater than 0
Ā Ā Ā Ā Ā Ā Ā Ā if (pq.length > 0) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Stores top element of pq
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā let interval = pq[0];
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā pq.shift();
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // arr[i] does not lie in
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // the range of the interval
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (arr[i] > interval.high) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Update count
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā count++;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā return count == intervals.length;
}
Ā 
// Function to find the intervals
// for each distinct character of str[]
function findSpecialIntevals(str, ch)
{
Ā 
Ā Ā Ā Ā // Stores the odd index
Ā Ā Ā Ā // of the character array
Ā Ā Ā Ā let oddPrev = -1;
Ā 
Ā Ā Ā Ā // Stores even index of
Ā Ā Ā Ā // the character array
Ā Ā Ā Ā let evenPrev = -1;
Ā 
Ā Ā Ā Ā // Stores all intervals for each
Ā Ā Ā Ā // distinct character of str
Ā Ā Ā Ā let intervals = [];
Ā 
Ā Ā Ā Ā // Traverse the character array
Ā Ā Ā Ā for (let i = 0; i < str.length; i++) {
Ā Ā Ā Ā Ā Ā Ā Ā if (str[i] == ch) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // If i is even
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (i % 2 == 0) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // If evenPrev not
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // equal to -1
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (evenPrev == -1) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Update evenPrev
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā evenPrev = i;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Initialize an interval
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā let interval = new SpecialInterval(evenPrev, i);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Insert current interval
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // into intervals
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā intervals.push(interval);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Update evenPrev
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā evenPrev = -1;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // If oddPrev not
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // equal to -1
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (oddPrev == -1) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Update oddPrev
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā oddPrev = i;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Initialize an interval
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā let interval = new SpecialInterval( oddPrev, i);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Insert current interval
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // into intervals
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā intervals.push(interval);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Update oddPrev
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā oddPrev = -1;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
Ā Ā Ā Ā return intervals;
}
Ā 
// Function to print lexicographically smallest
// character that satisfies the condition
function printAnswer(str, arr)
{
Ā 
Ā Ā Ā Ā // Sort the array
Ā Ā Ā Ā arr.sort();
Ā 
Ā Ā Ā Ā // Check if a character is present in
Ā Ā Ā Ā // str that satisfies the condition
Ā Ā Ā Ā let possible = false;
Ā 
Ā Ā Ā Ā // hash[i]: Check if character i
Ā Ā Ā Ā // is present in arr[] or not
Ā Ā Ā Ā let hash = checkChar(str);
Ā 
Ā Ā Ā Ā // Traverse all possible distinct
Ā Ā Ā Ā // character of str
Ā Ā Ā Ā for (let ch = 0; ch < 26; ch++) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // If ch present in str
Ā Ā Ā Ā Ā Ā Ā Ā if (!hash[ch])
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā continue;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Find all the intervals for
Ā Ā Ā Ā Ā Ā Ā Ā // current character
Ā Ā Ā Ā Ā Ā Ā Ā let intervals = findSpecialIntevals(str, String.fromCharCode(ch + 97));
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Update possible
Ā Ā Ā Ā Ā Ā Ā Ā possible = checkIfValid(intervals, arr);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // If a character found in str that
Ā Ā Ā Ā Ā Ā Ā Ā // satisfies the condition
Ā Ā Ā Ā Ā Ā Ā Ā if (possible) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā console.log(String.fromCharCode(ch + 97));
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // If no character found that
Ā Ā Ā Ā // satisfies the condition
Ā Ā Ā Ā if (!possible)
Ā Ā Ā Ā Ā Ā Ā Ā console.log(-1);
}
Ā 
// Driver Code
let str = ['a', 'b', 'c', 'a', 'e', 'a', 'a'];
Ā 
let arr = [4, 6];
Ā 
printAnswer(str, arr);
Ā 
// The code is contributed by Arushi Jindal.


C#




// C# code addition
Ā 
using System;
using System.Collections.Generic;
using System.Linq;
Ā 
public class SpecialInterval {
Ā Ā Ā Ā public int low;
Ā Ā Ā Ā public int high;
Ā 
Ā Ā Ā Ā // Initialize an interval
Ā Ā Ā Ā public SpecialInterval(int low, int high)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā this.low = low;
Ā Ā Ā Ā Ā Ā Ā Ā this.high = high;
Ā Ā Ā Ā }
}
Ā 
public class Program {
Ā Ā Ā Ā // Function to check if a character
Ā Ā Ā Ā // is present in arr[] or not
Ā Ā Ā Ā public static bool[] CheckChar(char[] str)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā // hash[i]: Check if character i
Ā Ā Ā Ā Ā Ā Ā Ā // is present in arr[] or not
Ā Ā Ā Ā Ā Ā Ā Ā bool[] hash = new bool[26];
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Traverse the character array
Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i < str.Length; i++) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Mark arr[i] as true
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā hash[str[i] - 'a'] = true;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā return hash;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Function to check if all the intervals of a
Ā Ā Ā Ā // character satisfies the condition or not
Ā Ā Ā Ā public static bool
Ā Ā Ā Ā CheckIfValid(List<SpecialInterval> intervals, int[] arr)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā // If no intervals found for
Ā Ā Ā Ā Ā Ā Ā Ā // the current character
Ā Ā Ā Ā Ā Ā Ā Ā if (intervals.Count == 0) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return true;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Sort intervals on increasing
Ā Ā Ā Ā Ā Ā Ā Ā // order of start point
Ā Ā Ā Ā Ā Ā Ā Ā intervals.Sort((a, b) = > a.low.CompareTo(b.low));
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Store the intervals on increasing
Ā Ā Ā Ā Ā Ā Ā Ā // order of end point of interval
Ā Ā Ā Ā Ā Ā Ā Ā List<SpecialInterval> pq
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = new List<SpecialInterval>();
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Stores count of array elements that
Ā Ā Ā Ā Ā Ā Ā Ā // is present in different intervals
Ā Ā Ā Ā Ā Ā Ā Ā int count = 0;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Stores index of an interval
Ā Ā Ā Ā Ā Ā Ā Ā int j = 0;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Traverse the character array
Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 0;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā i < arr.Length && count < intervals.Count;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā i++) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Traverse intervals[] array
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā while (j < intervals.Count) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Stores interval
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // at j-th index
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā SpecialInterval interval = intervals[j];
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // If start point of interval
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // greater than arr[i]
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (interval.low > arr[i]) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Update j
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā j++;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Insert interval into pq
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā pq.Add(interval);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā pq.Sort((a, b) =
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā > a.high.CompareTo(b.high));
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // If count of intervals
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // in pq greater than 0
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (pq.Count > 0) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Stores top element of pq
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā SpecialInterval interval = pq[0];
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā pq.RemoveAt(0);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // arr[i] does not lie in
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // the range of the interval
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (arr[i] > interval.high) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Update count
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā count++;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā return count == intervals.Count;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Function to find the intervals
Ā Ā Ā Ā // for each distinct character of str[]
Ā Ā Ā Ā public static List<SpecialInterval>
Ā Ā Ā Ā FindSpecialIntervals(char[] str, char ch)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā // Stores the odd index
Ā Ā Ā Ā Ā Ā Ā Ā // of the character array
Ā Ā Ā Ā Ā Ā Ā Ā int oddPrev = -1;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Stores even index of
Ā Ā Ā Ā Ā Ā Ā Ā // the character array
Ā Ā Ā Ā Ā Ā Ā Ā int evenPrev = -1;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Stores all intervals for each
Ā Ā Ā Ā Ā Ā Ā Ā // distinct character of str
Ā Ā Ā Ā Ā Ā Ā Ā List<SpecialInterval> intervals
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = new List<SpecialInterval>();
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Traverse the character array
Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i < str.Length; i++) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (str[i] == ch) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // If i is even
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (i % 2 == 0) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // If evenPrev not
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // equal to -1
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (evenPrev == -1) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Update evenPrev
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā evenPrev = i;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Initialize an interval
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā var interval = new SpecialInterval(
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā evenPrev, i);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Insert current interval
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // into intervals
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā intervals.Add(interval);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Update evenPrev
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā evenPrev = -1;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // If oddPrev not
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // equal to -1
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (oddPrev == -1) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Update oddPrev
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā oddPrev = i;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Initialize an interval
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā var interval = new SpecialInterval(
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā oddPrev, i);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Insert current interval
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // into intervals
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā intervals.Add(interval);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Update oddPrev
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā oddPrev = -1;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā return intervals;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Function to print lexicographically smallest
Ā Ā Ā Ā // character that satisfies the condition
Ā Ā Ā Ā public static void printAnswer(char[] str, int[] arr)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā // Sort the array
Ā Ā Ā Ā Ā Ā Ā Ā Array.Sort(arr);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Check if a character is present in
Ā Ā Ā Ā Ā Ā Ā Ā // str that satisfies the condition
Ā Ā Ā Ā Ā Ā Ā Ā bool possible = false;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // hash[i]: Check if character i
Ā Ā Ā Ā Ā Ā Ā Ā // is present in arr[] or not
Ā Ā Ā Ā Ā Ā Ā Ā bool[] hash = CheckChar(str);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Traverse all possible distinct
Ā Ā Ā Ā Ā Ā Ā Ā // character of str
Ā Ā Ā Ā Ā Ā Ā Ā for (int ch = 0; ch < 26; ch++) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // If ch present in str
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (!hash[ch])
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā continue;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Find all the intervals for
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // current character
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā List<SpecialInterval> intervals
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = FindSpecialIntervals(str,
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā (char)(ch + 'a'));
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Update possible
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā possible = CheckIfValid(intervals, arr);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // If a character found in str that
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // satisfies the condition
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (possible) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Console.WriteLine((char)(ch + 'a'));
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // If no character found that
Ā Ā Ā Ā Ā Ā Ā Ā // satisfies the condition
Ā Ā Ā Ā Ā Ā Ā Ā if (!possible)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Console.WriteLine(-1);
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Driver Code
Ā Ā Ā Ā static void Main()
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā // Console.WriteLine("hi2");
Ā Ā Ā Ā Ā Ā Ā Ā char[] str = { 'a', 'b', 'c', 'a', 'e', 'a', 'a' };
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā int[] arr = { 4, 6 };
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā printAnswer(str, arr);
Ā Ā Ā Ā }
}
Ā 
// The code is contributed by Arushi Goel.


Output:Ā 

a

Ā 

Time Complexity: O(N * log(N))
Auxiliary Space: O(N)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Thapelo Manthata
Iā€™m a desktop support specialist transitioning into a SharePoint developer role by day and Software Engineering student by night. My superpowers include customer service, coding, the Microsoft office 365 suite including SharePoint and power platform.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments