Given a string, and number of rows, write the string in zigzag pattern.
Problem Explanation: Given a string “prodevelopertutorial” and number of rows is 3. Write the string in a zigzag pattern. Example: Input: string = “prodevelopertutorial”. Number of rows 3 Output: Visualization of writing elements in zigzag fashion: Visualization of writing output in the array: Solution Explanation: The zigzag pattern of the array elements by taking the […]
Given an integer value, convert it into roman number.
Problem Explanation: Roman numbers are represented by below characters: I 1 V 5 X 10 L 50 C 100 D 500 M 1000 IV 4 IX 9 XL 40 XC 100 CD 400 CM 900 Example: Input: 3 Output: "III" Input: 58 Output: "LVIII" Problem Solution: This problem can be solved in many ways. […]
Given an array, find all the repeated elements in C language.
Problem Description: Given a positive integer array, find all the elements that have been repeated and display them. Example: Input: {1, 3, 2, 7, 5, 1, 3} Output: 1, 3 Solution: The solution is very simple. We traverse through the array and make the element in that index as negative. This means the element is […]
Given an unsorted integer array, find the smallest missing positive integer.
Note: Find the element in the order O(n) with a constant extra space. Example 1: Input: [ 2, 3, -1, -2] Output: 1 Example 2: Input: [5, 6, 7, 8, 9] Output: 1 Solution Explanation: The solution is actually very simple. And we know that an integer starts with 1 to n not zero. Hence […]
Given an unsorted array, and a key. Find 2 elements such that the difference between the elements is equal to the key.
Example: {4, 2, 5, 8, 21, 34, 10} key = 24 Output: Pair found (34, 10). This problem can be solved in 2 ways. Solution 1: Brute force method. Explanation: Step 1: Take 2 loops, outer loop and an inner loop. Step 2: The element from outer loop will pick every other element in the […]
Given an unsorted array, find the least difference between the element pairs. Display all the pairs present.
Problem Statement: Given an unsorted array, list all the pairs with a least difference. Example: Input: {5, 4, 3, 2, 1} Output: {1, 2}, {2, 3}, {3, 4}, {4, 5}. Solution Explanation: Step 1: Sort the array. Step 2: Find the lest difference. Step 3: Find all the pairs with that least difference. First we […]
Given an unsorted array, find the minimum difference between 2 elements.
Problem Explanation: Given an unsorted array, the output should be the minimum difference between the elements and the elements itself. Example: {6, 10, 5, 42, 43, 1, 2} output: The minimum difference between is 1 the elements are 1 and 2. This can be solved in 2 ways: Solution 1: Take a “diff” variable that […]
Given an array, the difference between the elements is one, find if the “key” element is present or not.
Example: Array: {5, 6, 7, 6, 5, 4, 3, 4, 5, 6} Key: 4 Output: The element 4 is found at the index 6. Explanation: We can solve this approach by travelling the matrix element one by one and searching for the element. But we can have an optimized solution, as we know that the […]
Sort an array of 0s, 1s and 2s in C
Problem Description: Given an array of elements consisting of 0, 1, 2. Sort the array. Example: {1, 2, 0, 0, 1, 2, 1} Output: {0, 0, 1, 1, 1, 2, 2} Solution explanation: The solution is very simple, we count the number of 0’s, 1’s and 2’s. Then we put those many 0’s, 1’s and […]
Count the number of ways a baby can reach the n’th stair taking 1 or 2 steps at a time in C language.
Problem Description: A baby wants to climb the stairs. It can take single step at a time or take 2 steps at a time. Or it can take combination of 1 and 2 steps at a time. Given that there are N stairs, find the number of ways the baby can reach the top of […]