0% found this document useful (0 votes)
6 views

Coding problems ( HackWithInfy)

Ramu wants to choose the maximum number of non-adjacent dishes of the same type from a given list. The input consists of multiple test cases where for each case, the type of dish that allows the maximum selection is to be determined. The solution involves counting the maximum non-adjacent dishes for each type and selecting the type with the highest count, or the smallest type in case of a tie.

Uploaded by

jenc22024.cd
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Coding problems ( HackWithInfy)

Ramu wants to choose the maximum number of non-adjacent dishes of the same type from a given list. The input consists of multiple test cases where for each case, the type of dish that allows the maximum selection is to be determined. The solution involves counting the maximum non-adjacent dishes for each type and selecting the type with the highest count, or the smallest type in case of a tie.

Uploaded by

jenc22024.cd
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

HackWith infy

Ramu has N dishes of different types arranged in a row: A1,A2,…,AN where Ai denotes the type of
the ith dish. He wants to choose as many dishes as possible from the given list but while satisfying
two conditions:

1. He can choose only one type of dish.

2. No two chosen dishes should be adjacent to each other.

Ramu wants to know which type of dish he should choose from, so that he can pick the maximum
number of dishes.

Example :

Given N= 9 and A= [1,2,2,1,2,1,1,1,1]

For type 1, Ramu can choose at most four dishes. One of the ways to choose four dishes of type 1 is
A1,A4, A7 and A9.

For type 2, Ramu can choose at most two dishes. One way is to choose A3 and A5.

So in this case, Ramu should go for type 1, in which he can pick more dishes.

INPUT FORMAT:

 The first line contains T, the number of test cases. Then the test cases follow.

 For each test case, the first line contains a single integer N.

 The second line contains N integers A1,A2,…,AN.

OUTPUT FORMAT

For each test case, print a single line containing one integer ― the type of the dish that Ramu should
choose from. If there are multiple answers, print the smallest one.

CONSTRAINTS :

 1 <= T <= 10^3

 1 <= N <= 10^3

 1 <= Ai <= 10^3

Sample Input :

12212

111111

12223421
Sample Output :

You are given an array of variable pairs equations and an array of real numbers values,
where equations[i] = [Ai, Bi] and values[i] represent the equation Ai / Bi = values[i]. Each Ai or Bi is a
string that represents a single variable.

You are also given some queries, where queries[j] = [Cj, Dj] represents the jth query where you must
find the answer for Cj / Dj = ?.

Return the answers to all queries. If a single answer cannot be determined, return -1.0.

Note: The input is always valid. You may assume that evaluating the queries will not result in division
by zero and that there is no contradiction.

Note: The variables that do not occur in the list of equations are undefined, so the answer cannot be
determined for them.

Example 1:

Input: equations = [["a","b"],["b","c"]], values = [2.0,3.0], queries = [["a","c"],["b","a"],["a","e"],


["a","a"],["x","x"]]

Output: [6.00000,0.50000,-1.00000,1.00000,-1.00000]

Explanation:

Given: a / b = 2.0, b / c = 3.0

queries are: a / c = ?, b / a = ?, a / e = ?, a / a = ?, x / x = ?

return: [6.0, 0.5, -1.0, 1.0, -1.0 ]

note: x is undefined => -1.0

Example 2:

Input: equations = [["a","b"],["b","c"],["bc","cd"]], values = [1.5,2.5,5.0], queries = [["a","c"],["c","b"],


["bc","cd"],["cd","bc"]]

Output: [3.75000,0.40000,5.00000,0.20000]

Example 3:

Input: equations = [["a","b"]], values = [0.5], queries = [["a","b"],["b","a"],["a","c"],["x","y"]]


Output: [0.50000,2.00000,-1.00000,-1.00000]

Constraints:

 1 <= equations.length <= 20

 equations[i].length == 2

 1 <= Ai.length, Bi.length <= 5

 values.length == equations.length

 0.0 < values[i] <= 20.0

 1 <= queries.length <= 20

 queries[i].length == 2

 1 <= Cj.length, Dj.length <= 5

 Ai, Bi, Cj, Dj consist of lower case English letters and digits.

// solution which gives type as well as count

import java.util.*;

/*

12212

111111

12223421

*/

class Main {

public static void solution( int a[] , int n) {

int i = 0;

int maximum = 0;
int type = a[0];

int c = 0;

while(i <= n - 1) { // i = 0

int j = i + 1; // j = 1

int counter = 1;

while(j < n) { // 1, 2 , 3, 4

if(a[i] == a[j] && j != i + 1) {

counter = counter+ 1;

c = counter;

if(j < n - 1 && a[j] == a[j + 1]) {

j = j+ 1;

j = j + 1;

if(c > maximum) {

maximum = c;

type = a[i];

i = i + 1;

System.out.println("type of dish is: " + type + " max count"+maximum);

}
public static void main(String args[]) {

Scanner s = new Scanner(System.in);

int tCases = s.nextInt();

for( int i = 0 ; i < tCases; i++) {

int n = s.nextInt();

int a[] = new int[n];

for(int k = 0 ; k< n ; k++) {

a[k] = s.nextInt();

solution(a , n);

https://leetcode.com/discuss/post/1326900/graph-algorithms-problems-to-practice-by-9u6j/

https://leetcode.com/problems/evaluate-division/description/

You might also like