Found 26504 Articles for Server Side Programming

Reduce a number to 1 by performing given operations in C++

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

1K+ Views

Given an integer Number as input. The goal is to find the minimum number of steps or operations required to reduce the input Number to 1. Operations that can be performed will be-:If Number is even, then Divide it by 2.If Number is odd, then increment or decrement it by 1.ExamplesInput − Number=28Output − Minimum steps to reduce 28 to 1: 6Explanation−28 is even - divide by 2 = 1414 is even - divide by 2 = 77 is odd - increment by 1 = 88 is even - divide by 2 = 44 is even - divide by 2 = 22 ... Read More

How to check which value is NA in an R data frame?

Nizamuddin Siddiqui
Updated on 03-Nov-2021 05:37:48

15K+ Views

To check which value in NA in an R data frame, we can use apply function along with is.na function.For Example, if we have a data frame called df that contains some NA values then we can check which value is NA by using the command mentioned below −apply(df,2, function(x) is.na(x))This will return the data frame in logical form with TRUE and FALSE. Check out the below Examples to understand how it works.Example 1Following snippet creates a sample data frame −x1

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

224 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

271 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

710 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

253 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

Advertisements