c program to clear nth bit of the number || Bitwise Operators || Problem Solving ||

#include <stdio.h> int main() {     int num, n, newNum;     printf("Enter any number: ");     scanf("%d", &num);     printf("Enter nth bit to clear (0-31): ");     scanf("%d", &n);     newNum = num & (~(1 << n));     printf("Number before clearing %d bit: %d (in decimal)\n", n, num);     printf("Number after clearing %d bit: %d (in decimal)\n", n, newNum);     return 0; } Logic :-  num&(~(1<<n));

Row with maximum ones || Leetcode Solution || Simple Approach || 🔥🔥🔥💯💯💯✅✅✅||

             

                                2643. Row With Maximum Ones


Solution :-

Solving this problem using dictionary concept:

Approach Of solving the problem : 

  1. Create a empty dictionary d = {} , initialise count = 0 , lst = []
  2. Iterate over the mat list, now add dictionary for its index and count
  3. from the dictionary find the maximum value and store it in v_max variable
  4. Now iterate over the dictionary using .items() method
  5. Check condition if(value == v_max) append the key and value to the list and terminate the loop
  6. now, return the list

Complexity

  • Time complexity: 
O(n)

    Since we used for loops 

Code in Python:

class Solution:
    def rowAndMaximumOnes(self, mat: List[List[int]]) -> List[int]:
        count = 0 
        d = {}
        for i in mat:
            d[count] = i.count(1)
            count += 1 
        v_list = d.values()
        v_max = max(v_list)
        lst = []
        for key,value in d.items():
            if(value == v_max):
                lst.append(key)
                lst.append(value)
                break 
        return lst 

Comments

Popular posts from this blog

Merge Strings Alternatively || Leetcode Solution || Easy Approach || Simple || ✅✅✅✅💯💯💯💯🔥🔥🔥🔥

HOW TO PREPARE FOR JEE MAINS IN HOME EFFECTIVELY

1978. Employees Whose Manager Left the Company || Leetcode SQL solution || simple sql query || 🔥🔥🔥💯💯💯✅✅✅