0 like 1 dislike
1,495 views
in Service-based-companies by Expert (14,170 points)
edited by | 1,495 views

1 Answer

0 like 0 dislike

Question :-

 

 

Answer :-

def banquet(arr, m, ans):
    for i in range(m):
        for j in range(len(arr[i])):
            ele = arr[i][j]
            key = ele
            dic.setdefault(key, [])
            dic[key].append([i, j])
    for k, v in dic.items():
        count = 1
        v.sort()
        for i in range(len(v) - 1):
            if v[i][0] == v[i + 1][0]:
                continue
            else:
                count += 1
        if count == m:
            for j in v:
                r = j[0]
                c = j[1]
                ans[r][c] = 0
        elif count < m / 2:
            for j in v:
                r1 = j[0]
                c1 = j[1]
                ans[r1][c1] = 1
        else:
            for t in v:
                r2 = t[0]
                c2 = t[1]
                if r2 % 2 == 0:
                    ans[r2][c2] = 1
                else:
                    ans[r2][c2] = 0

    for i in range(len(ans)):
        zeros = 0
        ones = 0
        for j in range(len(ans[0])):
            if ans[i][j] == 0:
                zeros += 1
            else:
                ones += 1
        if zeros == ones:
            for k in range(len(ans[0])):
                if ans[i][k] == 0:
                    ans[i][k] = 1
                else:
                    ans[i][k] = 0
        elif zeros == 0 or ones == 0:
            continue
        else:
            if ones % 2 == 1:
                for j in range(len(ans[0])):
                    r = 0
                    if ones != 0:
                        ans[i][j] = 1
                        ones -= 1
                        r = 1
                    if ones > 0:
                        t1 = m - j - 1
                        ans[i][t1] = 1
                        ones -= 1
                    else:
                        if r == 1:
                            continue
                        if j <= t1:
                            ans[i][j] = 0
                        else:
                            break
m = int(input())
arr = []
dup = []
for _ in range(m):
    dup = [t for t in input().split()]
    arr.append(dup.copy())
ans = arr.copy()
dic = {}
matrix(arr, m, ans)
for i in range(len(ans)):
    for j in range(len(ans[0]) - 1):
        print(ans[i][j], end=" ")
    print(ans[i][-1])
by Expert (14,170 points)
edited by

Get best answers to any doubt/query/question related to programming , jobs, gate, internships and tech-companies. Feel free to ask a question and you will receive the best advice/suggestion related to anything you ask about software-engineering , development and programming problems .