Given three arrays firstkey[], secondkey[] and value[] where elements firstkey[i] and secondkey[i] denotes a composite key and their value is value[i], the task is to answer Q queries, each having two elements which denotes the composite key, whose corresponding value needs to be printed.
Examples:
Input: firstkey[] = {4, 4, 5}
secondkey[] = {2, 1, 3}
value[] = {5, 3, 8}, Q = {(4, 1)}
Output: 3
Explanation:
The composite key is present at firstkey[1] (= 4) and secondkey[1] (= 1)
Therefore, the corresponding value is value[1] = 3
Input: firstkey[] = {3, 4, 3}
secondkey[] = {7, 1, 3}
value[] = {2, 3, 6}, Q = {(3, 3)}
Output: 6
Explanation:
The composite key is present at firstkey[2] (= 3) and secondkey[2] (= 3)
Therefore, the corresponding value is value[2] = 6
Approach: The idea is to use map where the key of the map is a pair of two integers in C++, tuple in python denoting the respective elements of firstkey[] and secondkey[] which is mapped to the corresponding value[] element. This enables us to answer every query in the O(1) time.
For Example:
Given arrays be -
firstkey[] = {4, 4, 5}
secondkey[] = {7, 1, 3}
value[] = {5, 3, 8}
Iterating over the Array, the map thus
formed is {(4, 7): 5, (4, 1): 3, (5, 3): 8}
Below is the implementation of the above approach:
C++
// C++ implementation to find the // value of the given composite keys #include <bits/stdc++.h> using namespace std; // Function to find the composite // key value for multiple queries void findCompositeKey(int firstkey[], int secondkey[], int value[], int n, vector<pair<int, int> > q) { // Map to store the composite // key with a value map<pair<int, int>, int> M; // Iterate over the arrays // and generate the required // mappings for (int i = 0; i < n; i++) { M.insert({ { firstkey[i], secondkey[i] }, value[i] }); } // Loop to iterate over the // elements of the queries for (auto i : q) { // Condition to check if there // is the composite key in map if (M.find({ i.first, i.second }) != M.end()) { cout << M[{ i.first, i.second }] << endl; } else { cout << "Not Found" << endl; } } } // Driver Code int main() { int firstkey[] = { 4, 4, 5 }; int secondkey[] = { 2, 1, 3 }; int value[] = { 5, 3, 8 }; int n = 3; vector<pair<int, int> > q = { { 4, 1 }, { 5, 2 }, { 5, 3 } }; findCompositeKey(firstkey, secondkey, value, n, q); return 0; } |
Java
// Java implementation to find the // value of the given composite keys import java.util.*; class GFG{ static class Pair { int first, second; Pair(int first, int second) { this.first = first; this.second = second; } @Override public boolean equals(Object obj) { if (obj == null) return false; if (!(obj instanceof Pair)) return false; if (obj == this) return true; return (this.first == ((Pair)obj).first) && (this.second == ((Pair)obj).second); } @Override public int hashCode() { return this.first + this.second; } } // Function to find the composite // key value for multiple queries static void findCompositeKey(int firstkey[], int secondkey[], int value[], int n, int[][] q) { // Map to store the composite // key with a value Map<Pair, Integer> M = new HashMap<>(); // Iterate over the arrays // and generate the required // mappings for(int i = 0; i < n; i++) { M.put(new Pair(firstkey[i], secondkey[i]), value[i]); } // Loop to iterate over the // elements of the queries for(int i = 0; i < q.length; i++) { // Condition to check if there // is the composite key in map if (M.containsKey(new Pair(q[i][0], q[i][1]))) { System.out.println(M.get(new Pair(q[i][0], q[i][1]))); } else { System.out.println("Not Found"); } } } // Driver code public static void main(String[] args) { int firstkey[] = { 4, 4, 5 }; int secondkey[] = { 2, 1, 3 }; int value[] = { 5, 3, 8 }; int n = 3; int[][] q = { { 4, 1 }, { 5, 2 }, { 5, 3 } }; findCompositeKey(firstkey, secondkey, value, n, q); } } // This code is contributed by offbeat |
3 Not Found 8
Recommended Posts:
- Check for Palindrome after every character replacement Query
- Find the largest composite number that divides N but is strictly lesser than N
- Find the maximum number of composite summands of a number
- XOR of K largest prime and composite numbers from the given array
- XOR of K smallest prime and composite numbers from the given array
- Sum of all the Composite Numbers from Odd indices of the given array
- Query to find the maximum and minimum weight between two nodes in the given tree using LCA.
- Product of all the Composite Numbers in an array
- Bitwise AND of the sum of prime numbers and the sum of composite numbers in an array
- Sum of elements in an array having composite frequency
- Check if a number exists with X divisors out of which Y are composite
- Number of odd and even results for every value of x in range [min, max] after performing N steps
- Find the maximum possible value of the minimum value of modified array
- Find the XOR of the elements in the given range [L, R] with the value K for a given set of queries
- Range Minimum Query (Square Root Decomposition and Sparse Table)
- Pair with minimum absolute difference after solving each query
- Range maximum query using Sparse Table
- Query to count odd and even parity elements in subarray after XOR with K
- Only integer with positive value in positive negative value in array
- Find number of Employees Under every Employee
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : offbeat


