All about Gadgets , Electronics and Coding

Wednesday, July 27, 2022

Coding Ninja || Flip Bits C++ Solution

 


Problem Statement

    Level: easy
You are given an array of integers ARR[] of size N consisting of zeros and ones. You have to select a subset and flip bits of that subset. You must return the count of maximum ones you can obtain by flipping chosen sub-array at most once.

A flip operation is one in which you turn 1 into 0 and 0 into 1

Approach:

  •  The Approach is simple. It's just another example of Kadence's algorithm. Let me show you how it is:
  • We will first create some variables;

  1.  maxCount - This will store the maximum continues zeros.
  2.  Count -  We will increase this count every time we get a zero. and if we get a '1' we will save this count to maxCount and make it zero.
  3.  maxOne; - thsi wiill store the count of the 1 that are already present.

  • Run a for loop for arr.Size elements, and if the value is zero, iterate the count by 1.
  • if it is not zero(i.e., 1), then decrease the count by 1. and increase the maxOne count by 1. 
  • If the count goes negative (i.e. count < 0), then make the count zero. ( 💡 You can recall the kadanes' algorithm here).
  • Finally, return the sum of the maximum zero subarrays (i..e. maxCount) and the count of the maxOne,

CODE:-


int flipBits(int* arr, int n) { // WRITE YOUR CODE HERE int count=0, maxcount=0, x=0; for(int i=0; imaxcount) maxcount=count; if(count<0) count=0; } return maxcount+x; }


Time Complexity:- O(n)

Space Complexity:- O(1) 

   that
Ti

Share:

Monday, July 25, 2022

Coding Ninja || Maximum Subarray Sum C++ Solution

 




Problem Statement

    Level: easy
You are given an array (ARR) of length N, consisting of integers. You have to find the sum of the subarray (including empty subarray) having maximum sum among all subarrays.
A subarray is a contiguous segment of an array. In other words, a subarray can be formed by removing 0 or more integers from the beginning, and 0 or more integers from the end of an array.


Approach:

A simple approach that comes to mind if you know is the Kadane’s algorithm.
Kadane's algorithm works on the idea of maintaining subarray sum at every iteration it becomes greater than the previous iteration. If the current Sum becomes negative than it is set to 0.

CODE:- 

#include long long maxSubarraySum(int arr[], int n) { long long currSum = 0; // Next we will be creating the maxSum element which will store the final // result of the code long long maxSum = INT_MIN; for(int i = 0; i < n; i++){ // running the for loop and adding up the value of the array elements currSum = currSum + arr[i]; // Here comes the twist of kadane's algorithm // If the cuurentSum becomes less than 0 then set it to 0; if(currSum < 0){ currSum = 0; } // Update the maxSum to the maximum of the previous iteration maxSum // and the cuurentSum in the current iteration maxSum = max(currSum, maxSum); } // finally returning the maxSum of trhe contigious sub array return maxSum; }


Time Complexity :- O(n)

Space Complexity :- O(1) 

   tthrth
Ti
Share:

Friday, July 15, 2022

Best gadgets to buy under ₹ 500 online



    With innovations reaching the skies, unique products have become readily available online over Amazon and Flipkart. These products have become a must-try out with the motive of fun or usefulness and are available at a reasonable price. Here I bring you some of the fascinating gadgets I have found and hope you like Them.

* the buying links are given below the discription of every product and
* Prices may very

1) Portable Small Humidifier Mini Spray Fan





Summers are becoming harsher and harsher every year, and you always want a water spray that can spray cold mist on your face and a portable fan. this unique gadget is a small humidifier with a built-in fan 




2) SMART MUSIC BULB with built-in bluetooth speaker 


This multicolour led light has a built-in speaker that you can connect via Bluetooth very useful if you want an instant party mood !! At your place. You can change the colour using the remote given.




3) 2 in 1 Metal Type C and Micro OTG Male to USB A 3.0





This converter is handy if you constantly transfer data from your mobile to your Pendrive and other USB devices.
Amazon : 198 
Flipkart: 199


4) zebronics Zeb-County Wireless Bluetooth Portable Speaker





Sold for sightly over 500 is a very affordable Bluetooth speaker from a trusted brand of Zebronics with decent sound quality and battery life.


Amazon : 549
Flipkart: Not available


5) Portronics Portable RuffPad Re-Writeable 21.59Cm (8.5-inch) 



For your children for their drawing practice or just their rough work, this Portronics re-writeable pad is a beneficial product. Even adults who are 


Amazon : 399
Amazon: 629 ( multicolor display )
Flipkart : 420

Share:

Monday, July 11, 2022

Try! this Vs code extensions for HTML/CSS

 Vs code is by far the best IDE out there, and one thing that makes coders love Vs code is the availability of Extensions. 👀

Using fancy extensions has become a new era in the world of coders. Many companies are reaching out with Artificial intelligence to improve the efficiency of writing code.

You can add extensions in Vs code like this:





If you are writing in HTML / CSS, here are some of the beneficial extensions that I have tried and want you to give a Try!!


Useful Vs Code extensions if you write HTML/CSS code

This is the most useful extension I have used while coding in HTML/CSS. If you are tired of switching to an HTML file for coping the class name and then pasting it in the CSS. this extension helps in showing the class name in the HTML file as the suggestions and is super easy to use.


After using the extension:

source: HTML to CSS autocompletion




Prettier is a code formatter with strong opinions. It enforces a consistent style by parsing your code and re-printing it with its own rules that take into account the maximum line length, wrapping code when necessary.


After using prettier:-





Share:

Search This Blog

Blog Archive

Powered by Blogger.

Coding Ninja || Flip Bits C++ Solution

Blog Archive

Recent Posts

Unordered List

Pages

Theme Support