Find the element in a linked list with frequency at least N/3
Given a linked list of size N consisting of a string as node value, the task is to find the majority string, having frequency greater than [N/3], in the linked list.
Note: It is guaranteed that there is only one majority string.
Examples:
Input: head -> geeks -> geeks -> abcd -> game -> knight -> geeks -> harry.
Output: geeks.
Explanation:
The frequency of geeks string in link list is 3 which is greater than [7/3] i.e 2.
Input: head -> hot -> hot -> cold -> hot -> hot
Output: hot
Explanation:
The frequency of hot string in the link list is 4 which is greater than [5/3] i.e 1.
Naive Approach:
Store the frequency of every string in a Map. Traverse the map and look for the string whose frequency is ? N / 3.
Time Complexity: O(N)
Auxiliary Space: O(N)
Efficient Approach:
The idea is based on Moore’s Voting algorithm.
Find two candidates and check if any of these two candidates is actually a majority element or not.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct node {
string i;
node* next = NULL;
};
struct node* newnode(string s)
{
struct node* temp = ( struct node*)
malloc ( sizeof ( struct node));
temp->i = s;
temp->next = NULL;
return temp;
}
string Majority_in_linklist(node* head)
{
string s = "" , t = "" ;
int p = 0, q = 0;
node* ptr = NULL;
while (head != NULL) {
if (s.compare(head->i) == 0) {
p = p + 1;
}
else {
if (t.compare(head->i) == 0) {
q = q + 1;
}
else {
if (p == 0) {
s = head->i;
p = 1;
}
else {
if (q == 0) {
t = head->i;
q = 1;
}
else {
p = p - 1;
q = q - 1;
}
}
}
}
head = head->next;
}
head = ptr;
p = 0;
q = 0;
while (head != NULL) {
if (s.compare(head->i) == 0) {
p = 1;
}
else {
if (t.compare(head->i) == 0) {
q = 1;
}
}
head = head->next;
}
if (p > q) {
return s;
}
else {
return t;
}
}
int main()
{
node* ptr = NULL;
node* head = newnode( "geeks" );
head->next = newnode( "geeks" );
head->next->next = newnode( "abcd" );
head->next->next->next
= newnode( "game" );
head->next->next->next->next
= newnode( "game" );
head->next->next->next->next->next
= newnode( "knight" );
head->next->next->next->next->next->next
= newnode( "harry" );
head->next->next->next->next->next->next
->next
= newnode( "geeks" );
cout << Majority_in_linklist(head) << endl;
return 0;
}
|
Java
class GFG{
static class node
{
String i;
node next = null ;
};
static node newnode(String s)
{
node temp = new node();
temp.i = s;
temp.next = null ;
return temp;
}
static String Majority_in_linklist(node head)
{
String s = "" ;
String t = "" ;
int p = 0 , q = 0 ;
node ptr = null ;
while (head != null )
{
if (s.equals(head.i))
{
p = p + 1 ;
}
else
{
if (t.equals(head.i))
{
q = q + 1 ;
}
else
{
if (p == 0 )
{
s = head.i;
p = 1 ;
}
else
{
if (q == 0 )
{
t = head.i;
q = 1 ;
}
else
{
p = p - 1 ;
q = q - 1 ;
}
}
}
}
head = head.next;
}
head = ptr;
p = 0 ;
q = 0 ;
while (head != null )
{
if (s.equals(head.i))
{
p = 1 ;
}
else
{
if (t.equals(head.i))
{
q = 1 ;
}
}
head = head.next;
}
if (p > q)
{
return s;
}
else
{
return t;
}
}
public static void main(String []arg)
{
node ptr = null ;
node head = newnode( "geeks" );
head.next = newnode( "geeks" );
head.next.next = newnode( "abcd" );
head.next.next.next = newnode( "game" );
head.next.next.next.next = newnode( "game" );
head.next.next.next.next.next = newnode( "knight" );
head.next.next.next.next.next.next = newnode( "harry" );
head.next.next.next.next.next.next.next = newnode( "geeks" );
System.out.println(Majority_in_linklist(head));
}
}
|
Python3
class Node:
def __init__( self , s):
self .i = s
self . next = None
def Majority_in_linklist(head):
s, t = " ", " "
p, q = 0 , 0
ptr = None
while head ! = None :
if s = = head.i:
p = p + 1
else :
if t = = head.i:
q = q + 1
else :
if p = = 0 :
s = head.i
p = 1
else :
if q = = 0 :
t = head.i
q = 1
else :
p = p - 1
q = q - 1
head = head. next
head = ptr
p = 0
q = 0
while head ! = None :
if s = = head.i:
p = 1
else :
if t = = head.i:
q = 1
head = head. next
if p > q:
return s
else :
return t
ptr = None
head = Node( "geeks" )
head. next = Node( "geeks" )
head. next . next = Node( "abcd" )
head. next . next . next = Node( "game" )
head. next . next . next . next = Node( "game" )
head. next . next . next . next . next = Node( "knight" )
head. next . next . next . next . next . next = Node( "harry" )
head. next . next . next . next . next . next . next = Node( "geeks" )
print (Majority_in_linklist(head))
|
C#
using System;
using System.Collections;
using System.Collections.Generic;
class GFG{
class node
{
public string i;
public node next = null ;
};
static node newnode( string s)
{
node temp = new node();
temp.i = s;
temp.next = null ;
return temp;
}
static string Majority_in_linklist(node head)
{
string s = "" ;
string t = "" ;
int p = 0, q = 0;
node ptr = null ;
while (head != null )
{
if (s.Equals(head.i))
{
p = p + 1;
}
else
{
if (t.Equals(head.i))
{
q = q + 1;
}
else
{
if (p == 0)
{
s = head.i;
p = 1;
}
else
{
if (q == 0)
{
t = head.i;
q = 1;
}
else
{
p = p - 1;
q = q - 1;
}
}
}
}
head = head.next;
}
head = ptr;
p = 0;
q = 0;
while (head != null )
{
if (s.Equals(head.i))
{
p = 1;
}
else
{
if (t.Equals(head.i))
{
q = 1;
}
}
head = head.next;
}
if (p > q)
{
return s;
}
else
{
return t;
}
}
public static void Main( string []arg)
{
node head = newnode( "geeks" );
head.next = newnode( "geeks" );
head.next.next = newnode( "abcd" );
head.next.next.next = newnode( "game" );
head.next.next.next.next = newnode( "game" );
head.next.next.next.next.next = newnode( "knight" );
head.next.next.next.next.next.next = newnode( "harry" );
head.next.next.next.next.next.next.next = newnode( "geeks" );
Console.Write(Majority_in_linklist(head));
}
}
|
Javascript
<script>
class node {
constructor() {
this .i = "" ;
this .next = null ;
}
}
function newnode(s) {
var temp = new node();
temp.i = s;
temp.next = null ;
return temp;
}
function Majority_in_linklist(head) {
var s = "" ;
var t = "" ;
var p = 0,
q = 0;
var ptr = null ;
while (head != null ) {
if (s == head.i) {
p = p + 1;
} else {
if (t == head.i) {
q = q + 1;
} else {
if (p == 0) {
s = head.i;
p = 1;
} else {
if (q == 0) {
t = head.i;
q = 1;
} else {
p = p - 1;
q = q - 1;
}
}
}
}
head = head.next;
}
head = ptr;
p = 0;
q = 0;
while (head != null ) {
if (s == head.i) {
p = 1;
} else {
if (t == head.i) {
q = 1;
}
}
head = head.next;
}
if (p > q) {
return s;
} else {
return t;
}
}
var head = newnode( "geeks" );
head.next = newnode( "geeks" );
head.next.next = newnode( "abcd" );
head.next.next.next = newnode( "game" );
head.next.next.next.next = newnode( "game" );
head.next.next.next.next.next = newnode( "knight" );
head.next.next.next.next.next.next = newnode( "harry" );
head.next.next.next.next.next.next.next = newnode( "geeks" );
document.write(Majority_in_linklist(head));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
19 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...