Programming Articles - Page 915 of 3366

Reduce the fraction to its lowest form in C++

Sunidhi Bansal
Updated on 03-Nov-2021 05:32:43

6K+ Views

Given two integers Num1 and Num2 as input. The integers can be represented as fraction Num1/Num2. The goal is to reduce this fraction to its lowest form.Using GCD to find highest denominatorWe will calculate the greatest common divisor of both numbers.Divide both numbers by that gcdSet both variables as quotient after division.Lowest fraction will be Num1/Num2.ExamplesInput − Num1=22 Num2=10Output − Num1 = 11 Num2 = 5Lowest Fraction : 11/5Explanation− GCD of 22 and 10 is 2.22/2=11 and 10/2=5Lowest fraction is 11/5Input− Num1=36 Num2=40Output− Num1 = 9 Num2 = 10Lowest Fraction : 9/10Explanation − GCD of 36 and 40 is 4.40/4=10 and 36/4=9Lowest ... Read More

Reduce the array to a single integer with the given operation in C++

Sunidhi Bansal
Updated on 03-Nov-2021 05:30:11

232 Views

Given an integer variable Number as input. Let us consider an array containing elements in the range 1 to Number in any order. If we perform an operation Number-1 times on an array such thatWe select two elements A and B from the arrayRemove A and B from arrayAdd sum of squares of A and B to the arrayWe will get a single integer value in the end; the goal is to find the maximum possible value for that element.Using Priority QueueIn order to maximize the end result, we will have to choose A and B such that they are ... Read More

Reduce the array to a single element with the given operation in C++

Sunidhi Bansal
Updated on 03-Nov-2021 05:26:04

281 Views

Given an integer variable Number as input. Let us consider an array containing elements in the range 1 to Number in sorted order. If we perform an operation on an array such that at each step the elements at odd positions are removed. Then the goal is to perform this operation N number of times till only a single element is left. Print that element at the end.Note-: The positioning of elements is such that the array at index 0 is at 1st position and so on.Test Cases for Number of elements in arrayInput Number=1, output = 1Input Number=2, output ... Read More

C Program to Redeclaration of global variable

Sunidhi Bansal
Updated on 03-Nov-2021 05:22:57

726 Views

We will understand how C and C++ behave differently in case we re-declare a global variable without initializing, redeclaring global variables with initialization, redeclaring global variables and initializing them twice. Also, we will repeat above combinations with local variables.1. A) C program : Redeclaring Global variables with no initialization#include int var; int var; int main(){    printf("Var = %d",var);    return 0; }OutputVar = 0B) C++ program : Redeclaring Global variables with no initialization#include using namespace std; int var; int var; int main(){    cout

Find the frequency of non-NA values in each row of an R dataframe.

Nizamuddin Siddiqui
Updated on 03-Nov-2021 05:30:50

261 Views

To find the frequency of non-NA values in each row of an R data frame, we can use apply function along with na.omit function.For Example, if we have a data frame called df that contains some NA values then we can find the frequency of non-NA values in each row of df by using the command as follows −apply(df,1, function(x) length(na.omit(x)))Example 1Following snippet creates a sample data frame −x1

Recursive Selection Sort in C++

Sunidhi Bansal
Updated on 03-Nov-2021 05:14:49

8K+ Views

Selection Sort is one of the sorting algorithms used to sort data by iterating an array from the beginning and replacing each element with the smallest element in the list. As we move forward the left array is sorted, and the right array is unsorted. Each move places the next smallest to the current position of the index by swapping.Selection Sort Algorithmint arr[5]= { 5,4,2,1,3 };int i, j ;Traverse from index i=0 to i

Reverse a number using stack in C++

Sunidhi Bansal
Updated on 03-Nov-2021 05:11:32

2K+ Views

We are given an integer number Num as input. The goal is to find the reverse of the number using stack.Stack:- A stack is a data structure in C++ which stores data in LIFO ( Last in First Out) manner. Major operations of stack are-:Declaration-: stack stck; //stck is now a stack variable.Finding Top using top(). Function stck.top() returns reference of top element in the stckRemoving Top using pop(). Function removes topmost element from the stckAdding element to top using push(). Function stck.push( value ) adds item value in stack. Value should be of type stck.Check if staxk is ... Read More

How to find the row sums if NA exists in the R data frame?

Nizamuddin Siddiqui
Updated on 03-Nov-2021 05:15:03

5K+ Views

To find the row sums if NA exists in the R data frame, we can use rowSums function and set the na.rm argument to TRUE and this argument will remove NA values before calculating the row sums.For Example, if we have a data frame called df that contains some NA values then we can find the row sums by using the below command −df$Row_Sums

Recursive Programs to find Minimum and Maximum elements of array in C++

Sunidhi Bansal
Updated on 03-Nov-2021 05:07:30

13K+ Views

We are given an integer array Arr[] as input. The goal is to find maximum and minimum elements among the array using recursive methods.Since we are using recursion, we will traverse the whole array till we reach length=1 then return A[0] which forms the base case. Else compare current element with present minimum or maximum and update its value by recursion for later elements.Let us see various input output scenarios for this −Input − Arr= {12, 67, 99, 76, 32};Output − Maximum in the array :99Explanation − Out of all elements 99 is maximum among them.Input − Arr= {1, 0, -99, 9, 3};Output − Minimum ... Read More

Recursive Program for Binary to Decimal in C++

Sunidhi Bansal
Updated on 03-Nov-2021 05:03:49

2K+ Views

We are given a string containing a binary number. The goal is to find the equivalent decimal number using the recursive method.A binary number can be converted to decimal using following method-: Traverse from LSB to MSB and multiply each with power of 2i Where 0

Advertisements