Posts

Showing posts from March, 2024

The function accepts two positive integers ‘r’ and ‘unit’ and a positive integer array ‘arr’ of size ‘n’ as its argument ‘r’ represents the number of rats present in an area, ‘unit’ is the amount of food each rat consumes and each ith element of array ‘arr’ represents the amount of food present in ‘i+1’ house number, where 0 <= i

Problem Description : The function accepts two positive integers ‘r’ and ‘unit’ and a positive integer array ‘arr’ of size ‘n’ as its argument ‘r’ represents the number of rats present in an area, ‘unit’ is the amount of food each rat consumes and each ith element of array ‘arr’ represents the amount of food present in ‘i+1’ house number, where 0 <= i Note:   Return -1 if the array is null Return 0 if the total amount of food from all houses is not sufficient for all the rats. Computed values lie within the integer range. Example:   Input:   r: 7 unit: 2 n: 8 arr: 2 8 3 5 7 4 1 2 Output:   4   Explanation: Total amount of food required for all rats = r * unit   = 7 * 2 = 14.   The amount of food in 1st houses = 2+8+3+5 = 18. Since, amount of food in 1st 4 houses is sufficient for all the rats. Thus, output is 4. C- Program for the above problem description is given below. #include<stdio.h> #defin...

Linked List

Image
What is linked list? A collection of nodes, each node contains two parts, an data part and an link part. Data part:- data part of a linked list contains the data to be stored in linked list. Link part:- which contains the address of the next node. In the case of the current node is only node in the list or current node is last node in the list then address part contains NULL value in it. Such a linked allocation based structure is called Linked list. Types of linked list:- There are 3 types of linked list. 1. Single Linked List (SLL). 2. Circular Linked List (CLL). 3. Double Linked List (DLL). Some basic algorithms: 1.Design an algorithm to insert an element 'e' into a single linked list (SLL). So that the inserted element becomes first in the list. Algorithm:- Insert_First_SLL. Input:- F, address of first node of SLL.             e, element to be inserted. Output:- F, updated. Method:-      n = CreateNode();      n.data = e; ...