Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

Sum Product Segments CodeChef Solution

Problem -Sum Product Segments CodeChef Solution

This website is dedicated for CodeChef solution where we will publish right solution of all your favourite CodeChef problems along with detailed explanatory of different competitive programming concepts and languages.
<<Previous CodeChef Problem Next Codechef Problem>>

Sum Product Segments CodeChef Solution in C++17

#include <bits/stdc++.h>
using namespace std;
#define ll long long int

int main()
{
    ll t;
    cin >> t;
    while (t--)
    {
        ll x, y, flag = 0;
        cin >> x >> y;
        ll s1, e1, s2, e2;
        s1 = x / 2;
        e1 = x / 2 + x % 2;

        for (ll i = 1; i * i <= y; i++)
        {
            if (y % i == 0)
            {
                s2 = i;
                e2 = y / i;
                if (s2 > e1 || e2 < s1)
                {
                    flag = 1;
                    break;
                }
            }
        }
        if (flag == 1)
        {
            cout << s1 << " " << e1 << endl
                 << s2 << " " << e2 << endl;
        }
        else
        {
            cout << "-1\n";
        }
    }
    return 0;
}

Sum Product Segments CodeChef Solution in C++14

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int main() {
	// your code goes here
	int t;
	cin>>t;
	while(t--)
	{
	    long long x,y;
	    cin>>x>>y;
	    long long s1=x/2;
	    long long e1=x-s1;
	    int f=0;
	    long long s2,e2;
	    for(long long i=1;i<=sqrt(y);i++)
	    {
	        s2=i;
	        if(y%s2==0)
	        {
	            e2=y/s2;
	            if((s2>e1||e2<s1)&&e2>=s2)
	            {
	                f=1;
	                break;
	            }
	        }
	    }
	    if(f)
	    {
	        cout<<s1<<" "<<e1<<endl;
	        cout<<s2<<" "<<e2<<endl;
	    }
	    else
	    cout<<-1<<endl;
	}
	return 0;
}

Sum Product Segments CodeChef Solution in PYTH 3

# cook your dish here
# cook your dish here

def divisors(a):
    ans=[]
    for i in range(1,int(a**0.5) + 1):
        if a%i==0:
            ans+=[[i,a//i]]
    return ans
for _ in range(int(input())):
    [x,y]=[int(i) for i in input().split()]
    d=divisors(y)
    low=x//2
    high=low
    if x&1:
        high+=1
    flag=0
    for i in d:
        if high < i[0] or low > i[1]:
            print(low,high)
            print(*i)
            flag=1
            break
    if flag==0:
        print(-1)

Sum Product Segments CodeChef Solution in C

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#define lli long long int
#define li long int
#define inf 1000010
#define mod 998244353

lli Floor(lli a, lli b)
{
    if(a%b == 0)
    {
        return (a/b);
    }
    if(a>=0&&b>=0 || a<=0&&b<=0)
    {
        return a/b;
    }
    else
    {
        return (a/b - 1);
    }
}

lli max(lli a, lli b)
{
    lli m = a;
    if(b>a)
        m = b;
    return m;
}

lli min(lli a, lli b)
{
    lli m = a;
    if(b<a)
        m = b;
    return m;
}

// Merges two subarrays of arr[].
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void merge(int arr[], int l, int m, int r)
{
    int i, j, k;
    int n1 = m - l + 1;
    int n2 = r - m;
 
    /* create temp arrays */
    int L[n1], R[n2];
 
    /* Copy data to temp arrays L[] and R[] */
    for (i = 0; i < n1; i++)
        L[i] = arr[l + i];
    for (j = 0; j < n2; j++)
        R[j] = arr[m + 1 + j];
 
    /* Merge the temp arrays back into arr[l..r]*/
    i = 0; // Initial index of first subarray
    j = 0; // Initial index of second subarray
    k = l; // Initial index of merged subarray
    while (i < n1 && j < n2) {
        if (L[i] <= R[j]) {
            arr[k] = L[i];
            i++;
        }
        else {
            arr[k] = R[j];
            j++;
        }
        k++;
    }
 
    /* Copy the remaining elements of L[], if there
    are any */
    while (i < n1) {
        arr[k] = L[i];
        i++;
        k++;
    }
 
    /* Copy the remaining elements of R[], if there
    are any */
    while (j < n2) {
        arr[k] = R[j];
        j++;
        k++;
    }
}
 
/* l is for left index and r is right index of the
sub-array of arr to be sorted */
void mergeSort(int arr[], int l, int r)
{
    if (l < r) {
        // Same as (l+r)/2, but avoids overflow for
        // large l and h
        int m = l + (r - l) / 2;
 
        // Sort first and second halves
        mergeSort(arr, l, m);
        mergeSort(arr, m + 1, r);
 
        merge(arr, l, m, r);
    }
}

lli sqroot(lli y)
{
    lli i = 1;
    for ( i = 1; i < y; i++)
    {
        if(i*i > y)
        {
            break;
        }
    }
    return i;
}

/*void init()
{
    Prime[0] = 2;
    Prime[1] = 3;
    int i, j = 2, k;
    for ( i = 4; i < inf/2; i++)
    {
        int flag = 1;
        for ( k = 0; k < j; k++)
        {
            if(i%Prime[k] == 0)
            {
                flag = 0;
                break;
            }
        }
        if(flag == 1)
        {
            Prime[j] = i;
            j++;
        }
    }
    return;
}*/

void program()
{
    lli x,y;
    scanf("%lld%lld", &x, &y);
    lli rightmin;
    if(x%2 == 0)
    {
        rightmin = x/2;
    }
    else
    {
        rightmin = x/2 + 1;
    }
    lli rooty = (lli)(sqroot(y)) + 1;
    lli i;
    for ( i = rooty; i >= 1; i--)
    {
        if(y%i == 0 && i<=y/i)
        {
            break;
        }
    }
    lli leftmax = i;
    if((min(leftmax,y/leftmax) > rightmin) || ((max(leftmax,y/leftmax) < x-rightmin)))
    {
        printf("%lld %lld\n", x - rightmin, rightmin);
        printf("%lld %lld\n", leftmax, y/leftmax);
    }
    else
    {
        printf("-1\n");
    }
}

int main(void) 
{
    // init();
    long long int T;
    scanf("%lld", &T);
    //printf("%d\n", T);
    // T = 1;
    for (long long int i = 0; i < T; i++)
    {
        program();
    }
	return 0;
}

Sum Product Segments CodeChef Solution in JAVA

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner in = new Scanner(System.in);
		int t = in.nextInt();
		while(t-- > 0){
		    long sum = in.nextLong();
		    long prod = in.nextLong();
		    
		    long a = sum/2;
		    long b = sum - a;
		    
		    long c = 0;
		    long d = 0;
		    for(int i =(int)Math.floor(Math.sqrt(prod)); i>0 ; i--){
		        if(prod%i == 0){
		            c = i;
		            d = prod/i;
		            
		            break;
		        }
		    }
		    
		    if(c > b || a > d){
		        System.out.println(a+" "+b+"\n"+c+" "+d);
		    }else{
		        System.out.println(-1);
		    }
		}
	}
}

Sum Product Segments CodeChef Solution in PYPY 3

import math
for _ in range(int(input())):
    a,b=map(int,input().split())
    l=a//2
    r=l
    if a&1:r+=1
    ans=False
    for i in range(1,int(math.sqrt(b))+1):
        if not b%i:
            y=b//i
            if (i<l and y<l ) or (i>r and y>r):
                x=i
                if x>y:x,y=y,x
                ans=True
                break
    if ans:print(l,r);print(x,y)
    else:print(-1)

Sum Product Segments CodeChef Solution in C#

using System;
namespace Paula
{
    class Program
    {

       static bool intersection(Tuple<long,long,long,long> maria)
       {
           if((maria.Item1>=maria.Item3 && maria.Item1<=maria.Item4) || (maria.Item2>=maria.Item3 && maria.Item2<=maria.Item4))
           return true;

           if((maria.Item3>=maria.Item1 && maria.Item3<=maria.Item2) || (maria.Item4>=maria.Item1 && maria.Item4<=maria.Item2))
           return true;

           return false;
       }
        public static Tuple<long, long, long, long> pepe (long x,long y)
        {
            

            long l1=0,r1=0,l2=0,r2=0;
            if(x%2==0)
            {
                l1=x/2;
                r1=l1;
            }
            else
            {
                l1=x/2;
                r1=l1+1;
            }


            int raiz=Convert.ToInt32(Math.Sqrt(y));

            for (int i = raiz; i > 0 ; i--)
            {
                if(y%i==0)
                {
                    r2=y/i;
                    l2=i;
                    break;
                }
                
                
            }

             Tuple<long, long, long, long> segmentos = new Tuple<long, long, long, long>(l1,r1,l2,r2);

            //System.Console.WriteLine(l1+" "+r1+" "+l2+" "+r2+" aaaaaaaaa ");


            return segmentos;


        }

        static void Main(string[] args)
        {
            
            int cp = int.Parse(Console.ReadLine());
            for (int c = 0; c < cp; c++)
            {
                string[] x=Console.ReadLine().Split(' ');
                long m=long.Parse(x[0]);
                long n=long.Parse(x[1]);

                if(intersection(pepe(m,n)))
                    System.Console.WriteLine(-1);

                else
                {
                    Tuple<long,long,long,long> Juan=pepe(m,n);
                    System.Console.WriteLine(Juan.Item1 + " " + Juan.Item2);
                    System.Console.WriteLine(Juan.Item3 + " " + Juan.Item4);
                }


                
                
            }
            
        }
    }
}

Sum Product Segments CodeChef Solution in NODEJS

process.stdin.resume();
process.stdin.setEncoding('utf8');

// your code goes here
process.stdin.on('data', (data) => {
    data = data.replace(/\n$/,"") ;
    const [n,...arr]= data.split("\n");
    getSegment(n, arr) ;
}) ;

function getSegment(n, arr) {
    let index = 0 ;
    while(n--) {
        let [x, y] = arr[index].split(' ') ;
        x = parseInt(x) ;
        y = parseInt(y) ;
        let l,r, l1, r1 ;
        if (x%2 === 0) {
            l = x/2 ;
            r = x/2 ;
        }
        else {
            l = Math.floor(x/2) ;
            r = Math.floor(x/2) + 1 ;
        }
        for (let i=1; i<= Math.ceil(Math.sqrt(y)); i++) {
            if (y%i === 0) {
                let div = y/i ;
                //console.log("Val ",i, div) ;
                if ((i < l && div <r) || (i > r && div  > r)) {
                    //console.log("IN") ;
                    l1 = i ;
                    r1 = div ;
                    break ;
                }
            }
        }
        if (l1 === undefined) {
            console.log(-1) ;
        }
        else {
            console.log(l+' '+r) ;
            console.log(l1+' '+r1) ;
        }
        index++;
    }
}

Sum Product Segments CodeChef Solution in GO

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
)

var scanner = bufio.NewScanner(os.Stdin)
var writter = bufio.NewWriter(os.Stdout)

func scan(args ...*int) {

	for _, arg := range args {
		scanner.Scan()
		*arg, _ = strconv.Atoi(scanner.Text())
	}
}

func scan64(args ...*int64) {
	for _, arg := range args {
		scanner.Scan()
		*arg, _ = strconv.ParseInt(scanner.Text(), 10, 64)
	}
}

func print(args ...interface{}) {
	writter.WriteString(fmt.Sprintln(args...))
}

func main() {
	scanner.Split(bufio.ScanWords)
	defer writter.Flush()
	var t int

	scan(&t)

	var x, y int64

	for i := 0; i < t; i++ {
		scan64(&x, &y)

		var l1, l2, r1, r2 int64

		l1 = x / 2
		r1 = x/2 + x%2

		sqrt := int64(math.Sqrt(float64(y)))
		for j := sqrt; j >= 1; j-- {
			if y%j == 0 {
				l2 = j
				r2 = y / j
				break
			}
		}

		if l1 < l2 && (r1 < l2) {
			print(l1, r1)
			print(l2, r2)
		} else if l2 < l1 && (r2 < l1) {
			print(l1, r1)
			print(l2, r2)
		} else {
			print(-1)
		}
	}
}
Sum Product Segments CodeChef Solution Review:

In our experience, we suggest you solve this Sum Product Segments CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Sum Product Segments CodeChef Solution.

Find on CodeChef

Conclusion:

I hope this Sum Product Segments CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Programming Language in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

Cognitive Class Answer

CodeChef Solution

Microsoft Learn

Leave a Reply

Your email address will not be published. Required fields are marked *