Outputing 64bit integers in c++ - c++

I wrote the following code-
#include <iostream>
#include <tuple>
int sumarr(int64_t arr[], int length)
{
long long int sum= 0;
for(int i=0; i<length; i++)
{
sum += arr[i];
}
return sum;
}
void minimaxval(int64_t arr[], int length,int64_t *x,int64_t *y)
{
long long int c=0;
long long int d=10e9;
for(int j=0; j<5; j++)
{
if(arr[j] >= c)
{
c = arr[j];
}
if (arr[j] <= d)
{
d= arr[j];
}
}
*x=c;
*y=d;
}
int main()
{
int64_t arr[5];
int64_t p, q;
int64_t sum;
for(int k=0; k<5; k++){
std::cin>> arr[k];
if (arr[k]<1 || arr[k]>10e9){return 0;}
}
sum= sumarr(arr, 5);
minimaxval (arr, 5, &p, &q);
std::cout << sum-p << " " << (sum-q);
return 0;
}
This is my first question on stackoverflow. So I dont know a lot about format.
Input for the que. will be 5 space-separated integers.
Output should be the minimum sum and the maximum sum we can get by removing only one integer from the list.
It does not show any compiler issues.
It works fine for smaller integers but outputs a negative value for larger integers.
Where did I go wrong.

In 'summar' you have wrong return type. If you change it to int64_t everything will be fine.
P. S. And yes this is also my first answer)

Related

replace the two largest elements of an array, inputed by user

So basically i need to replace the two largest elements of an array, inputed by user. I don't understand why doesn't my code working.
#include <iostream>
using namespace std;
double Max (double a[], int n) {
double max1=0, max2=0;
for (int i=0; i<n; i++){
if(max1 < a[i]){
max2 = max1;
max1 =a[i];}
else
if(max2 < a[i]){
max2 = a[i];}
}
return max2,max1;
}
int main () {
setlocale(0,".1251");
double a[7];
cout<<"Input 7-digit array:\n";
for (int i=0; i<7; i++) cin >> a[i];
for (int i=0; i<7; i++) {
cout<<a[i]<<"\t";
}
system ("pause>>void");
return 0;
}
The function double Max (double a[], int n) is only defined, but you haven't called it. As it is right now, you are only outputting the same digits you inputted.
Also double Max (double a[], int n) can only return 1 double, so I'm assuming you were trying to return two variables return max2,max1; at once which isn't supported.
A fix to this could be by using a std::vector as data type and returning a 2 element array of max1 & max2 and then you would have to finish the code by actually calling the double Max (double a[], int n) function.
vector Maximums = Max(a[],7);
cout<< "\n max 1:\n" << Maximums[0] << "\n max 2:\n" << Maximums[1]

optimising count of inversion(i.e for i<j, a[i]>a[j]) for a large array

I am trying to count inversion(for eg.,for array [2,5,4,1] the inversion count is=4 namely (2,1),(5,4),(5,1),(4,1) using divide and conquer for a large set of arrays, I am getting a recursive count of value when a function executes each merge sort. I store all the count values in a vector and then again use sum operations, it works well for an array size of 70,000 but fails after it. I feel I am unnecessarily storing a large value to vector, instead, I am looking for a way to directly count the same but I am not getting a way to do the same, please help me in achieving it.
ps:the file link is this.
my code looks like;
#include<iostream>
#include<vector>
#include<fstream>
using namespace std;
long long greater_v(long long *array,long long ap,long long p){
long long numx=0;
for(long long i=0;i<p;i++){
if(array[i]>ap){
numx++;
}
}
return numx;
}
long long merge(long long U[],long long Lu,long long V[],long long Lv,long long S[],long long count1){
long long uf=0;long long vf=0;
for(long long sb=0;sb<Lu+Lv;sb++){
if(uf<Lu && vf<Lv){
if(U[uf]<V[vf]){
S[sb]=U[uf];uf++;}
else{
S[sb]=V[vf];
count1=count1+=greater_v(U,V[vf],Lu);
vf++;
}
}
else if(uf<Lu){
S[sb]=U[uf];uf++;
}
else{
S[sb]=V[vf];vf++;
}
}
return count1;
}
In this part I am looking for help where I am storing the value in the vector, instead, i want a way to directly count.
vector<unsigned long long int>v_val;
void MergeSort(long long arr[],long long n){
long long count=0;
//cout<<"sorting ";print(arr,n);
if(n==1)
return;
long long U[n/2];long long V[n-n/2];
for(long long i=0;i<n/2;i++){
U[i]=arr[i];
}
for(long long i=0;i<n-n/2;i++){
V[i]=arr[i+n/2];
}
MergeSort(U,n/2);
MergeSort(V,n-n/2);
count+=merge(U,n/2,V,n-n/2,arr,count);
v_val.push_back(count);
}
main function is;
int main(){
long long test_count=0;
ifstream file_num("pr_as_2.txt");
long long arr_num[100000];
for(long long i=0;i<100000;i++){
file_num>>arr_num[i];
}
unsigned long long int sum_val=0;
MergeSort(arr_num,70000);
for(size_t i=0;i<v_val.size();i++){
sum_val+=v_val[i];
}
cout<<sum_val;
}
look at this approach, it worked for me.
#include <bits/stdc++.h>
using namespace std;
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
unsigned int merge(int arr[], int temp[], int l, int m, int r) {
unsigned int inversions = 0;
int i = l;
int j = m;
int k = l;
while (i < m && j <= r) {
if (arr[i] <= arr[j]) {
temp[k] = arr[i];
i++;
} else {
temp[k] = arr[j];
j++;
inversions += m-i;
}
k++;
}
while (i < m) {
temp[k] = arr[i];
i++;
k++;
}
while (j <= r) {
temp[k] = arr[j];
j++;
k++;
}
for (int i = l; i <= r; i++) {
arr[i] = temp[i];
}
return inversions;
}
unsigned int count(int arr[], int temp[], int l, int r) {
unsigned int inversions = 0;
if (r > l) {
int m = (r+l)/2;
inversions = count(arr, temp, l, m);
inversions += count(arr, temp, m+1, r);
inversions += merge(arr, temp, l, m+1, r);
}
return inversions;
}
int main() {
int arr_size = 100000;
int arr[arr_size];
ifstream file("IntegerArray.txt");
string str;
int i = 0;
while (getline(file, str)) {
arr[i] = stoi(str);
i++;
}
// int arr[] = { 1, 20, 6, 4, 5 };
// int arr_size = sizeof(arr) / sizeof(arr[0]);
int temp[arr_size];
/* mergeSort(arr, 0, arr_size-1);
for (int i = 0; i < arr_size; i++) {
cout << arr[i] << " ";
} */
cout << count(arr, temp, 0, arr_size-1) << endl;
}

C++ program test case acceptance

I've attempted to solve a coding contest out of curiosity. Out there, there's one of the problems which couldn't pass all the test cases for my solution. Could there be any improvement in my solution that you would suggest that might help? TIA. The problem along with my solution is stated down below.
The Constraints:
1 ≤ M,N ≤ 10^7,
1 ≤ a[i],b[i] ≤ 10^7
The Output:
Print a single int that the maximum cool value that he
can obtain after buying two items with the given
amount of money
And my solution is:
#include<bits/stdc++.h>
using namespace std;
int main()
{
long long N, M, C, count;
cin>>N>>M>>C;
long long a[N], b[M];
for(long long i=0; i<N; i++){
cin>>a[i];
}
for(long long i=0; i<M; i++){
cin>>b[i];
}
sort(a, a+N);
sort(b, b+M);
if(a[N-1] + b[M-1] <= C){
count = (N-1) + (M-1);
cout<<count<<endl;
}
else{
cout<<"Not even closer"<<endl;
}
return 0;
}
Can anyone suggest what I could improve here to pass all the test?
After sorting, the price a[i] has a coolness equal to i.
Therefore, a solution consists in sorting a and b, and then maximize i+j such that a[i]+a[j] <= C
This last optimisation is performed by a simple for loop, with two indices, one for a and one for b
Complexity is dominated by sorting: O(NlogN + MlogM)
Note: the code currently posted in the question was edited after some comments, and do not correspond to the original code
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
int N, M, C;
std::cin >> N >> M >> C;
std::vector<int> a(N), b(M);
for (int i = 0; i < N; i++){
std::cin >> a[i];
}
for (int i = 0; i < M; i++){
std::cin >> b[i];
}
sort(a.begin(), a.end());
sort(b.begin(), b.end());
if(a[0] + b[0] > C) {
std::cout <<"E kemon aynabaji!!!" << std::endl;
return 0;
}
int max_cool = 0;
int iB = M-1;
for (int iA = 0; iA < N; ++iA) {
while (iB >= 0 && (a[iA] + b[iB] > C)) {
iB--;
}
if (iB < 0) break;
int cool = iA + iB;
if (cool > max_cool) max_cool = cool;
}
std::cout << max_cool << std::endl;
return 0;
}

cannot figure how to return multiple values from c++ function from online answers

Question:
Write a function sumArray() that has as parameters two arrays A and B and calculates and stores the sum of corresponding elements of the arrays. Include any additional parameter(s) you consider necessary.
Write a main() function that uses the function inputArray() in the previous slide to perform the input of two integer arrays X1 and X2 and uses sumArray() to calculate the sum of corresponding values of X1 and X2. It then displays the calculated values.
I was searching a way to return multiple values from a function but still it is not working even after trying some examples from SO. can anyone help me with it i just start learning it.
Problem: i can't figure out how to get the values of sumA and sumB in main().
Here are my codes:
#include <iostream>
using namespace std;
int sumArray(int a[], int b[]){
int sumA=0, sumB=0;
for(int i=0; i<4; i++){
sumA += a[i];
sumB += b[i];
}
return sumA,sumB;
}
void inputArray(int arg[], int n){
for(int i=0; i<n; i++){
cin>>arg[i];
}
}
int main(){
int firstarray[4];
int secondarray[4];
int l=4;
cout<<"Input 4 values for the array 1: ";
inputArray(firstarray,l);
cout<<"Input 4 values for the array 2: ";
inputArray(secondarray,l);
sumArray(firstarray,secondarray);
cout<<"sum of array 1 is: "<<firstarray<<endl;
cout<<"sum of array 2 is: "<<secondarray<<endl;
return 0;
}
This is the only way i have learned it.
You could accept references as 2 additional arguments. You can then set sumA and sumB to the correct values.
void sumArray (int a[], int b[], int & sumA, int & sumB)
{
sumA=0;
sumB=0;
for(int i=0; i<4; i++){
sumA += a[i];
sumB += b[i];
}
}
Then you can use it like
int firstarray[4];
int secondarray[4];
// do your stuff here
int sumA, sumB;
sumArray(firstarray,secondarray, sumA, sumB);
cout<<"sum of array 1 is: "<<sumA<<endl;
cout<<"sum of array 2 is: "<<sumB<<endl;
std::tuple<int,std::string> return_two()
{
return std::make_tuple(42, "don't panic");
}
auto sval = std::string{};
auto ival = 0;
std::tie(ival,sval) = return_two();
Why not just call that function twice?
Change your sumArray function to:
int sumArray(int array[]){
int sum=0;
for(int i=0; i<4; i++){
sum += array[i];
}
return sum;
}
Then for the rest of your code:
void inputArray(int arg[], int n){
for(int i=0; i<n; i++){
cin>>arg[i];
}
}
int main(){
int firstarray[4];
int secondarray[4];
int l=4;
cout<<"Input 4 values for the array 1: ";
inputArray(firstarray,l);
cout<<"Input 4 values for the array 2: ";
inputArray(secondarray,l);
cout<<"sum of array 1 is: "<<sumArray(firstArray)<<endl;
cout<<"sum of array 2 is: "<<sumArray(secondArray)<<endl;
return 0;
}
If you can't do it that way, then you can return a pointer to the first part of an array.
As seen here: Return array in a function

To count the comparisons in QuickSort, small size data is OK, but large size(like over 50 ints)would take a long time, why?

Following code runs correctly when the size of array below 40, but larger size makes it run very long time.
please tell me why it work like this. Thank you very much.
why the website always tells me to add some more details.
here is my code
#include<iostream>
using namespace std;
int Quicksort(int arr[], int l, int r) {
int p = arr[l];
int i = l+1;
for (int j = l+1; j <=r; j++){
if (arr[j] < p){
int tem = arr[i];
arr[i] = arr[j];
arr[j] = tem;
i +=1;
}
}
arr[l] = arr[i-1];
arr[i-1] = p;
int count = r-l;//each subarray has (r-l) comparisons
if (r-l ==0){
return 0;
}else{
int j =i-1;
if (j>l){
Quicksort(arr, l,j-1);
count +=Quicksort(arr,l,j-1);
}
if (j <r){
Quicksort(arr, j+1,r);
count += Quicksort(arr, j+1,r);
}
}
return count;
}
int main(){
int n;
cin >>n;
int arr[n];
for (int i = 0; i<n; i++){
cin >>arr[i];
}
cout<<Quicksort(arr, 0, n-1)<<endl;
for (int i = 0; i<n; i++){
cout << arr[i] <<' ';
}
cout <<endl;
return 0;
}
You are using the leftmost element of the partition as your pivot. This has a worst case scenario when the array is already sorted.
"But my array isn't already sorted!" you say. Well, look at these lines of code:
if (j <r){
Quicksort(arr, j+1,r);
count += Quicksort(arr, j+1,r);
}
You are calling QuickSort twice, the second time you call it, the partition is already sorted. Leading to a worst case scenario for the second call.
Your two consecutive calls to Quicksort with the same arguments and poor choice of pivot will result in exponential runtime. It can be tricky to write a good Quicksort algorithm, but there are excellent algorithm texts, such as by Robert Sedgewick, which have good implementations of Quicksort.
On the other hand, you can use the generic sort algorithm provided by the C++ STL.
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
int n;
cin >> n;
vector<int> arr;
for (int i = 0; i < n; i++) {
int val;
cin >> val;
arr.push_back(val);
}
sort(arr.begin(), arr.end());
for (int x : arr)
cout << x << ' ';
cout << endl;
return 0;
}