// C++ program for connecting // n ropes with minimum cost #include <bits/stdc++.h> using namespace std; // A Min Heap: Collection of min heap nodes struct MinHeap { unsigned size; // Current size of min heap unsigned capacity; // capacity of min heap int* harr; // Array of minheap nodes }; // A utility function to create // a min-heap of a given capacity struct MinHeap* createMinHeap(unsigned capacity) { struct MinHeap* minHeap = new MinHeap; minHeap->size = 0; // current size is 0 minHeap->capacity = capacity; minHeap->harr = new int[capacity]; return minHeap; } // A utility function to swap two min heap nodes void swapMinHeapNode(int* a, int* b) { int temp = *a; *a = *b; *b = temp; } // The standard minHeapify function. void minHeapify(struct MinHeap* minHeap, int idx) { int smallest = idx; int left = 2 * idx + 1; int right = 2 * idx + 2; if (left < minHeap->size && minHeap->harr[left] < minHeap->harr[smallest]) smallest = left; if (right < minHeap->size && minHeap->harr[right] < minHeap->harr[smallest]) smallest = right; if (smallest != idx) { swapMinHeapNode(&minHeap->harr[smallest], &minHeap->harr[idx]); minHeapify(minHeap, smallest); } } // A utility function to check // if size of heap is 1 or not int isSizeOne(struct MinHeap* minHeap) { return (minHeap->size == 1); } // A standard function to extract // minimum value node from heap int extractMin(struct MinHeap* minHeap) { int temp = minHeap->harr[0]; minHeap->harr[0] = minHeap->harr[minHeap->size - 1]; --minHeap->size; minHeapify(minHeap, 0); return temp; } // A utility function to insert // a new node to Min Heap void insertMinHeap(struct MinHeap* minHeap, int val) { ++minHeap->size; int i = minHeap->size - 1; while (i && (val < minHeap->harr[(i - 1) / 2])) { minHeap->harr[i] = minHeap->harr[(i - 1) / 2]; i = (i - 1) / 2; } minHeap->harr[i] = val; } // A standard function to build min-heap void buildMinHeap(struct MinHeap* minHeap) { int n = minHeap->size - 1; int i; for (i = (n - 1) / 2; i >= 0; --i) minHeapify(minHeap, i); } // Creates a min-heap of capacity // equal to size and inserts all values // from len[] in it. Initially, size // of min heap is equal to capacity struct MinHeap* createAndBuildMinHeap(int len[], int size) { struct MinHeap* minHeap = createMinHeap(size); for (int i = 0; i < size; ++i) minHeap->harr[i] = len[i]; minHeap->size = size; buildMinHeap(minHeap); return minHeap; } // The main function that returns // the minimum cost to connect n // ropes of lengths stored in len[0..n-1] int minCost(int len[], int n) { int cost = 0; // Initialize result // Create a min heap of capacity // equal to n and put all ropes in it struct MinHeap* minHeap = createAndBuildMinHeap(len, n); // Iterate while size of heap doesn't become 1 while (!isSizeOne(minHeap)) { // Extract two minimum length // ropes from min heap int min = extractMin(minHeap); int sec_min = extractMin(minHeap); cost += (min + sec_min); // Update total cost // Insert a new rope in min heap // with length equal to sum // of two extracted minimum lengths insertMinHeap(minHeap, min + sec_min); } // Finally return total minimum // cost for connecting all ropes return cost; } // Driver program to test above functions int main() { int len[] = { 2,4,3 }; int size = sizeof(len) / sizeof(len[0]); cout << "Total cost for connecting ropes is " << minCost(len, size); return 0; }
Write, Run & Share Kotlin code online using OneCompiler’s Kotlin online compiler for free. It’s a modern and fast online playground for Kotlin, supporting the latest version and ideal for learning, experimenting, and sharing code instantly.
Kotlin is a statically typed, modern programming language developed by JetBrains. It runs on the JVM and is fully interoperable with Java. Kotlin is concise, expressive, and safe, and it’s officially supported by Google for Android app development.
The following is a simple Kotlin program that prints a greeting:
fun main() {
println("Hello, OneCompiler!")
}
OneCompiler’s Kotlin editor supports stdin. You can provide input using the I/O tab. Here's a sample program that reads a line of input and prints a greeting:
fun main() {
print("Enter your name: ")
val name = readLine()
println("Hello, $name")
}
val name: String = "OneCompiler" // Immutable
var age: Int = 25 // Mutable
Kotlin supports type inference, so explicit types are optional:
val city = "Hyderabad"
var count = 10
val score = 85
if (score >= 50) {
println("Pass")
} else {
println("Fail")
}
for (i in 1..5) {
println(i)
}
var i = 1
while (i <= 5) {
println(i)
i++
}
var j = 1
do {
println(j)
j++
} while (j <= 5)
fun add(a: Int, b: Int): Int {
return a + b
}
fun greet(name: String) = "Hello, $name"
val items = listOf("apple", "banana", "cherry")
for (item in items) {
println(item)
}
This guide provides a quick reference to Kotlin programming syntax and features. Start coding in Kotlin using OneCompiler’s Kotlin online compiler today!