Problem Solving:
Things to note / Instruction:
* You are expected to approach all Questions in Dart Programming Language.
Program:
A) Array Problem:
Given an array arr[] of non-negative integers and a value sum, the task is to
check if there is a subset of the given array whose sum is equal to the given
sum.
Examples:
Input: arr[] = {3, 34, 4, 12, 5, 2}, sum = 9
Output: True
Explanation: There is a subset (4, 5) with sum 9.
Input: arr[] = {3, 34, 4, 12, 5, 2}, sum = 30
Output: False
Explanation: There is no subset that add up to 30.
B) String Problem:
Write a program to give the following output for the given input
Eg 1: Input: a1b10 Output: abbbbbbbbbb
Eg: 2: Input: b3c6d15
Output: bbbccccccddddddddddddddd
The number varies from 1 to 99.
C) Permutation Problem:
Given a collection of numbers, nums, that might contain duplicates, return all
possible unique permutations in any order.
Example 1:
Input: nums = [1,1,2]
Output:
[[1,1,2],
[1,2,1],
[2,1,1]]
Example 2:
Input: nums = [1,2,3]
Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
D) Problem Statement:
Longest Subarray with Sum ≤ K
Given an array arr[] of size N containing positive integers and an integer K, find
the length of the longest contiguous subarray whose sum is less than or equal
to K.
Input:
An integer N (1 ≤ N ≤ 10⁵), the size of the array.
An integer array arr[] of size N, containing positive numbers.
An integer K (1 ≤ K ≤ 10⁹), the target sum.
Output:
Return an integer representing the length of the longest subarray whose sum
is ≤ K.
Example 1:
arr[] = {3, 1, 2, 1, 4, 5}
K=7
Output:4
Explanation:
The longest subarray with sum ≤ 7 is {3, 1, 2, 1}, which has length 4.
E) Problem Statement: Sort Array with Odd in Ascending and Even in
Descending Order
Given an array of integers, sort the odd numbers in ascending order and the
even numbers in descending order, while maintaining their relative positions in
separate groups.
Input:
An integer array arr[] of size N (1 ≤ N ≤ 10⁵) where each element arr[i] (−10⁶ ≤
arr[i] ≤ 10⁶).
Output:
Print the modified array where:
Odd numbers appear first in ascending order.
Even numbers appear next in descending order.
Example 1:
Input:
Copy
Edit
arr[] = {5, 8, 11, 6, 2, 1, 7}
Output:1 5 7 11 8 6 2
Explanation:
Odd numbers {5, 11, 1, 7} → Sorted as {1, 5, 7, 11}
Even numbers {8, 6, 2} → Sorted as {8, 6, 2}
Example 2:
Input: arr[] = {9, 4, 3, 10, 15, 2}