Hi! Here We Will dicuss about various programming challenges using C++ Language
Hey there!!! Let's begin our journey with a very simple problem. You are given an array and you need to sort it. Try yourself. I will be posting different approaches to tackle it.
Well there, sorting is an important tool to solve many problems. You will never be asked to sort an array in any competitive programming contest but sometimes you might have to sort an array as per requirements. So , you need to master the art of sorting an array correctly and quickly.
Some of the popular sorting algorithms are:
1) Bubble sort - https://www.geeksforgeeks.org/bubble-sort
2) Selection sort - https://www.geeksforgeeks.org/selection-sort
3) Insertion sort - https://www.geeksforgeeks.org/insertion-sort
4) Quick Sort - https://www.geeksforgeeks.org/quick-sort
5)Merge sort - https://www.geeksforgeeks.org/merge-sort
6)Count sort - https://www.geeksforgeeks.org/counting-sort
Of these sortng algorithms, Quick sort and Merge sort is more used as time taken by these algorithm is quite less than compared to bubble sort, insertion sort or selection sort. So it is recommended to use one of the Merge or Quick sort.
BUT WAIT!!!!! You surely don't want to write such a huge code during an ongoing competition. There is an easy way...........
Use the in-built sorting function of C++. It's just the matter of a line now!!!!
The prototype of the function is:
sort(start address, end address)
Eg: Suppose you have to sort an array A[n]. Then you just have to simply write
sort(A , A+n ) ;
and there you go you have a sorted array with very few efforts.
Remember to include "bits/stdc++.h" header file at the begining of you code.
I will be posting more on this sorting function as soon as possible.
AND HAA....here is the solution of the problem given:
https://www.codechef.com/viewsolution/23726104
Try to submit by yourself!!!!!!!!!
Some of the popular sorting algorithms are:
1) Bubble sort - https://www.geeksforgeeks.org/bubble-sort
2) Selection sort - https://www.geeksforgeeks.org/selection-sort
3) Insertion sort - https://www.geeksforgeeks.org/insertion-sort
4) Quick Sort - https://www.geeksforgeeks.org/quick-sort
5)Merge sort - https://www.geeksforgeeks.org/merge-sort
6)Count sort - https://www.geeksforgeeks.org/counting-sort
Of these sortng algorithms, Quick sort and Merge sort is more used as time taken by these algorithm is quite less than compared to bubble sort, insertion sort or selection sort. So it is recommended to use one of the Merge or Quick sort.
BUT WAIT!!!!! You surely don't want to write such a huge code during an ongoing competition. There is an easy way...........
Use the in-built sorting function of C++. It's just the matter of a line now!!!!
The prototype of the function is:
sort(start address, end address)
Eg: Suppose you have to sort an array A[n]. Then you just have to simply write
sort(A , A+n ) ;
and there you go you have a sorted array with very few efforts.
Remember to include "bits/stdc++.h" header file at the begining of you code.
I will be posting more on this sorting function as soon as possible.
AND HAA....here is the solution of the problem given:
https://www.codechef.com/viewsolution/23726104
Try to submit by yourself!!!!!!!!!
GeeksforGeeks
Bubble Sort Algorithm - GeeksforGeeks
Your All-in-One Learning Portal: GeeksforGeeks is a comprehensive educational platform that empowers learners across domains-spanning computer science and programming, school education, upskilling, commerce, software tools, competitive exams, and more.
We have seen that the sort function of c++ sorts the array in increasing order by default, but what if we want to sort the array in decreasing order? We can simply do it by just modifying the function call statement. We just need to give an extra parameter during calling.
The syntax of the sort function will be:
sort (arr , arr+n , greater<int>());
A program which sorts an array in decreasing order is given here - https://pastebin.com/9zWH71Mz
The syntax of the sort function will be:
sort (arr , arr+n , greater<int>());
A program which sorts an array in decreasing order is given here - https://pastebin.com/9zWH71Mz
Pastebin
Sort in Descding order - Pastebin.com
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
Further more, we can use this sorting function to sort a structure according to one of its member element.
Here is an example of that - https://pastebin.com/LzYqHBVJ
In this example we use function named "func" to compare two elements. Basically the sort function passes two elements to the func function and if it gets 1 in return then first element is placed before the second element. And if 0 is returned then second element is placed before the first element. In this way we can sort a structure according to our need.
Here is an example of that - https://pastebin.com/LzYqHBVJ
In this example we use function named "func" to compare two elements. Basically the sort function passes two elements to the func function and if it gets 1 in return then first element is placed before the second element. And if 0 is returned then second element is placed before the first element. In this way we can sort a structure according to our need.
Pastebin
Sort Structure - Pastebin.com
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
A prime number is a whole number greater than 1, which is only divisible by 1 and itself. First few prime numbers are : 2 3 5 7 11 13 17 19 23 β¦..
In many programming problems we have to check prime numbers efficiently. One of efficeient algorithms to find prime numbers is Seive of Eratosthenes. Check it out at GEEKS FOR GEEKS.
https://www.geeksforgeeks.org/sieve-of-eratosthenes/
For any query you can ask me
@saranyanaharoy
Thanks for joining our channel. πππππ
In many programming problems we have to check prime numbers efficiently. One of efficeient algorithms to find prime numbers is Seive of Eratosthenes. Check it out at GEEKS FOR GEEKS.
https://www.geeksforgeeks.org/sieve-of-eratosthenes/
For any query you can ask me
@saranyanaharoy
Thanks for joining our channel. πππππ
GeeksforGeeks
Sieve of Eratosthenes - GeeksforGeeks
Your All-in-One Learning Portal: GeeksforGeeks is a comprehensive educational platform that empowers learners across domains-spanning computer science and programming, school education, upskilling, commerce, software tools, competitive exams, and more.
Hello coding enthusiasts !!!!! This time we are upto something very interesting. It's PREFIX ARRAY!!!!! Prefix array is an important tool in competitive programming and it is something which every beginners should know. It comes under the category of basic dynamic programming and it reduces the repeated calculation process. Here is an excellent post on prefix array which will definitely help you to grab it in an easy way -
https://codeforces.com/blog/rash42
Though you may not get a direct problem which will ask you to create a prefix array, but sometimes you will have to use it in some other problem to reduce repeated calculations. So make yourself prepared for it.
Happy coding!!!
https://codeforces.com/blog/rash42
Though you may not get a direct problem which will ask you to create a prefix array, but sometimes you will have to use it in some other problem to reduce repeated calculations. So make yourself prepared for it.
Happy coding!!!
Codeforces
Blog entries - Codeforces
Codeforces. Programming competitions and contests, programming community
Well, many times you have uploaded a code and got different errors. Well it's ok.... it's a part of the learning process. Today I am going to discuss different types of errors we generally face with online judges.
1. Runtime Error- This mainly occurs if you try to access a memory location which is not allocated. For example if you initialize an array arr[5] and try to access arr[10] then you will get RE. You should always check whether the array location you are going to access is valid or not.
Even accessing a memory location suh as: arr[-1] will also give a RE. So also check the lower bound while accessing the array elements in reverse order.
2. Wrong Answer- The output of your code doesnot match with the correct answer. Well mostly it is the corner cases in which the code fails, so try to make some corner test cases by yourself and see whether your code passes it. There may be a possible buffer overflow so use long long int instead of int and watch carefully the given constraints.
3. Time Limit Exceeded (TLE)- Well nothing much to do in this one except brainstorming . You need to think of another algorithm to make it work.
4. Memory Limit Exceeded- This happens when you try to use more memory than the allotted memory. Probably you are declaring too big 2D array!!! Try to use different containers or data structure but it would be far better if you think of some different Algo.
You can get some silly errors which coders ofte make and how to handle it wisely in this article -
https://codeforces.com/blog/entry/67417
Happy Coding!!!
1. Runtime Error- This mainly occurs if you try to access a memory location which is not allocated. For example if you initialize an array arr[5] and try to access arr[10] then you will get RE. You should always check whether the array location you are going to access is valid or not.
Even accessing a memory location suh as: arr[-1] will also give a RE. So also check the lower bound while accessing the array elements in reverse order.
2. Wrong Answer- The output of your code doesnot match with the correct answer. Well mostly it is the corner cases in which the code fails, so try to make some corner test cases by yourself and see whether your code passes it. There may be a possible buffer overflow so use long long int instead of int and watch carefully the given constraints.
3. Time Limit Exceeded (TLE)- Well nothing much to do in this one except brainstorming . You need to think of another algorithm to make it work.
4. Memory Limit Exceeded- This happens when you try to use more memory than the allotted memory. Probably you are declaring too big 2D array!!! Try to use different containers or data structure but it would be far better if you think of some different Algo.
You can get some silly errors which coders ofte make and how to handle it wisely in this article -
https://codeforces.com/blog/entry/67417
Happy Coding!!!
Codeforces
Codeforces. Programming competitions and contests, programming community
Every C/C++ program is said to have a main() function, but have you ever wondered is it possible to write a C/C++ program without main() function??
It is possible to write a C/C++ program without a main() function. Check out how -https://qr.ae/TWGwGa
It is possible to write a C/C++ program without a main() function. Check out how -https://qr.ae/TWGwGa
Quora
Can a C program be written without main ()?
Answer (1 of 36): The main() funtion is commonly used as your entry/exit point when writing a console program, but:
Inside a UI framework:
If you are writing an app using a UI framework, you will probably have some sort of message/event handler templateβ¦
Inside a UI framework:
If you are writing an app using a UI framework, you will probably have some sort of message/event handler templateβ¦
Hello Everyone!!!
It is time to face some interesting problems.
Are you excitedππππ
Our 1st problem is from codechef
PROBLEM LINK:
ONE KING
DIFFICULTY:
EASY-MEDIUM
PROBLEM:
Given N (β€100000) intervals [Ai , Bi], one such interval can be deleted by placing a bomb at x if Ai β€ x β€ Bi. Find minimum number of bombs required to delete all intervals.
SOLUTION:
EXPLANATION
CODE:
code
NOTE: Above idea is implemented using C++. Some C++ template classes from STL(Standard Template Library) are used. (STL). Specifically vector and pair are used in the solution code.
vector
pair
sorting vector of pairs
#oneking #codechef
It is time to face some interesting problems.
Are you excitedππππ
Our 1st problem is from codechef
PROBLEM LINK:
ONE KING
DIFFICULTY:
EASY-MEDIUM
PROBLEM:
Given N (β€100000) intervals [Ai , Bi], one such interval can be deleted by placing a bomb at x if Ai β€ x β€ Bi. Find minimum number of bombs required to delete all intervals.
SOLUTION:
EXPLANATION
CODE:
code
NOTE: Above idea is implemented using C++. Some C++ template classes from STL(Standard Template Library) are used. (STL). Specifically vector and pair are used in the solution code.
vector
pair
sorting vector of pairs
#oneking #codechef
Codechef
One Dimensional Kingdoms Practice Coding Problem
Improve your coding skills with our One Dimensional Kingdoms practice problem! Challenge yourself and solve One Dimensional Kingdoms practical programming coding exercises.