Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Questions on Array
  • Practice Array
  • MCQs on Array
  • Tutorial on Array
  • Types of Arrays
  • Array Operations
  • Subarrays, Subsequences, Subsets
  • Reverse Array
  • Static Vs Arrays
  • Array Vs Linked List
  • Array | Range Queries
  • Advantages & Disadvantages
Open In App
Next Article:
Greatest number that can be formed from a pair in a given Array
Next article icon

Largest number in an array that is not a perfect cube

Last Updated : 28 Dec, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

Given an array of n integers. The task is to find the largest number which is not a perfect cube. Print -1 if there is no number that is a perfect cube.

Examples: 

Input: arr[] = {16, 8, 25, 2, 3, 10}  Output: 25 25 is the largest number that is not a perfect cube.   Input: arr[] = {36, 64, 10, 16, 29, 25} Output: 36

A Simple Solution is to sort the elements and sort the numbers and start checking from back for a non-perfect cube number using cbrt() function. The first number from the end which is not a perfect cube number is our answer. The complexity of sorting is O(n log n) and of cbrt() function is log n, so at the worst case, the complexity is O(n log n).

An Efficient Solution is to iterate for all the elements in O(n) and compare every time with the maximum element and store the maximum of all non-perfect cubes.

Below is the implementation of the above approach: 

C++




// CPP program to find the largest non-perfect
// cube number among n numbers
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if a number
// is perfect cube number or not
bool checkPerfectcube(int n)
{
    // takes the sqrt of the number
    int d = cbrt(n);
 
    // checks if it is a perfect
    // cube number
    if (d * d * d == n)
        return true;
 
    return false;
}
 
// Function to find the largest non perfect
// cube number in the array
int largestNonPerfectcubeNumber(int a[], int n)
{
    // stores the maximum of all
    // perfect cube numbers
    int maxi = -1;
 
    // Traverse all elements in the array
    for (int i = 0; i < n; i++) {
 
        // store the maximum if current
        // element is a non perfect cube
        if (!checkPerfectcube(a[i]))
            maxi = max(a[i], maxi);
    }
 
    return maxi;
}
 
// Driver Code
int main()
{
    int a[] = { 16, 64, 25, 2, 3, 10 };
 
    int n = sizeof(a) / sizeof(a[0]);
 
    cout << largestNonPerfectcubeNumber(a, n);
 
    return 0;
}
 
 

C




// C program to find the largest non-perfect
// cube number among n numbers
#include <stdio.h>
#include <math.h>
#include <stdbool.h>
 
int max(int a, int b)
{
  int max = a;
  if(max < b)
    max = b;
  return max;
}
 
// Function to check if a number
// is perfect cube number or not
bool checkPerfectcube(int n)
{
    // takes the sqrt of the number
    int d = cbrt(n);
 
    // checks if it is a perfect
    // cube number
    if (d * d * d == n)
        return true;
 
    return false;
}
 
// Function to find the largest non perfect
// cube number in the array
int largestNonPerfectcubeNumber(int a[], int n)
{
    // stores the maximum of all
    // perfect cube numbers
    int maxi = -1;
 
    // Traverse all elements in the array
    for (int i = 0; i < n; i++) {
 
        // store the maximum if current
        // element is a non perfect cube
        if (!checkPerfectcube(a[i]))
            maxi = max(a[i], maxi);
    }
 
    return maxi;
}
 
// Driver Code
int main()
{
    int a[] = { 16, 64, 25, 2, 3, 10 };
    int n = sizeof(a) / sizeof(a[0]);
    printf("%d",largestNonPerfectcubeNumber(a, n));
 
    return 0;
}
 
// This code is contributed by kothavvsaakash.
 
 

Java




// Java program to find the largest non-perfect
// cube number among n numbers
 
import java.io.*;
 
class GFG {
   
 
// Function to check if a number
// is perfect cube number or not
static boolean checkPerfectcube(int n)
{
    // takes the sqrt of the number
    int d = (int)Math.cbrt(n);
 
    // checks if it is a perfect
    // cube number
    if (d * d * d == n)
        return true;
 
    return false;
}
 
// Function to find the largest non perfect
// cube number in the array
static int largestNonPerfectcubeNumber(int []a, int n)
{
    // stores the maximum of all
    // perfect cube numbers
    int maxi = -1;
 
    // Traverse all elements in the array
    for (int i = 0; i < n; i++) {
 
        // store the maximum if current
        // element is a non perfect cube
        if (!checkPerfectcube(a[i]))
            maxi = Math.max(a[i], maxi);
    }
 
    return maxi;
}
 
// Driver Code
 
 
    public static void main (String[] args) {
    int a[] = { 16, 64, 25, 2, 3, 10 };
 
    int n = a.length;
 
    System.out.print( largestNonPerfectcubeNumber(a, n));
    }
}
// This code is contributed
// by inder_verma
 
 

Python 3




# Python 3 program to find the largest
# non-perfect cube number among n numbers
import math
 
# Function to check if a number
# is perfect cube number or not
def checkPerfectcube(n):
     
    # takes the sqrt of the number
    cube_root = n ** (1./3.)
    if round(cube_root) ** 3 == n:
        return True
    else:
        return False
 
# Function to find the largest non
# perfect cube number in the array
def largestNonPerfectcubeNumber(a, n):
     
    # stores the maximum of all
    # perfect cube numbers
    maxi = -1
 
    # Traverse all elements in the array
    for i in range(0, n, 1):
         
        # store the maximum if current
        # element is a non perfect cube
        if (checkPerfectcube(a[i]) == False):
            maxi = max(a[i], maxi)
     
    return maxi
 
# Driver Code
if __name__ == '__main__':
    a = [16, 64, 25, 2, 3, 10]
 
    n = len(a)
 
    print(largestNonPerfectcubeNumber(a, n))
 
# This code is contributed by
# Surendra_Gangwar
 
 

C#




// C# program to find the largest non-perfect
// cube number among n numbers
using System;
public class GFG {
 
 
    // Function to check if a number
    // is perfect cube number or not
    static bool checkPerfectcube(int n)
    {
        // takes the sqrt of the number
        int d = (int)Math.Ceiling(Math.Pow(n, (double)1 / 3));
 
        // checks if it is a perfect
        // cube number
        if (d * d * d == n)
            return true;
 
        return false;
    }
 
    // Function to find the largest non perfect
    // cube number in the array
    static int largestNonPerfectcubeNumber(int []a, int n)
    {
        // stores the maximum of all
        // perfect cube numbers
        int maxi = -1;
 
        // Traverse all elements in the array
        for (int i = 0; i < n; i++) {
 
            // store the maximum if current
            // element is a non perfect cube
            if (checkPerfectcube(a[i])==false)
                maxi = Math.Max(a[i], maxi);
        }
 
        return maxi;
    }
 
    // Driver Code
 
 
        public static void Main () {
        int []a = { 16, 64, 25, 2, 3, 10 };
 
        int n = a.Length;
 
        Console.WriteLine( largestNonPerfectcubeNumber(a, n));
        }
}
/*This code is contributed by PrinciRaj1992*/
 
 

PHP




<?php
// PHP program to find the largest non-perfect
// cube number among n numbers
 
 
// Function to check if a number
// is perfect cube number or not
function checkPerfectcube($n)
{
    // takes the sqrt of the number
    $d = (int)round(pow($n, 1/3));
    // checks if it is a perfect
    // cube number
    if ($d * $d * $d == $n)
        return true;
 
    return false;
}
 
// Function to find the largest non perfect
// cube number in the array
function largestNonPerfectcubeNumber($a, $n)
{
    // stores the maximum of all
    // perfect cube numbers
    $maxi = -1;
 
    // Traverse all elements in the array
    for ($i = 0; $i < $n; $i++) {
 
        // store the maximum if current
        // element is a non perfect cube
        if (!checkPerfectcube($a[$i]))
            $maxi = max($a[$i], $maxi);
    }
 
    return $maxi;
}
 
// Driver Code
 
    $a = array( 16, 64, 25, 2, 3, 10 );
 
    $n = count($a);
 
    echo largestNonPerfectcubeNumber($a, $n);
 
 
// this code is contributed by mits
?>
 
 

Javascript




<script>
// Javascript program to find the largest non-perfect
// cube number among n numbers
 
// Function to check if a number
// is perfect cube number or not
function checkPerfectcube(n)
{
 
    // takes the sqrt of the number
    let d = Math.cbrt(n);
 
    // checks if it is a perfect
    // cube number
    if (d * d * d == n)
        return true;
 
    return false;
}
 
// Function to find the largest non perfect
// cube number in the array
function largestNonPerfectcubeNumber(a, n)
{
    // stores the maximum of all
    // perfect cube numbers
    let maxi = -1;
 
    // Traverse all elements in the array
    for (let i = 0; i < n; i++) {
 
        // store the maximum if current
        // element is a non perfect cube
        if (!checkPerfectcube(a[i]))
            maxi = Math.max(a[i], maxi);
    }
 
    return maxi;
}
 
// Driver Code
let a = [ 16, 64, 25, 2, 3, 10 ];
let n = a.length;
document.write(largestNonPerfectcubeNumber(a, n));
 
// This code is contributed by souravmahato348.
</script>
 
 
Output
25

Complexity Analysis:

  • Time Complexity : O(nlog3(val)), since there runs a loop from 0 to (n – 1) where val is the max value of the array.
  • Auxiliary Space: O(1), since no extra space has been taken.


Next Article
Greatest number that can be formed from a pair in a given Array

V

VishalBachchas
Improve
Article Tags :
  • Algorithms
  • Arrays
  • DSA
  • Mathematical
  • Technical Scripter
  • maths-cube
  • Technical Scripter 2018
Practice Tags :
  • Algorithms
  • Arrays
  • Mathematical

Similar Reads

  • Largest perfect cube number in an Array
    Given an array of N integers. The task is to find the largest number which is a perfect cube. Print -1 if there is no number that is perfect cube. Examples: Input : arr[] = {16, 8, 25, 2, 3, 10} Output : 25 Explanation: 25 is the largest number that is a perfect cube. Input : arr[] = {36, 64, 10, 16
    7 min read
  • Largest perfect square number in an Array
    Given an array of n integers. The task is to find the largest number which is a perfect square. Print -1 if there is no number that is perfect square.Examples:   Input : arr[] = {16, 20, 25, 2, 3, 10} Output : 25 Explanation: 25 is the largest number that is a perfect square. Input : arr[] = {36, 64
    7 min read
  • Largest number that is not a perfect square
    Given n integers, find the largest number is not a perfect square. Print -1 if there is no number that is perfect square. Examples: Input : arr[] = {16, 20, 25, 2, 3, 10| Output : 20 Explanation: 20 is the largest number that is not a perfect square Input : arr[] = {36, 64, 10, 16, 29, 25| Output :
    15+ min read
  • Perfect cube greater than a given number
    Given a number N, the task is to find the next perfect cube greater than N.Examples: Input: N = 6 Output: 8 8 is a greater number than 6 and is also a perfect cube Input: N = 9 Output: 27 Approach: Find the cube root of given N.Calculate its floor value using floor function in C++.Then add 1 to it.P
    3 min read
  • Greatest number that can be formed from a pair in a given Array
    Given an array arr[], the task is to find the largest number that can be formed from a pair in the given array.Examples: Input: arr[] = { 3, 1, 9, 2 } Output: 93 The pair (3, 9) leads to forming of maximum number 93Input: arr[] = { 23, 14, 16, 25, 3, 9 } Output: 2523 The pair (23, 25) leads to formi
    9 min read
  • Count of pairs in an Array whose sum is a Perfect Cube
    Given an array arr of distinct elements of size N, the task is to find the total number of pairs in the array whose sum is a perfect cube.Examples: Input: arr[] = {2, 3, 6, 9, 10, 20} Output: 1 Only possible pair is (2, 6)Input: arr[] = {9, 2, 5, 1} Output: 0 Naive Approach: Use nested loops and che
    15 min read
  • Smallest and Largest N-digit perfect cubes
    Given an integer N, the task is to find the smallest and the largest N digit numbers which are also perfect cubes.Examples: Input: N = 2 Output: 27 64 27 and 64 are the smallest and the largest 2-digit numbers which are also perfect cubes.Input: N = 3 Output: 125 729 Approach: For increasing values
    3 min read
  • Number of times the largest Perfect Cube can be subtracted from N
    Given a number N, at every step, subtract the largest perfect cube( ? N) from N. Repeat this step while N > 0. The task is to count the number of steps that can be performed. Examples: Input: N = 100 Output: 4 First step, 100 - (4 * 4 * 4) = 100 - 64 = 36 Second step, 36 - (3 * 3 * 3) = 36 - 27 =
    5 min read
  • Find largest d in array such that a + b + c = d
    Given a set S (all distinct elements) of integers, find the largest d such that a + b + c = d where a, b, c, and d are distinct elements of S. Constraints: 1 ? number of elements in the set ? 1000 INT_MIN ? each element in the set ? INT_MAX Examples : Input : S[] = {2, 3, 5, 7, 12} Output : 12 Expla
    15+ min read
  • Largest sphere that can be inscribed inside a cube
    Given here is a cube of side length a, the task is to find the biggest sphere that can be inscribed within it.Examples: Input: a = 4 Output: 2 Input: a = 5 Output: 2.5 Approach: From the 2d diagram it is clear that, 2r = a, where, a = side of the cube r = radius of the sphere so r = a/2. Below is th
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences