Skip to main content

Greedy approach to Solve Array/Lists Problems having large number of elements.


Greedy  Approach to Solve Array problems

Are you tired of using two loops to check individual elements ?
If yes, then try this Greedy Approach to solve these kind of problems.

For e.g., 

1) We have two arrays as given below:



2) First, sort the arrays in descending order:

3) Now, check the conditions in a single for loop.
Check below for full greedy approach (PYTHON CODE SNIPPET) :
count,j=0,0
            for j in range(N):
                if A[count]>B[j]:  
                    count+=1
            print(count)


Comment below if it works!!!
Thank You

Comments

Post a Comment

Popular posts from this blog

How to fix/solve Presentation Error

What is “ Presentation Error ”? As you start participating in coding competitions, you are fully acknowledged with most of the errors which generally occurs during the program. What to do if you’re facing an error that doesn’t usually occurs i.e., a special type of error known as “ Presentation Error ”. These errors may occurs due to the following factors: 1)   If there is wrong formatting from your side, the answer seems to be correct but the online judge doesn’t identifies it. 2)   If an output results to an integer but you’re   presenting it in the form of string, then it may be a serious issue. For e.g., the program OUTPUT should be 15.76 ASSUME THAT WE HAVE THE FOLLOWING JAVA CODE SNIPPET : . . String ans="15.76"; System.out.println(ans);        //gives a presentation error . . float correct_form=Float.valueOf(ans); System.out.printf("%.2f",...

How to fix/solve Compilation Error in Python

A Quick Overview on Compilation Error A Compilation Error is a special type of error that occurs while using Python as Source Code. The main reason behind this error is that python is an interpreted language. So, the online judge rather than interpreting it compiles the code and hence the error occurs. The best approach to tackle these kind of problems is either use a compilation command or convert the program in single line using the single function containing the whole program For.e.g., Program to convert a character in LowerCase at a specified place. def mains():                    //Driver Function     n,K=input().split()     n=int(n)     K=int(K)     S=input()     print(S[:(K-1)]+chr(ord(S[K-1])+32)+S[(K):]) mains() Input: 4 3 TEAM Output: TEaM Hope this articles ...

Technical Knowledge

A  feature phone  is a  mobile phone  that retains the  form factor  of earlier-generation phones, with button-based input and a small display. Feature phones are sometimes called  dumbphones  in contrast with touch-input  smartphones . [1]  They tend to use an  embedded operating system  with a small and simple  graphical user interface , unlike large and complex general-purpose  mobile operating systems  like  Android  or  iOS . Feature phones typically provide  voice calling  and  text messaging  functionality as well as basic  multimedia  and  Internet  capabilities and other services offered by the user's  wireless service provider . Feature phones have a backlit  LCD  screen, a hardware  notification LED , and  micro USB  port and have a physical keyboard, a microphone,  SD card  slot, a rear-facing camera to...