GeeksforGeeks
17.4K subscribers
1.83K photos
34 videos
24 files
2.76K links
Official Hub βœ…

Your coding haven – where tech meets simplicity.

Discover:
πŸ˜‚ Coding Laughter
πŸ“˜ Curated Study Material
πŸ’Ž Exclusive Offers

Utilize every service we provide and make a promise to find better opportunities for yourself! #gfgkarlohojayega!
Download Telegram
Must Do DSA Topics day 19
Hashing

Print a Binary Tree in Vertical Order : Given a binary tree, print it vertically. The following example illustrates the vertical order traversal.

Learn more here : https://bit.ly/3Uc4oeE

Find whether an array is subset of another array : Given two arrays: arr1[0..m-1] and arr2[0..n-1]. Find whether arr2[] is a subset of arr1[] or not. Both the arrays are not in sorted order. It may be assumed that elements in both arrays are distinct.

Examples:
Input: arr1[] = {11, 1, 13, 21, 3, 7},
arr2[] = {11, 3, 7, 1}

Output: arr2[] is a subset of arr1[]
Learn more here : https://bit.ly/3UyW3SP

Union and Intersection of two linked lists: Given two Linked Lists, create union and intersection lists that contain union and intersection of the elements present in the given lists. Order of elements in output lists doesn’t matter.
Learn more : https://bit.ly/3NprUD1
πŸ‘1
Problem Of The Day
"Array Removals"

Solve the problem to win points

Given an array arr[] of size N and an integer K. The task is to find the minimum number of elements that should be removed, such that Amax-Amin<=K. After the removal of elements, Amax and Amin is considered among the remaining elements.

Note: Can you solve the probelm without using any extra space and O(N*log(N)) time complexity?

Example 1:

Input: N = 9, K = 4
arr[] = {1,3,4,9,10,11,12,17,20}
Output: 5
Explanation: Remove 1, 3, 4 from beginning
and 17, 20 from the end.
Example 2:

Input: N = 5, K = 2
arr[] = {1, 5, 6, 2, 8}
Output: 3
Explanation: There are multiple ways to
remove elements in this case.
One among them is to remove 5, 6, 8.
The other is to remove 1, 2, 5
Your Task:
You don't need to read input or print anything. Your task is to complete the function removals() which takes the array of integers arr, n and k as parameters and returns an integer, denotes minimum number of elements should be remove to satisfy the condition.

Expected Time Complexity: O(N^2)
Expected Auxiliary Space: O(N^2)

Solve the problem and submit your answers here: https://practice.geeksforgeeks.org/problems/array-removals/1
Must Do DSA Topics day 20
Hashing

Check if a pair exists with given sum in given array : Given an array A[] of n numbers and another number x, the task is to check whether or not there exist two elements in A[] whose sum is exactly x.
Examples:
Input: arr[] = {0, -1, 2, -3, 1}, x= -2
Output: Yes

Learn more : https://bit.ly/3DvTdag

Minimum delete operations to make all elements of array same : Given an array of n elements such that elements may repeat. We can delete any number of elements from the array. The task is to find a minimum number of elements to be deleted from the array to make it equal.
Learn more :

https://bit.ly/3UeGj7m

Minimum operation to make all elements equal in array: Given an array with n positive integers. We need to find the minimum number of operation to make all elements equal. We can perform addition, multiplication, subtraction or division with any element on an array element.

Learn more : https://bit.ly/3fu1Uts
Must Do DSA Topics day 21
Hashing

Maximum distance between two occurrences of same element in array : Given an array with repeated elements, the task is to find the maximum distance between two occurrences of an element.
Input : arr[] = {3, 2, 1, 2, 1, 4, 5, 8, 6, 7, 4, 2}
Output: 10
// maximum distance for 2 is 11-1 = 10
// maximum distance for 1 is 4-2 = 2
// maximum distance for 4 is 10-5 = 5

Learn more : https://bit.ly/3gVEnSz

Count maximum points on same line : Given N point on a 2D plane as pair of (x, y) co-ordinates, we need to find maximum number of point which lie on the same line.
Learn more : https://bit.ly/3WlCL4Z

Check if a given array contains duplicate elements within k distance from each other : Given an unsorted array that may contain duplicates. Also given a number k which is smaller than size of array. Write a function that returns true if array contains duplicates within k distance.
Learn more : https://bit.ly/3h6pvAW
πŸ‘1
Problem Of The Day
"Base Equivalence"

Solve the problem to win points

Given a number (n) and no. of digits (m) to represent the number, we have to check if we can represent n using exactly m digits in any base from 2 to 32.

Example 1:

Input: n = 8, m = 2
Output: Yes
Explanation: Possible in base 3 as 8 in base 3 is 22.

Example 2:

Input: n = 8, m = 3
Output: No
Explanation: Not possible in any base.

Your Task:
You dont need to read input or print anything. Complete the function baseEquiv() which takes n and m as input parameter and returns "Yes" if its possible to represent the number else "No" without quotes..

Expected Time Complexity: O(logN).
Expected Auxiliary Space: O(1)

Solve the problem and submit your answers here : https://practice.geeksforgeeks.org/problems/base-equivalence1022/1
πŸ‘4
Problem Of The Day
"Grouping Of Numbers"

Solve the problem to win points

One day Jim came across array arr[] of N numbers. He decided to divide these N numbers into different groups. Each group contains numbers in which sum of any two numbers should not be divisible by an integer K. Print the size of the group containing maximum numbers.



Example 1:

Input:
N = 4, K = 8
arr[] = {1, 7, 2, 6}
Output:
2
Explanation:
The group of numbers which can be formed
are: (1),(2),(7),(6),(1,2),(1,6),(7,2),(7,6).
So,the maximum possible size of the group is 2.

Your Task:
You don't need to read input or print anything. Your task is to complete the function maxGroupSize() which takes 2 Integers N, and K and also an array arr[] of N integers as input and returns the maximum group size possible.

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(K)

Solve the problem and submit your answers here: https://practice.geeksforgeeks.org/problems/grouping-of-numbers0015/1
Must Do DSA Topics day 22
Strings

Function to copy string (Iterative and Recursive) : Given two strings, copy one string to other using recursion. We basically need to write our own recursive version of strcpy in C/C++
Examples: Input : s1 = "hello"
s2 = "geeksforgeeks"
Output : s2 = "hello"

Input : s1 = "geeksforgeeks"
s2 = ""
Output : s2 = "geeksforgeeks"
Learn more : https://bit.ly/3FMU6O8

Check if given String is Pangram or not : Given a string Str. The task is to check if it is Pangram or not.
Examples:
Input: β€œThe quick brown fox jumps over the lazy dog”
Output: is a Pangram
Explanation: Contains all the characters from β€˜a’ to β€˜z’]
Learn more : https://bit.ly/3T6NvRR

Missing characters to make a string Pangram : Pangram is a sentence containing every letter in the English alphabet. Given a string, find all characters that are missing from the string. We need to print output in alphabetic order.
Learn more : https://bit.ly/3heToyV
πŸ‘7
Problem Of The Day
"Two numbers with odd occurrences"

Solve the problem to win points

Given an unsorted array, Arr[] of size N and that contains even number of occurrences for all numbers except two numbers. Find the two numbers in decreasing order which has odd occurrences.

Example 1:

Input:
N = 8
Arr = {4, 2, 4, 5, 2, 3, 3, 1}
Output: {5, 1}
Explaination: 5 and 1 have odd occurrences.

Your Task:
You don't need to read input or print anything. Your task is to complete the function twoOddNum() which takes the array Arr[] and its size N as input parameters and returns the two numbers in decreasing order which have odd occurrences.


Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)

Solve the problem and submit your answers here: https://practice.geeksforgeeks.org/problems/two-numbers-with-odd-occurrences5846/1
πŸ‘4πŸ”₯1
Problem Of The Day
"Minimum sum partition"

Solve the problem to win points

Given an array arr of size n containing non-negative integers, the task is to divide it into two sets S1 and S2 such that the absolute difference between their sums is minimum and find the minimum difference


Example 1:

Input: N = 4, arr[] = {1, 6, 11, 5}
Output: 1
Explanation:
Subset1 = {1, 5, 6}, sum of Subset1 = 12
Subset2 = {11}, sum of Subset2 = 11

Your Task:
You don't need to read input or print anything. Complete the function minDifference() which takes N and array arr as input parameters and returns the integer value


Expected Time Complexity: O(N*|sum of array elements|)
Expected Auxiliary Space: O(N*|sum of array elements|)

Solve the problem and submit your answers here: https://practice.geeksforgeeks.org/problems/minimum-sum-partition3317/1
πŸ‘3πŸ‘2πŸ”₯1
Problem Of The Day
"Jumping Numbers"

Solve the problem to win points

Given a positive number X. Find the largest Jumping Number which is smaller than or equal to X.
Jumping Number: A number is called Jumping Number if all adjacent digits in it differ by only 1. All single-digit numbers are considered as Jumping Numbers. For example 7, 8987 and 4343456 are Jumping numbers but 796, 677 and 89098 are not.



Example 1:

Input:
X = 10
Output:
10
Explanation:
10 is the largest Jumping Number
possible for X = 10.

Your Task:
You don't need to read input or print anything. Your task is to complete the function jumpingNums() which takes an Integer X as input and returns the largest Jumping Number less than or equal to X.



Expected Time Complexity: O(k), where k is no of jumping numbers
Expected Auxiliary Space: O(k), where k is no of jumping numbers

Solve the problem and submit your answers here: https://practice.geeksforgeeks.org/problems/jumping-numbers3805/1
Must Do DSA Topics day 23
Strings

Check if a string is Pangrammatic Lipogram : A pangrammatic lipogram is a text that uses every letter of the alphabet except one. For example, β€œThe quick brown fox jumped over the lazy dog” omits the letter S, which the usual pangram includes by using the word jumps.
Given a string, our task is to check whether this string is a pangrammatic lipogram or not?
Learn more : http://bit.ly/3EjrGdy

Removing punctuations from a given string : Given a string, remove the punctuation from the string if the given character is a punctuation character, as classified by the current C locale. The default C locale classifies these characters as punctuation:

! " # $ % & ' ( ) * + , - . /
Learn more : http://bit.ly/3G2lAzs

Rearrange characters in a String such that no two adjacent characters are same : Given a string with lowercase repeated characters, the task is to rearrange characters in a string so that no two adjacent characters are the same.
Learn more : http://bit.ly/3UrNZmU
πŸ‘2
Problem Of The Day
"Primes sum"

Solve the problem to win points

Given a number N. Find if it can be expressed as sum of two prime numbers.

Example 1:

Input: N = 34
Output: "Yes"
Explanation: 34 can be expressed as
sum of two prime numbers.

Your Task:
You dont need to read input or print anything. Complete the function isSumOfTwo() which takes N as input parameter and returns "Yes" if can be expressed as sum of two prime numbers. else return "No".

Expected Time Complexity: O(N*sqrt(N))
Expected Auxiliary Space: O(1)

Solve the problem and submit your answers here : https://practice.geeksforgeeks.org/problems/primes-sum5827/1
πŸ‘1😁1
Problem Of The Day
"Maximum Sub-String after at most K changes"

Solve the problem to win points

We have a string s of length n, which contains only UPPERCASE characters and we have a number k (always less than n). We can make at most k changes in our string. In one change, you can replace any s[i] (0<= i < n) with any uppercase character (from 'A' to 'Z'). After k changes, find the maximum possible length of the sub-string which have all same characters.

Example 1:

Input: s = "ABAB", k = 2
Output: 4
Explanation: Change 2 'B' into 'A'.
Now s = "AAAA"

Your Task:
You don't need to read or print anything. Your task is to complete the function characterReplacement() which takes s and k as input parameters and returns the maximum length of sub-string after doing k changes.


Expected Time Complexity: O(n)
Expected Space Complexity: O(26)

Solve the problem and submit your answers here: https://practice.geeksforgeeks.org/problems/maximum-sub-string-after-at-most-k-changes3220/1
πŸ‘3❀1πŸ‘Ž1
Problem Of The Day
"Find patterns"

Solve the problem to win points

Given two strings S and W. Find the number of times W appears as a subsequence of string S where every character of string S can be included in forming at most one subsequence.

Example:
Input:
S = "abcdrtbwerrcokokokd"
W = "bcd"
Output:
2
Explanation:
The two subsequences of string W are
{ S1 , S2 , S3 } and { S6 , S11 , S18 }
(Assuming 0- based indexing).

Your Task:
You don't need to read input or print anything. Your task is to complete the function numberOfSubsequences() which takes the string S and string W as input parameters and returns the number of subsequences of string W in string S.

Expected Time Complexity: O(N2)
Expected Auxiliary Space: O(N)

Solve the problem and submit your answers here : https://practice.geeksforgeeks.org/problems/find-patterns0606/1
Must Do DSA Topics day 24
Strings

Array of Strings in C++ (3 Different Ways to Create) : In C++, a string is usually just an array of (or a reference/points to) characters that ends with the NULL character β€˜\0β€˜. A string is a 1-dimensional array of characters and an array of strings is a 2-dimensional array of characters.
Learn more : http://bit.ly/3O7ZY74

Strings in C : String in C programming is a sequence of characters terminated with a null character β€˜\0’. Strings are defined as an array of characters. The difference between a character array and a string is the string is terminated with a unique character β€˜\0’.
Learn more : http://bit.ly/3URZJPE

Storage for Strings in C : In C, a string can be referred to either using a character pointer or as a character array.
Learn more : http://bit.ly/3TAm42X
Problem Of The Day
"Longest Perfect Piece"

Solve the problem to win points

Given an array arr[] of N Numbers. A Perfect Piece is defined as a subarray such that the difference between the minimum and the maximum value in that range is at most 1. Find the Maximal Length Perfect Piece.

Example :
Input:
N = 4
arr[] = {8, 8, 8, 8}
Output:
4
Explanation:
The longest subarray is [1, 4]
where maximum=8 and minimum = 8 and
difference is 0, which is less than 1.
Its length is 4.

Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPerfectPiece() which takes an Integer N and an array arr[] of length N as input and returns the maximal length Perfect Piece.

Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(N)

Solve the problem and submit your answers here: https://practice.geeksforgeeks.org/problems/close-to-perfection1525/1
πŸ‘1
Must Do DSA Topics day 25
Strings

sprintf() in C : sprintf stands for β€œString print”. Instead of printing on console, it store output on char buffer which are specified in sprintf.
Learn more : http://bit.ly/3UWNe5p

Program to find second most frequent character : Given a string, find the second most frequent character in it. Expected time complexity is O(n) where n is the length of the input string.
Learn more : http://bit.ly/3UzuJ7d

C Program to Sort an array of names or strings : Given an array of strings in which all characters are of the same case, write a C function to sort them alphabetically. The idea is to use qsort() in C and write a comparison function that uses strcmp() to compare two strings.
Learn more : http://bit.ly/3Ewcdan
πŸ‘4
Problem Of The Day
"Sum of Beauty of All Substrings"

Solve the problem to win points

Given a string S, return the sum of beauty of all its substrings.
The beauty of a string is defined as the difference in frequencies between the most frequent and least frequent characters.

For example, the beauty of string "aaac" is 3 - 1 = 2.

Example :
Input:
S = "aaac"
Output:
3

Explanation: The substrings with non - zero beauty are ["aaac","aac"] where beauty of "aaac" is 2 and beauty of "aac" is 1.

Your Task:
You don't need to read input or print anything. Your task is to complete the function beautySum() which takes string S as input paramters and returns the sum of beauty of all its substrings.

Expected Time Complexity: O(|S|2)
Expected Auxiliary Space: O(1)

Solve the problem and submit your answers here: https://practice.geeksforgeeks.org/problems/sum-of-beauty-of-all-substrings-1662962118/1
πŸ‘1
Must Do DSA Topics day 26
Strings

String class in Java : String is a sequence of characters. In java, objects of String are immutable which means a constant and cannot be changed once created.
Learn More : http://bit.ly/3hLbMjw

String in Switch Case in Java : The switch statement is a multi-way branch statement. It provides an easy way to dispatch execution to different parts of code based on the value of the expression. Basically, the expression can be a byte, short, char, and int primitive data types. Beginning with JDK7, it also works with enumerated types ( Enums in java), the String class, and Wrapper classes.
Learn More : http://bit.ly/3UZu92j

Java program to swap first and last characters of words in a sentence : Write a Java Program to Swap first and last character of words in a Sentence.
Examples:

Input : geeks for geeks
Output :seekg rof seekg
Learn More : http://bit.ly/3g7VoZT
πŸ‘1
Must Do DSA Topics day 27
Linked Lists

A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations.

What is Linked List : Like arrays, Linked List is a linear data structure. Unlike arrays, linked list elements are not stored at a contiguous location; the elements are linked using pointers. They include a series of connected nodes. Here, each node stores the data and the address of the next node.
Learn more : http://bit.ly/3TGDdrK

Linked List vs Array : Major differences in linked lists and arrays are size, memory allocation, memory efficiency, execution time, insertion and dependency.
Learn more : http://bit.ly/3Gl7UQs

Insertion in Linked List : We have introduced Linked Lists in the previous post. We also created a simple linked list with 3 nodes and discussed linked list traversal.
All programs discussed in this post consider the following representations of the linked list.
Learn more : http://bit.ly/3Gl80HO
πŸ‘1