N numbers were converted into their respective Binary form of M length and arranged in an NxM matrix.
Your task is to find the index of row (1 based indexing) which contains the binary number with maximum value. If multiple rows have maximum value then print the row with minimum index.
Example:
Input: n = 4, m = 4, matrix = {"1010", "0100", "0010", "0011"}
Output: 1
Approach
C++
#include <bits/stdc++.h>using namespace std;bool comp(pair<string, int> p1, pair<string, int> p2){if (p1.first != p2.first){return (p1.first < p2.first);}else{return (p1.second > p2.second);}}void binaryMatrix(int n, int m, vector<string> &matrix){vector<pair<string, int>> v;for (int i = 0; i < n; i++){string inp = matrix[i];v.push_back(make_pair(inp, i + 1));}sort(v.begin(), v.end(), comp);cout << (v[n - 1].second);}int main(){int n = 4, m = 4;vector<string> matrix = {"1010","0100","0010","0011"};binaryMatrix(n, m, matrix);return 0;}
No comments:
Post a Comment