C++ :: Magic Pairs - Integer Numbers

Dec 10, 2013

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

My actual code is

#include <stdio.h>
using namespace std;
#ifndef ONLINE_JUDGE
#define getNumber getchar()
#define getString getchar()

[Code] ....

View 4 Replies


ADVERTISEMENT

C++ ::  Sorting Pairs Of Numbers Using AMP

Dec 15, 2014

I need numbers next to each other to be sorted (in increasing order).On the CPU I'd do something like this:

for(int i=0; i < length; i+=2) {
if(a[i]>a[i+1]) {
switch places...
}
}

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).

View 3 Replies View Related

C++ :: Program To Generate Pairs Of RSA Keys Using Small Prime Numbers

May 21, 2012

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?

View 1 Replies View Related

C# :: Creating A Magic Square

Feb 5, 2015

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?

View 6 Replies View Related

C/C++ :: 3x3 Magic Square That Sums Up To 15 On All Sides

Mar 10, 2015

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.

View 3 Replies View Related

C# :: Creating Normal Magic Squares

Jan 13, 2014

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);

[Code] ....

View 1 Replies View Related

C :: Magic Letter For Making A Constant Number Be Int?

Sep 26, 2014

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?

View 4 Replies View Related

C++ :: PPM Image File Magic Number And Max Color Value

Feb 28, 2015

When reading a PPM Image file, how would I find the magic number and the max color value?

int main() {
string imageFileName;
ifstream image;

imageFileName = "C:UsersDesktopdrink.ppm";
image.open(imageFileName.c_str(), ifstream::binary);

[Code] ....

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?

View 1 Replies View Related

C :: Storing Key - Value Pairs

Mar 26, 2013

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?

View 14 Replies View Related

C++ :: Vector Of Pairs Of References?

Jun 19, 2014

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};

[Code].....

View 3 Replies View Related

C/C++ :: Non Repeating Binary Pairs Of N Values

May 19, 2014

I am working with cart algorithm and i have the following problem,i need to try all possible splits of n values in order to determine the best.

So lets say i have 3 values("low","medium",high) then the possible splits(pairs) would be:

(assuming low=0,medium=1,high=2)

0,1-2 low,medium-high
1,0-2 medium,low-high
2,1-0 high,medium-low

For 4 values(a,b,c,d) it would be:

ab,cd
ac,bd
ad,bc
abc,d
adb,c
adc,b
bcd,a

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.

View 1 Replies View Related

C++ :: Searching A Vector Of Objects For Pairs

May 16, 2013

Say I have a vector of objects and I want to return an object based on a pair of strings. The strings can be in either order Ie; A B==B A.

In general, what do you think is the best way to do this?

View 5 Replies View Related

C :: Add Two Big Integer Numbers Without Use Of Arrays

Dec 1, 2014

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?

View 4 Replies View Related

C++ :: LinkedList Based Abstract Datatype For Pairs And Such

Feb 28, 2013

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>(

[code].....

View 10 Replies View Related

C :: Take Integer And Splits It Into Individual Numbers

Oct 9, 2013

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"

Code:
int Angle_in_degree,a,b,c
c= Angle_in_degree %10;
b=Angle_in_degree/10 %10;
a=Angle_in_degree/100 % 10;

function(a);
function(b);
function(c);

View 2 Replies View Related

C++ :: Storing A List Of Numbers In Integer?

Dec 6, 2014

I am writing trying to store a list of numbers into an integer as bits. So far I have this:

n=n & 0x0f
integer = integer<<2
integer = integer | n;
convert(integer));

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?

View 1 Replies View Related

C :: Program That Sorts Array Of 5 Integer Numbers

Oct 6, 2013

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];

printf("Enter %d numbers
", ARRAY_SIZE);
for(i=0; i<ARRAY_SIZE; ++i)
scanf("%d", &array[i]);

[Code] .....

I'm just not sure how to proceed. I'm not really sure how to progress the checkArrayOrder function to the swap function (in the if statement).

View 7 Replies View Related

C :: Convert String Made Of Many Numbers To Integer

Jan 6, 2015

I am supposed to convert a string made of many numbers between every number spaces are between, to an integer variable for every number..

How am I supposed to get over this problem?

Example: String: " 322 52 231"

View 1 Replies View Related

C/C++ :: Find Minimum Difference Between Two Pairs Of Integers In A List / Array

Jan 18, 2015

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;

[Code] .....

OUTPUT:
size_main: 20
arr_main: 80
arr[0]_main: 4
size: 2
arr: 8
arr[0]: 4
0: 34
1: -12
2: 18
3: 61
19: 75
closest_pair: 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)

View 2 Replies View Related

C++ :: 3 Integer Numbers - Find And Print Mean / Maximum And Second Minimum

Oct 25, 2014

The question is: Write a program that reads 3 integer numbers, then finds and prints the: Mean - Maximum & Second Minimum.

#include <iostream>
using namespace std;
int main () {
double a, b, c;
cout<< "Please enter three values"<<endl;

[Code] .....

View 6 Replies View Related

C++ :: Ask User For A Positive Integer Value And Then Prints Out All Perfect Numbers

Sep 17, 2013

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: ";

[Code] ....

View 4 Replies View Related

Visual C++ :: A Class For Operations With Large Integer Numbers?

Nov 27, 2012

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.

Its the link to this class [URL]

View 2 Replies View Related

Visual C++ :: Write A Program Where The User Will Input Integer Numbers?

Oct 21, 2014

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;

[code].....

View 5 Replies View Related

C++ :: How To Count Total Integer And Floating Point Numbers In Text File

Feb 16, 2013

I have a text file like below read.txt:

1.0 2.0 3.0 4.0
2.0 3.0 4.0 6
5.0 7 1.0 5.0

calc.cpp:

void main() {
FILE *fp;
fp=fopen("read.txt","r");
double *read_feature = new double*[3];

[Code] ....

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.

View 3 Replies View Related

C :: Assign Integer Value To Unsigned Char Array But It Is Not Storing Integer Values

Oct 25, 2013

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

Code: uchar uc[100];
for(i=0;i<100;i++)
{
uc[i] = i;
}

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.

View 9 Replies View Related

C++ :: Changing Integer Into New Integer With Simple Mathematical Operations?

Jun 15, 2014

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).

View 4 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved