Find the row with maximum unique elements in given Matrix
Last Updated :
17 Apr, 2022
Given a matrix arr[][] of size N*M The task is to find the index of the row that has the maximum unique elements. If there are multiple rows possible, return the minimum indexed row.
Examples:
Input: arr[][] = { {1, 2, 3, 4, 5}, {1, 2, 2, 4, 7}, {1, 3, 1, 3, 1} }
Output: 0
Explanation: Rows 0, 1 & 2 have 5, 4 & 2 unique elements respectively.
Input: arr[][] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 1, 2, 3} }
Output : 0
Approach: The task can be solved using a set data structure. Iterate over the rows and store the corresponding element inside a set to keep track of the unique elements, return the minimum indexed row with maximum unique elements.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int get( int n, int m, vector<vector< int > >& v)
{
set< int > s;
int max_ans = INT_MAX;
int cnt = -1;
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < m; j++) {
s.insert(v[i][j]);
}
int size = ( int )s.size();
if (cnt < size) {
size = cnt;
max_ans = min(max_ans, i);
}
s.clear();
}
return max_ans;
}
int main()
{
vector<vector< int > > arr
= { { 1, 2, 3, 4, 5 },
{ 1, 2, 2, 4, 7 },
{ 1, 3, 1, 3, 1 } };
int n = arr.size();
int m = arr[0].size();
cout << get(n, m, arr);
}
|
Java
import java.util.*;
class GFG
{
public static int get( int n, int m,
ArrayList<ArrayList<Integer> > v)
{
HashSet<Integer> s = new HashSet<Integer>();
int max_ans = Integer.MAX_VALUE;
int cnt = - 1 ;
for ( int i = 0 ; i < n; i++) {
for ( int j = 0 ; j < m; j++) {
s.add(v.get(i).get(j));
}
int size = ( int )s.size();
if (cnt < size) {
size = cnt;
max_ans = Math.min(max_ans, i);
}
s.clear();
}
return max_ans;
}
public static void main(String[] args)
{
ArrayList<ArrayList<Integer> > arr
= new ArrayList<ArrayList<Integer> >();
ArrayList<Integer> temp1
= new ArrayList<>(Arrays.asList( 1 , 2 , 3 , 4 , 5 ));
ArrayList<Integer> temp2
= new ArrayList<>(Arrays.asList( 1 , 2 , 2 , 4 , 7 ));
ArrayList<Integer> temp3
= new ArrayList<>(Arrays.asList( 1 , 3 , 1 , 3 , 1 ));
arr.add(temp1);
arr.add(temp2);
arr.add(temp3);
int n = arr.size();
int m = arr.get( 0 ).size();
System.out.print(get(n, m, arr));
}
}
|
Python3
def get(n, m, v):
s = set ()
max_ans = 1000000000
cnt = - 1
for i in range ( 0 , n):
for j in range ( 0 , m):
s.add(v[i][j])
size = len (s)
if cnt < size:
size = cnt
max_ans = min (max_ans, i)
s.clear()
return max_ans
if __name__ = = "__main__" :
arr = [[ 1 , 2 , 3 , 4 , 5 ],
[ 1 , 2 , 2 , 4 , 7 ],
[ 1 , 3 , 1 , 3 , 1 ]]
n = 3
m = 5
print (get(n, m, arr))
|
C#
using System;
using System.Linq;
using System.Collections.Generic;
public class GFG {
public static int get ( int n, int m, List<List< int >> v) {
HashSet< int > s = new HashSet< int >();
int max_ans = int .MaxValue;
int cnt = -1;
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < m; j++) {
s.Add(v[i][j]);
}
int size = ( int ) s.Count;
if (cnt < size) {
size = cnt;
max_ans = Math.Min(max_ans, i);
}
s.Clear();
}
return max_ans;
}
public static void Main(String[] args) {
List<List< int >> arr = new List<List< int >>();
int [] t1 = {1, 2, 3, 4, 5};
List< int > temp1 = new List< int >();
temp1 = (t1.ToList());
List< int > temp2 = new List< int >();
int []t2 = {1, 2, 2, 4, 7};
temp2 = (t2.ToList());
List< int > temp3 = new List< int >();
int [] t3 = {1, 3, 1, 3, 1};
temp3 = t3.ToList();
arr.Add(temp1);
arr.Add(temp2);
arr.Add(temp3);
int n = arr.Count;
int m = arr[0].Count;
Console.Write( get (n, m, arr));
}
}
|
Javascript
<script>
function get(n, m, v) {
let s = new Set();
let max_ans = Number.MAX_VALUE;
let cnt = -1;
for (let i = 0; i < n; i++) {
for (let j = 0; j < m; j++) {
s.add(v[i][j]);
}
let size = s.size;
if (cnt < size) {
size = cnt;
max_ans = Math.min(max_ans, i);
}
s.clear();
}
return max_ans;
}
let arr
= [[1, 2, 3, 4, 5],
[1, 2, 2, 4, 7],
[1, 3, 1, 3, 1]];
let n = arr.length;
let m = arr[0].length;
document.write(get(n, m, arr));
</script>
|
Time Complexity: O(N*M*logM)
Auxiliary Space: O(M)
Share your thoughts in the comments
Please Login to comment...