Alexandra has some distinct integer numbers a1,a2...an.
Count number of pairs (i,j) such that:
1≤ i ≤ n
1≤ j ≤ n
ai < aj
Input
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains a single integer n denoting the number of numbers Alexandra has. The second line contains n space-separated distinct integers a1, a2, ..., an denoting these numbers.
Output
For each test case, output a single line containing number of pairs for corresponding test case.
Constraints
1 ≤ T ≤ 4
1 ≤ n ≤ 100000
0 ≤ ai ≤ 109
All the ai are distinct
Example
2
2
2 1
3
3 1 2
Output:
1
3
Explanation
Case 1: Only one such pair: (2,1)
Case 2: 3 possible pairs: (2,1), (2,3), (3,1)
as I understand the problem is just counting how many Ai's are 1 <= Ai <= N and then apply ((R*(R-1))/2), R is the count of valid Ai's
But how would I do this using parallel_for_each (C++AMP) ? I need this for some algorithm that works with very long arrays and I think GPU would do this faster than CPU (even if I use all threads).
I have been writing a program to generate pairs of RSA keys using small prime numbers. I have been using mpz_invert() to get a value for d for the private key. On small prime numbers it calculates the correct value, but on larger primes it calculates incorrect values.
I'm assuming there must be an upper limit to the values the mpz_invert() function can handle? If so, what is this limit to avoid erroneous values?
My university assignment requires us to create a magic square. The definition of which can be found on Wikipedia and such, but essentially it's a (n) times (n) grid where all the row, column and diagonal totals meet the formula [n(n2 + 1)]/2. So a 3x3 would provide totals of 15 for example.
It's been requested that we use brute force for this, nothing mathematically fancy. I've succeeded in the task and initially during testing was coming up with a result of a 3x3 magic square after between 10 million and 1 billion attempts over a few minutes to nearly an hour.
I proceeded to refine my code and have as little as possible in the while loop which I'd break out of once the function had found a magic square, nothing really made much difference until I moved the declaration of my random to just before my while loop instead of within.
Random RanGenerator = new Random();
Once I'd done this I was getting 3x3 magic squares after a few thousand iterations in less than a second, which has completely baffled me. I can understand if it would save time, but how could that reduce the amount of attempts required to solve the magic square?
I have an assignment that I have to make a 3x3 magic square that sums up to 15 on all sides. I can't use arrays, just Loops and If and Else statements.
My first attempt was like this
//Program for printing magic square that sums up to 15.
#include <iostream> using namespace std; int main() { int a, b, c, d, e, f, g, h, i; int r1, r2, r3; int c1, c2, c3;
[Code] .....
I can't think of a way to loop that when it prints out, it will have a sum of 15 on all sides and/or print one of the 8 solution for a 3x3 magic square.
I have been asked to develop a program with 6 methods which I have presented below. The aim of the program is to find and generate a magic square with a given dimension. This is a console program and so the 'Main' is also provided. However, I am having a problem with my code. When ever I try to generate a magic square it continuously cycles through 'forever' and I have never yet got a magic square; no matter what dimension I enter.
I must use methods 'CreateRandomlyAssignedArray' and 'CheckSquareMatrix'. There is another method 'SearchForValue', which we were told to creat. How this can be useful.
I have provided my code below:
class Program { static Random rand = new Random(); static void Main(string[] args) { int[,] array = new int[5,5]; array = GenerateMagicSquare(5);
For example if I have typed 0xFF (a literal hex number that represents the value 255 for Unsigned Char or -1 for Signed Char) in part of my program. That 0xFF is treated as a Char not an Int, because the value is within the range supported by Char, the C compiler always tries to use the smallest datatype possible for the number that is needed for a literal value like this.
Unfortunately because Signed Char is the default Char type, 0xFF is translated into -1. I am wanting to use it to represent 255. So I'm trying to tell the compiler that 0xFF should be interpreted as either an Int or an Unsigned Char. How do I do this?
I already tried typing it with the magic letter "I", like this: 0xFFI
But that didn't work. What is the correct way to do this?
The code gives me seemingly logical integer values for size, width and height. But how would I find the max color value and so called "magic number" for this PPM image file?
I have a project to do in C and coming form Java I miss all the included features that are missing from C! I need to be able to store key value pairs in an quick and memory efficient manner. I've looked up using hash maps but I'm very new to C so don't really understand even the basic ones. I've looked at using a multi-dimensional array as I'm more comfortable with arrays but I'm unsure if that would count as a memory efficient and quick method?
I am attempting to combine two vectors into a vector of pairs. I want to be able to alter the first and second of each pair and have those alterations reflected in the original vectors. I thought the following code might work but get compilation errors about a lack of viable overload for "=" for the line with the call to std::transform:
void f() { std::vector<int> a = {1,2,3,4,5}; std::vector<int> b = {6,7,8,9,0};
Problem is i dont know n so the solution must be recursive. The possible splits are 2^(n-1)-1. I am really stuck and most of the code is complete for cart and i really don't want to restrict it to binary values.
i'm having difficulty with a problem, i need to add two big numbers suchas 54646774456776 and another one 445556777554 and it would print the result. how can i approach this problem without the use of arrays?
coming from Java, my experience with the Classes in C++ is quite limited.
Thats why I am having trouble converting the following (simple!) Java-Program.
Most examples with linked lists I found on the web describe how to implement the LinkedList class itself. But my problem is different: I want to use such a Class (I have a LinkedList class available on my system which is presumably OK).
Code: package main; import java.util.ArrayList; public class Main { public static void main(String[] args) { BoxList<String> sl = new BoxList<String>( new ArrayList<ABox<String>>()); sl.getList().add( new BoxPair2<String>(new BoxPair2<String>(
I'm supposed to write a code that takes an integer and splits it into individual numbers e.g. 234 becomes 2 3 4 .the individual numbers are then passed on to a function that accepts only one number at a time
All i could think of was this,but its a very bad method coz i dont know how long a number would be inputed into "Angle_in_degree"
I want it to store the integers as bits so that I am move them over and store them farther down the row as more numbers are added, but instead each new number is being added to the previous and I'm just getting a larger integer. Is this even a feasible way to store integers within an integer?
Write a C program that sorts an unsorted array of 5 integer numbers in ascending order by swapping numbers repeatedly. Specifically, first prompt the user to input 5 numbers and store the numbers in an array. Then, if the numbers in the array are not in ascending order, ask the user to provide the indices of two numbers in the array that the user wants to swap. If the user does not enter valid indices (i.e., outside 0 to4), then re-prompt the user to enter new indices. Continue prompting the user to provide indices of numbers to swap until the array becomes sorted in ascending order. Finally, when the array becomes sorted, the program should print "The array is now sorted in ascending order".
Implementation Requirements
Use the #define directive to define the size of the array.
Implement a function called "checkArrayOrder" to check whether an array is in ascending order or not. The prototype of the function is
"intcheckArrayOrder(int arr[], int size);". The function should return
1 if the array passed to the function is in ascending order0 if the array passed to the function is not in ascending order
Define the function prototype in your program.
Implement a function called "swap", whose prototype is "int swap(int arr[], int size, int i, int j);" which swaps the numbers in positions "i" and "j" of the array passed to the function and returns
1 if the indices"i" and "j" are within bounds (i.e., between 0 and 4) and the swap operation is performed correctly. 0 if the indices"i" and "j" are outside bounds so that the swap operation cannot be performed.
Define the function prototype in your program.
Use the function "checkArrayOrder" whenever your program needs to check whether an array is in ascending order or not; and the function "swap" to swap the numbers the user specifies (Note that, you should check whether the user provided indices are valid inside the function "swap" and not inside "main").
here is my code so far
Code: #define ARRAY_SIZE 5 void main() { int i, array[ARRAY_SIZE];
I'm trying to write a simple program that finds the minimum difference between two pairs of integers in a list/array. Of no surprise, it not work the first time I ran it so naturally i chucked in a whole bunch of print statements to debug
I encountered a peculiar error that is really stumping me.
#include <iostream> using namespace std; int closest_pair(int arr[]) { int i=0; int j=0; int size=0; int min=0;
I'm printing the size of the array in main and inside the function...and inside the function its giving me a size = 2 which is incorrect though the size printed in main is correct (size = 20). I further narrowed down the discrepancy to this statement: sizeof(arr)
Write a program asks the user for a positive integer value and then prints out all perfect numbers from 1 to that positive integer. I have been trying for some time, i found a way to check if its a perfect number or not but could not find a way to prints out all perfect numbers from 1 to that positive integer. I am here so far.
#include<iostream> #include<iomanip> using namespace std; int main(){ int n,i=1,sum=0; cout<<"Enter a number: ";
Why this class doesn't work for Subtraction, Division and Square Root. When I try to get results for Subtraction, it output right answer, but with any trash. And When I try to get answer for Division and Square Root, it just brakes and don't output anything. For Addition and Multiplication class work correctly.
I have to write a program where the user will input integer numbers. How many numbers they enter is unknown, therefor you should use a repetition structure for the input. When the user is done, they will enter -1 to exit.
Create a dynamic array if the size=2( the initial size must be 2) Repeat until user enters -1.
I have to do this without using vectors.
This is what i have, I cannot figure out what to put in main. I was thinking of a do-while?
Code: #include <iostream> using namespace std; void resize(int *[], int); int main() { int *listDyn; int size=2;
I want to count all the numbers in my text file (read.txt). Read text file consist of floating and integer number. Answer for the above file would be integer=2 and float =10.
I am trying to assign the integer value to unsigned char array. But it is not storing the integer values. It prints the ascii values. Here the code snippet
The values which are stored in uc[] is ascii values.I need the integer values to be stored in uc[]. I tried to do it with sprintf. but the output is not as expected. if I print the uc[i] it should diplay the value as 0,1,2....99.
changing a 9 digit integer into a new 9 digit integer through simple mathematical operations. For example, I need to change 123456789 into the new digit 456123789. Sometimes I need to change a 9 digit integer into an 8 digit integer. An example is 789062456 into 62789456. I can ONLY use simple mathematical operations (addition, subtraction, multiplication, division and modulo).