#include using namespace std;void findTotalDistance(string S, int N){ vector prefixDistance(N); vector suffixDistance(N); int cnt = 0; bool haveOne = false; for (int i = 0;…

# Greedy

### Minimize value of |A – X| + |B – Y| + |C – Z| such that X * Y = Z

Given three integers A, B, and C, the task is to find the minimum possible value of |A – X|…

### Minimum number of coins needed to remove all the elements of the array based on given rules

Given an array arr of length N with values 1 and 2 indicating type 1 and type 2 elements and…

### Find all possible unique indices after reducing the array based on given conditions

Given an array arr of N integers, and an array of keys, such that key for each element in arr…

### K-th smallest positive integer having sum with given number equal to bitwise OR with given number

Given two positive integers X and K, the task is to find the K-th smallest positive integer Y, such that…

### Lexicographically smallest binary string formed by flipping bits at indices not divisible K1 or K2 such that count of 1s is always greater than 0s from left

#include using namespace std; void generateString(int k1, int k2, string s){ int C1s = 0, C0s = 0; int flag = 0; vector pos; for…

### Split given Array in minimum number of subarrays such that rearranging the order of subarrays sorts the array

Given an array arr[] consisting of N integers, the task is to find the minimum number of splitting of array…

### Split array into minimum number of subsets having difference between maximum and minimum element at most K

Given an array arr[] consisting of N integers and an integer K, the task is to find the minimum number…

### Minimize absolute difference between sum of subtrees formed after splitting Binary tree into two

import java.lang.Math;import java.io.*; class GFG { static class Node { Node left, right; int val; public Node(int val) { this.val = val; this.left = this.right = null; } } public static…

### Last element remaining after repeated removal of Array elements at perfect square indices

Given an array arr[](1-based indexing) consisting of N integers, the task is to find the last element remaining element after…