Sunidhi Bansal

Sunidhi Bansal

809 Articles Published

Articles by Sunidhi Bansal

Page 14 of 81

Print n smallest elements from given array in their original order

Sunidhi Bansal
Sunidhi Bansal
Updated on 15-Mar-2026 291 Views

Given an array of elements, the program must find the n smallest elements and display them in their original order of appearance in the array. Input : arr[] = {1, 2, 4, 3, 6, 7, 8}, k=3 Output : 1, 2, 3 Input k is 3 it means 3 smallest elements among the set needs to be displayed in original order like 1 than 2 and than 3 Syntax void findNSmallest(int arr[], int size, int k); Algorithm START Step 1 −> start variables as int i, max, pos, j, k=4 ...

Read More

Print n terms of Newman-Conway Sequence

Sunidhi Bansal
Sunidhi Bansal
Updated on 15-Mar-2026 471 Views

The Newman-Conway Sequence is a fascinating mathematical sequence that generates integers using a recursive formula. It starts with 1, 1 and each subsequent term is calculated based on previous values in the sequence. Syntax P(n) = P(P(n - 1)) + P(n - P(n - 1)) where P(1) = P(2) = 1 Algorithm The algorithm to generate n terms of Newman-Conway sequence is − START Step 1 → Input variable n (e.g. 20) Step 2 → Initialize variables as i, p[n+1], p[1]=1, p[2]=1 Step 3 → Loop For i=3 and i

Read More

Print the kth common factor of two numbers

Sunidhi Bansal
Sunidhi Bansal
Updated on 15-Mar-2026 376 Views

Given two numbers x and y, we need to find their kth common factor. Common factors are the numbers that divide both x and y without leaving a remainder. Syntax int findKthCommonFactor(int x, int y, int k); Algorithm The approach to find the kth common factor is − Find the smaller of the two numbers (maximum possible common factor) Iterate from 1 to the smaller number Check if the current number divides both x and y Count common factors and return when we reach the kth factor Example Let's ...

Read More

Print N lines of numbers such that every pair among numbers has a GCD K

Sunidhi Bansal
Sunidhi Bansal
Updated on 15-Mar-2026 289 Views

In C programming, we can print N lines of numbers where every pair among the numbers has a specific GCD (Greatest Common Divisor). This involves generating sequences of numbers that maintain the required GCD property. What is GCD? GCD stands for Greatest Common Divisor of two or more integers (excluding 0). It is the largest positive integer that divides all the given numbers without remainder. For example, to find the GCD of 48 and 180 − 48 = 2 × 2 × 2 × 2 × 3 180 = 2 × 2 × 3 × ...

Read More

Print the nearest prime number formed by adding prime numbers to N

Sunidhi Bansal
Sunidhi Bansal
Updated on 15-Mar-2026 416 Views

Given a number N, if it is not prime, the task is to find the nearest prime number by repeatedly adding prime numbers (starting from 2) to N until we get a prime result. Input: N=6 Output: 11 Explanation Since 6 is not prime, we add the first prime number 2 to get 6+2=8. Since 8 is also not prime, we add the next prime number 3 to get 8+3=11. Since 11 is prime, the output is 11. Syntax int isPrime(int n); int findNearestPrime(int n); Algorithm Check if ...

Read More

Print Reverse a linked list using Stack

Sunidhi Bansal
Sunidhi Bansal
Updated on 15-Mar-2026 2K+ Views

Given a linked list, the program must print the list starting from the end till the front using the stack data structure. This approach leverages the LIFO (Last In First Out) property of stacks to reverse the order of elements. Input : 10 -> 5 -> 3 -> 1 -> 7 -> 9 Output: 9 -> 7 -> 1 -> 3 -> 5 -> 10 Syntax struct Node { int data; struct Node* next; }; // Stack operations void push_to_stack(int stack[], int* top, int value); ...

Read More

Print Postorder traversal from given Inorder and Preorder traversals

Sunidhi Bansal
Sunidhi Bansal
Updated on 15-Mar-2026 1K+ Views

Given inorder and preorder traversals of a binary tree, we can construct the postorder traversal without actually building the tree. This approach uses the property that preorder gives us the root first, inorder helps us divide left and right subtrees, and postorder processes left subtree, then right subtree, and finally the root. Syntax void postorder(int pre_order[], int in_order[], int n); int find_value(int element, int in_order[], int n); Algorithm 1. Find the root element (first element of preorder) in inorder array 2. Recursively process left subtree (elements before root in inorder) 3. Recursively process ...

Read More

Print numbers having first and last bits as the only set bits

Sunidhi Bansal
Sunidhi Bansal
Updated on 15-Mar-2026 278 Views

The task is to print numbers that have exactly two set bits: one at the first (rightmost) position and one at the last (leftmost) position. These numbers follow the pattern where only the most significant bit and the least significant bit are set to 1. Set bits are binary digits with value 1, while unset bits have value 0. Numbers with first and last bits set follow the pattern: 1, 3 (11), 5 (101), 9 (1001), 17 (10001), etc. Syntax // Check if number has only first and last bits set if (!(n-1 & n-2)) ...

Read More

Print prime numbers with prime sum of digits in an array

Sunidhi Bansal
Sunidhi Bansal
Updated on 15-Mar-2026 686 Views

Given an array of elements, the task is to print those numbers that are prime and whose digit sum is also prime. This problem combines prime number checking with digit sum calculation. Input: arr[]={2, 4, 3, 19, 25, 6, 11, 12, 18, 7} Output: 2, 3, 25, 11, 12, 7 The output contains numbers where both the number itself and its digit sum are prime. For example − 2, 3, 7 are prime numbers, while 25(2+5=7), 11(1+1=2), 12(1+2=3) have prime digit sums. Numbers like 19(1+9=10) are excluded because 10 is not prime. Syntax ...

Read More

Print an array with numbers having 1, 2 and 3 as a digit in ascending order

Sunidhi Bansal
Sunidhi Bansal
Updated on 15-Mar-2026 783 Views

In this problem, we need to find and print all numbers from an array that contain the digits 1, 2, and 3. The numbers should be printed in ascending order. If no such numbers exist, we return -1. Syntax // Check if number contains digits 1, 2, and 3 int containsAllDigits(int num); // Sort and filter array elements void printNumbersWithDigits(int arr[], int n); Algorithm The approach involves the following steps − Step 1: Sort the array in ascending order Step 2: For each number, convert it to string format Step 3: ...

Read More
Showing 131–140 of 809 articles
« Prev 1 12 13 14 15 16 81 Next »
Advertisements